Corona SDK로 앵그리버드 스타일 게임 30분만에 만들기

October 24, 2013

Corona SDK로 앵그리버드 스타일 게임 30분만에 만들기

Cocos2D 또는 Sprite Kit으로 아래 컨셉앱을 개발한다면 어느정도 개발시간이 필요할 것이다. 그런데 Corona를 사용해서 만들면 아주 단순하고 쉽고 명확하다. 주석으로 간단하게 설명을 하도록 하겠다. 1~13까지 하나 하나씩 복사&붙여넣기로 따라해보면 변화되는 모습을 살펴볼 수 있다.


-- 1. 스테이터스 바 숨기기
display.setStatusBar(display.HiddenStatusBar)

-- 2. Box2D를 사용하여 물리현상 적용 
require("physics")
-- 3. 물리 현상을 실행한다. 
physics:start()
-- 4. 중력값을 적용한다. x, y축. 작성하지 않으면 기본 0, 9.8 중력이 적용됨
physics.setGravity(0, 9.8)    

-- 5. 원을 생성해서 화면에 보여준다. display.newCircle([parentGroup,], xCenter, yCenter, radius)
local ball = display.newCircle(30, 200, 20)
-- 6. 원의 색상을 변경한다. 
ball:setFillColor(255, 255, 0)
-- 7. ball에 물리현상을 적용.  움직이므로 dynamic 타입. physics.addBody(object, [bodyType,], {density=d, friction=f, bounce=b, [,radius=r], [,filter=f], })
physics.addBody(ball, "dynamic", {density=1.0, friction=0.3, bounce=0.2, radius=20})

-- 8. 사각형을 생성해서 화면에 보여준다. display.newRect([parentGroup,], left, top, width, height)
local plate = display.newRect(-100, 700, 3000, 20)
-- 9. plate에 물리현상을 적용. 고정된 것은 static 타입. physics.addBody(object, [bodyType,], {density=d, friction=f, bounce=b, [,radius=r], [,filter=f], })
physics.addBody(plate, "static", {density=1.0, friction=0.3, bounce=0.2})

-- 10. for문으로 산모양으로 블럭을 쌓는다. 위치값은 잘 알아서. 색상은 math.random()을 사용하여 랜덤값으로 설정.
for i=0, 4 do
    for j=1, i do
        local block = display.newRect(1000 - i * 100 + j * 40, 700 - j * 100 - j * 30 + 20, 30, 100)
        block:setFillColor(math.random(200, 255), 0, 0)
        physics.addBody(block, "dynamic", {density=1.0, friction=1.0, bounce=0.0})

        if j < i then
            local slate = display.newRect(1000 - i * 100 + (j+1) * 40 + 10 , 700 - (j+1) * 100 - j * 20, 30, 100)
            slate.rotation = 90
            slate:setFillColor(0, math.random(255), math.random(255))
            physics.addBody(slate, "dynamic", {density=1.0, friction=1.0, bounce=0.0})

        end
    end
end

-- 11. 터치 이벤트 함수를 만든다. 추후 이벤트 리스너에서 콜백으로 사용한다. 
-- touch 이벤트 사용 예. 참조. http://docs.coronalabs.com/api/event/touch/phase.html 
function circleTouchEvent( event )
    if event.phase == "began" then
            print( "began phase" )

        display.getCurrentStage():setFocus( ball )
        ball.isFocus = true

    elseif ball.isFocus then
        if event.phase == "moved" then

            print( "moved phase" )

        elseif event.phase == "ended" or event.phase == "cancelled" then
            print( "ended phase" )

            display.getCurrentStage():setFocus( nil )
            ball.isFocus = false
            -- 12. http://docs.coronalabs.com/api/type/Body/applyLinearImpulse.html 참조 
            ball:applyLinearImpulse( event.xStart - event.x, event.yStart - event.y , ball.x, ball.y )
            -- ball:applyForce( event.xStart - event.x, event.yStart - event.y , ball.x, ball.y )

        end
    end

    return true
end

-- 13. 터치 이벤트 함수를 만든다. 추후 이벤트 리스너에서 콜백으로 사용한다. 
ball:addEventListener("touch", circleTouchEvent )
저작자 표시
비영리
Array