This has got to be an easy one that I’m missing. My character walks through walls. I’ve looked at the demo castle map in tiled but I’m not seeing how to mark a wall as a wall to be something you can’t walk through. How is this done? Thanks in advance
There are many ways to implement walls, but in CastleDemo we look for a tile property named “solid.” In the function gameLoop(event), at line 196, we call a function named obstacle. This function which is at line81 of main.lua retrieves the tile’s properties and checks for the “solid” property. If the tile is solid the function returns the boolean true.
To actually set a tile property you must open the map in the Tiled map editor, select the desired tileset in the tileset window, rightclick on a tile, and select “Tile Properties…” CastleDemo looks for a property named solid; if you are using the CastleDemo code you’ll want to double click <new property> and type “solid” without the quotations. Then double click the value field and type “true” without the quotations. This will make the tile solid in CastleDemo.
Once you’re done, go to File - > Export As, give your map file a name and set the file type to Json, and click save. Copy the json file into the CastleDemo/map folder, change line 13 to load your map instead of map/CastleDemo, and you’re good to go.
This is not set in stone! You can setup tile collisions using any properties you want to create your own effects and the like. For example you could make it so tiles are only solid from certain directions.
There are many ways to implement walls, but in CastleDemo we look for a tile property named “solid.” In the function gameLoop(event), at line 196, we call a function named obstacle. This function which is at line81 of main.lua retrieves the tile’s properties and checks for the “solid” property. If the tile is solid the function returns the boolean true.
To actually set a tile property you must open the map in the Tiled map editor, select the desired tileset in the tileset window, rightclick on a tile, and select “Tile Properties…” CastleDemo looks for a property named solid; if you are using the CastleDemo code you’ll want to double click <new property> and type “solid” without the quotations. Then double click the value field and type “true” without the quotations. This will make the tile solid in CastleDemo.
Once you’re done, go to File - > Export As, give your map file a name and set the file type to Json, and click save. Copy the json file into the CastleDemo/map folder, change line 13 to load your map instead of map/CastleDemo, and you’re good to go.
This is not set in stone! You can setup tile collisions using any properties you want to create your own effects and the like. For example you could make it so tiles are only solid from certain directions.
Solid from only one direction!? Madness! Guess you could do a check, somehow(?), of where the sprite is in relation to the character then check a property on the file like “solid_from_left_only = true”.
One method I’ve been using that’s helpful, is to set a solid property on a layer then check for it on collision / obstacle method. For instance, creating a “walls” layer having a “solid” property, preventing character movement. Seems like a good idea to save time editing each and every single tile.
[lua]
–DETECT OBSTACLES ------------------------------------------------------------
local obstacle = function(level, locX, locY)
local detect = mte.getTileProperties({level = level, locX = locX, locY = locY})
for i = 1, #detect, 1 do
– when you roll over a tile in another layer, this is no longer nil
if detect[i].tile then
– does its layers property solid?
if layers[i].properties and layers[i].properties.solid and detect[i].tile > 0 then
detect = “stop”
player:pause()
return detect
end
end
end
end
[/lua]
local obstacle = function(level, locX, locY) local detect = mte.getTileProperties({level = level, locX = locX, locY = locY}) for i = 1, #detect, 1 do if detect[i].properties then if detect[i].properties.solid and i == 1 then detect = "stop" player:pause() return detect end end end end
This code was not working for me (the standard code in the demo’s)
I had to delete the: “and i == 1”… but I dont know why?
Could you explain what the “and i == 1” does exactly in this case?
The function mte.getTileProperties({level = level, locX = locX, locY = locY}) returns all the tiles at a given location in a map level. Levels can contain multiple map layers, so the function returns an array containing multiple tiles and their properties. “and i = 1” limits the collision detection to tiles on the lowest layer of the level. If you have tiles you want to collide with which are not on the lowest layer of a level, the “and i = 1” will prevent the collision detection from detecting them.
Solid from only one direction!? Madness! Guess you could do a check, somehow(?), of where the sprite is in relation to the character then check a property on the file like “solid_from_left_only = true”.
One method I’ve been using that’s helpful, is to set a solid property on a layer then check for it on collision / obstacle method. For instance, creating a “walls” layer having a “solid” property, preventing character movement. Seems like a good idea to save time editing each and every single tile.
[lua]
–DETECT OBSTACLES ------------------------------------------------------------
local obstacle = function(level, locX, locY)
local detect = mte.getTileProperties({level = level, locX = locX, locY = locY})
for i = 1, #detect, 1 do
– when you roll over a tile in another layer, this is no longer nil
if detect[i].tile then
– does its layers property solid?
if layers[i].properties and layers[i].properties.solid and detect[i].tile > 0 then
detect = “stop”
player:pause()
return detect
end
end
end
end
[/lua]
local obstacle = function(level, locX, locY) local detect = mte.getTileProperties({level = level, locX = locX, locY = locY}) for i = 1, #detect, 1 do if detect[i].properties then if detect[i].properties.solid and i == 1 then detect = "stop" player:pause() return detect end end end end
This code was not working for me (the standard code in the demo’s)
I had to delete the: “and i == 1”… but I dont know why?
Could you explain what the “and i == 1” does exactly in this case?
The function mte.getTileProperties({level = level, locX = locX, locY = locY}) returns all the tiles at a given location in a map level. Levels can contain multiple map layers, so the function returns an array containing multiple tiles and their properties. “and i = 1” limits the collision detection to tiles on the lowest layer of the level. If you have tiles you want to collide with which are not on the lowest layer of a level, the “and i = 1” will prevent the collision detection from detecting them.
Im having the same issue as riflemaster. I dont really understand what you mean by “and i = 1” limits the collision detection to tiles on the lowest layer of the level." What do i need to do to fix this without deleting the “and i = 1”?
Nevermind i figured it out.
Im having the same issue as riflemaster. I dont really understand what you mean by “and i = 1” limits the collision detection to tiles on the lowest layer of the level." What do i need to do to fix this without deleting the “and i = 1”?
Nevermind i figured it out.