Corona SDK + Tiled + Ceramic Tile Engine : Create Collision Map?

心已入冬 提交于 2019-12-08 13:48:37

问题


I'm making a simple tiled-based game, with a:

  • Tile Map with collision layer ( made by Tiled , loaded with Ceramic Tile Engine )
  • a character moving inside the map

What I want to do: Move character around in Tile Map, with respecting collision layer in Tile Map.

Here is the code:

-- Load Map
local ceramic = require("Ceramic")
local map = ceramic.buildMap("maps/map.lua")


-- Create Character
hero = display.newImage("images/man.png")
hero.x = 0
hero.y = 0
map.layer['World']:insert(hero)

-- Create buttons
btnLeft = display.newImage("images/btnLeft.png", 10, 10)
btnRight = display.newImage("images/btnRight.png", 150, 10)

-- Button Event Listeners
btnLeft:addEventListener("tap", moveLeft)
btnRight:addEventListener("tap", moveRight)

-- Button functions
function moveLeft()
    hero.x = hero.x - 50
end

function moveRight()
    hero.x = hero.x + 50
end

The problem is, the map is separate with the character. How can I put the character "into" the map? What did I miss?

Note: the tile map is Top-Down angle.

Note: The Map has 4 layers: Background, World, Doors, Collision, and I want player NOT to walk into Collision layer's areas.


回答1:


In order for the player to collide, you'll need to implement some type of collision detection - whether it be with or without physics.

Which means you can either...

  1. Create a non-physical collision system with checking for nearby "collidable" tiles
  2. Implement Corona's Box2D physics in your map.

Nota Bene: When using Ceramic, it's easier to use Box2D, but once the Twilight Engine comes out, non-physical collisions should get easier to use.

Here I'll only give the procedure for Box2D. It's done quite simply by either making single tiles physical or making an entire layer's tiles physical. Single tiles can be made physical by editing the tile properties and adding physics:enabled = true in them. To see a slightly more in-depth explanation about physics, you can check out this link. It also applies to single tiles.

You can make entire layers physical by adding that property to your layer's property list; to change single tiles' physical body, edit the tile-specific properties.



来源:https://stackoverflow.com/questions/19352745/corona-sdk-tiled-ceramic-tile-engine-create-collision-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!