Anyone have luck with getting objects between tile layers?

Trying to replicate the tutorials and just can’t seem to get an object layer working between two tile layers. Some sample code…

[code]-- Assuming there is a tiled file with an object layer sandwiched between two tile layers already…

– Create the map object as usual

– Fetch the layer
local layer = map:getObjectLayer(“objects”)

– Method A: Create the object listener before creating the mapvis
local function onObject(object)
local objectA = display.newRect(layer.group, object.x, object.y, 32, 32)
end
layer:addObjectListener(“basic”, onObject)

– Method B: Add an existing object
local objectB = display.newRect(0,0,32,32)
layer:addObject(objectB)
[/code]

Both methods successfully create objects. And no error is thrown so in both cases the object should (seemingly) be added to the object layer. But in both cases the object layer is still above all tile layers.

(A great way to test - make the (top) tile layer cover everything. If it works, you shouldn’t see any objects from the objects layer) [import]uid: 41884 topic_id: 27205 reply_id: 327205[/import]

All layers in Lime are placed in groups so you can move your second tile-layer to the front this way:

local tileLayer1 = map:getTileLayer("Tile Layer 1")  
local objectLayer = map:getObjectLayer("Object Layer 1")  
local tileLayer2 = map:getTileLayer("Tile Layer 2")  
  
local objectB = display.newRect(100,100,132,132)  
objectLayer:addObject(objectB)  
  
tileLayer2.group:toFront()  

By doing so, your object layer will then be positioned between your tile-layers. [import]uid: 129287 topic_id: 27205 reply_id: 110565[/import]

Thanks Dr.Mabuse. It worked!

Kinda dumbfounded as to why that is needed (or rather, why the default layer order is ignored) but at least I can get back to making code progress with this :slight_smile: [import]uid: 41884 topic_id: 27205 reply_id: 110668[/import]

UPDATE: Unfortunately, this solution breaks Lime commands connected to the layer, i.e.: getPropertyValue() no longer works. I can fetch the tiles just fine but that’s it. They have no properties to grab.

So I guess I need to find another solution.

EDIT: And just to test, I did attempt to a whole new “fetch layer again, fetch tile again, fetch property value again” and it only failed on the property value part. [import]uid: 41884 topic_id: 27205 reply_id: 110756[/import]

I’ve been struggling with grabbing properties as well, but I don’t think it has to do with moving tile layer 2 to the front. There are still issues with the properties related to this link (which we have discussed earlier):
http://developer.anscamobile.com/forum/2011/08/27/moving-tile-leaves-tile-values-detection-behind

However I’ve figured out some ways of dealing with it.

Using the code from my last post, you can check for a property this way:

--Assuming there is a tile at tile layer 2 with the property "door" and the value "open"  
gridPosition = { row = 1, column = 1 }  
local tileGrab = tileLayer2:getTileAt( gridPosition )  
if tileGrab.door then  
 print ("Door found")  
end   
if tileGrab.door == "open" then  
 print ("Door is open")  
end   

Note! You might have to pass the true argument if you move your tiles around:

local tileGrab = tileLayer2:getTileAt( gridPosition, true )  

Other code snippets that might be useful:

-- Lists the properties and values of all tiles that have got properties  
local tiles = tileLayer2.tiles  
for i=1, #tiles, 1 do  
 local tileProperties = tiles[i]:getProperties()  
 for key, value in pairs(tileProperties) do  
 local property = tileProperties[key]  
 print(property:getName(), property:getValue())  
 -- Here you can store each tile (tiles[i]) in a table if needed!  
 -- Or you can check for specific properties (if key == "door") before you store them.  
 end  
end  
-- Lists the values of all the tiles with property "door"  
local tiles = tileLayer2.tiles  
for i = 1,#tiles do  
 if tiles[i]:getProperty("door") then  
 print (tiles[i]:getPropertyValue("door"))  
 end  
end  
-- Lists every grid-position which have a tile with property "door".  
local tiles = tileLayer2.tiles  
for i = 1,#tiles do   
 if tiles[i]:getProperty("door") then  
 print ("There is a door at row:"..tiles[i].row.." column:"..tiles[i].column)  
 end   
end  

If you need to remove or set a property you can do it this way:

gridPosition = { row = 1, column = 1 }  
local tileGrab = tileLayer2:getTileAt( gridPosition )  
--remove a property:  
tileGrab.door = nil  
--set a property:  
tileGrab.door = "closed"  

From this you should be able to tweak the code to your need. Just be sure to use the latest Beta.

Hope this might help.
[import]uid: 129287 topic_id: 27205 reply_id: 110775[/import]

Yeah, it turned out I was making an elaborate and stupid mistake. I’ve emailed Graham about the tile layer issue, but I’ve got fetching properties working, at least.

(Your functions look great; not useful right away but there’s certain things I had no idea about, like a .tiles property! Super useful) [import]uid: 41884 topic_id: 27205 reply_id: 110781[/import]