I am currently working on a top down rpg (imagine early legend of zelda style game mechanics). I have been dabbling with tilemaps for a little bit, but i was wondering if in a top down scenario i could set tiles to act as walls, event triggers, npc spawns, etc. Imagine how powerful this could be.. Code base game then open tile and make all of the maps without even touching a .lua file. Is this doable/realistic? I have been looking at these lines of code because they seem relevant..
mainScene:object(layer.objects[i].x + layer.objects[i].width/ 2, layer.objects[i].y + layer.objects[i].height/2,
layer.objects[i].width, layer.objects[i].height, layer.objects[i].name, layer.objects[i].type,
tilemap:getProperty(layer.objects[i], "score"))
This is from:
http://giderosmobile.com/forum/discussion/999/tiled-example-with-tile-layer-object-layer-and-box2d-collisions/p1Thanks!
“ The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time. ” - Tom Cargill
Comments
Box2d with 0 gravity would indeed work i believe... But it seems like using a sledgehammer on a nail. Will have to check it out...
by simple boundary check you mean writing a function to check if tile is.. say..#1 instead of #4? You don't mean draw bounding boxes everywhere manually right?
So you have a TileMap and each of it's tile is the same size. For example you have a tiles that are 64x64px
Let's first write a function that determines if tile i, j collides with your character:
1) if you have a small map, you can store all i, j of tile maps which you can't go on and check them in a loop every time you move
2) if you have a larger, but your character is smaller than one single tile, then you may want to check against all tiles around him
3) if you character is larger than one single tile, than you may want to check against all tiles on the screen, etc
Let's go with option 2)
First we need to determine, on which tile your character currently is:
Something like this:
Likes: Harrison
main.lua:117: attempt to call method 'getBounds' (a nil value)
Also, when I include print(allLayers:getX(), allLayers:getY()) in the onMouseUP function, I get values like -27 -61. I can imagine turning those values into x=1 and y=2, but even when I use print(map:getTile(1,1)), I get something similar:
attempt to call method 'getTile' (a nil value)
Maybe I should be querying a specific layer? But that doesn't work either. print(allLayers[1]:getTile(1,1)) gives me:
attempt to index field '?' (a nil value)
Clearly I'm confused how to query the TileMap variable to determine the tile in question.
Any help would be appreciated. Thanks.
When you
allLayers[1] could be a tilemap object, but are you sure that you have allLayers[1] and that you can access it?
If I recall correctly, in desert example the tilemap object is added as local group to allLayers, so it becomes unaccessible outside the function that creates it.
local tilemap = TiledMap.new("platformer.lua")
local tiles = tilemap:getTileMap()
local function onMouseUp(event)
currentTileX, currentTileY, currentTileFlip = tiles:getTile(13,6)
http://giderosmobile.com/forum/discussion/2028/another-tilemap-question-animated-tiles/p1
Here's my simplified version with two layers from the original Sewers2 project, Tilemap.lua from the TileMultiple.lua example and a simple attempt to query a tile like the animated-tiles example.
Of course, I know it's because I'm not querying a Tilemap object. But at this point, I really don't know how to extract that information from my code. The animated-tiles example works with one layer. Maybe it's just not possible with two layers?!
I do believe that you need to keep different tilesets on separate layers though, because of the way that setTile works (you have to use another texture from the same tileset).
still I am wondering if there is a better way to achieve the same result
function TiledMap:getTileMap(layer) --layer added
if layer then
return self.worldmap[layer]
else
return self.worldmap
end
end
i.e. we index the self.worldmap table of TileMap layers. What if self.worldmap was a Sprite, instead of a table. How would you access a TileMap layer? In TileMap.lua, I changed the definition of self.worldmap from {} to
self.worldmap = Sprite.new()
On line 54, I changed the table.insert to
self.worldmap:addChild(group)
And finally I rewrote the GetTileMap function to be
function TiledMap:getTileMap(layer) --layer added
return self.worldmap:getChildAt(layer)
end
Depending on what exactly I do, I either don't see anything, or I get an index error. Like I said, I'm happy with tables, but Giderosmobile seems like it's built with Sprites in mind (like the Desert and Sewers examples). So I'm still trying to understand the world of classes instead of the being content in the world of tables.
Thanks again.
Maybe there's a way to get a TileMap from the Sprite 'TiledMap'?
in Lua, "Classes don't exist", and theoretically everything is a table:
Sprite too is a table with extended functionality - like any other "class".
http://lua-users.org/wiki/ObjectOrientationTutorial
I think that would be possible to get a tilemap layer directly from an instance of the Sprite TiledMap, the "problem" is that you can't access locals outside their scope, and that this code is adding the tilemap object (which is what you need to retrieve later) to a local Sprite (group) before group itself is added to TiledMap Sprite.
Unless you save a reference to each tilemap layer, I can't see how to retrieve them.
It's usually better to use local variables because they can be collected by garbage collector as soon as they are no longer referenced, but
If you need to access something "later" you need to store it as a property of a global object, or as a global variable:
self.worldmap has nothing to do with the display on screen of the tilemap saved there (in fact you can see the tilemap layer even if you don't have self.worldmap) it's just an "address" to find it later