2015년 6월 26일 금요일

corona sdk 에서 tiled 사용하기 13 – Moving a Map via Touch


https://github.com/anthonymoralez/lime-tutorials/tree/master/tutorial-13

NOTE: This tutorial is copied from the original at OutlawGameTools.com

13 – Moving a Map via Touch (터치를 통해서 맵이동)

Difficulty: Beginner
Duration: 10 minutes
Description:
This tutorial shows off how you can easily move your map around via tap/touch events. It also shows that platformer style maps aren’t the only thing you can make.

Step 1: Creating your map (맵 만들기)

Any map will do for this however I have created an RPG style map just to be different.
이번에는 RPG스타일로 맵을 만들었는데 기존에 사용하던 맵으로 해도 됩니다.
The Map
You can download the map here.

Step 2: Adding the tap code (탭 코드 넣기)

Firstly we will see how you can move around the map based on a tap from the user, naturally you could use any other method you like for actually getting the position you wish to move the map to.
You will need to add all the code like normal to simply load up a map and its visual, no need for physics or any other features for now.
Now create a tap event listener function like so:
물리 설정이나 다른 설정은 필요없고 아래 탭 이벤트 핸들러에 코드를 넣으면 됩니다. 그러면 터치 하는곳으로 맵의 중심이 이동됩니다.
그리고 아래 소스 그대로 하면 안됩니다.
"." -> ":" 로 변경하세요. 완벽한 소스는 해당 링크에 있으니 참고하세요.
local worldPosition = lime.utils:screenToWorldPosition(map, screenPosition)
local onTap = function(event)
    local screenPosition = { x = event.x, y = event.y }
    local worldPosition = lime.utils.screenToWorldPosition(map, screenPosition)
    -- Instantly jump to the new position
    map:setPosition(worldPosition.x, worldPosition.y)
    -- Fade out, set position and then fade back in again
    --  map:fadeToPosition(worldPosition.x, worldPosition.y, 1000)
    -- Slide to the new position
    --  map:slideToPosition(worldPosition.x, worldPosition.y, 2000)
end
Runtime:addEventListener("tap", onTap)

Step 3: Run your game (게임 실행 하기)

If you run your game now and tap somewhere on the map you will notice that the map gets recentred imediately around the tap position, try commeting back in the other map movement functions above to see them in action.

Step 4: Adding the touch code (터치 코드 넣기)

Dragging the map is even simpler, just add a touch event listener function like this:
드래그 이벤트에 해당 코드르 넣으면 쉽게 처리가 됩니다.
local onTouch = function(event)
    map:drag(event)
end
Runtime:addEventListener("touch", onTouch)
You may want to comment out the tap listener for now to see this working.

Step 5: Run your game again

Now run your game and drag your map around.
Resources:
Completed Project: git clone https://github.com/anthonymoralez/lime-tutorials
Tileset: Download

댓글 없음:

댓글 쓰기