Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Z-ordering using Tiled / problem using sprite groups — Gideros Forum

Z-ordering using Tiled / problem using sprite groups

Hello everybody,

I am currently working on a little project alike to traditional RPGs, so think orthogonal tiles like in Final Fantasy / Chrono Trigger / Zelda aLttP for example. Main part of this tile engine is the example code coming with Gideros (sewers) and I have been able to include character movement (pixel perfect), simple collision detection (rectangles in an object layer) and applying the outer borders, so the map won't pan out of the screen so far. The characters are drawn in a specified layer between normal objects and those which should appear above the hero / character.

As my character sprite is taller than one tile, I've stumbled upon the problem that tiles drawn in the "upper layer" always are on top of this sprite, even if the character is in front of a tree for example.

My first idea was to add each row of the map individually to the scene so that - depending on the y-position of the character - he is always drawn above the tiles which are "behind" him. This turned out to be not that trivial because of my lack of understanding the principles of sprite groupings and the behaviour of the tilemap class. Simply putting the "allLayers:addChild(row)" inside the vertical "for loop" didn't do the trick, as sprites seem to be grouped or something.

Another option would have been to assign z-order values to each tile and sort the layer group (but then I would have to add each object as a separate layer)

Does anyone has a simple solution to this issue? I've got the feeling that it should be easier to be done ;)

Comments

  • Libertine1832Libertine1832 Member
    edited June 2014
    Thanks for the links, but this isn't solving my problem. Not the sorting itself, but creating different layers (e.g. per y-row) with the tilemap class at least don't work as I would expect them to. If it is possible to put every row on a seperate stage the only things that are to be sorted would be moving objects like the player and npcs.

    The first link is using box2d which I am not and draws every object seperatly, which would be quite the nuisance with tiled (at least if I am not overlooking something)
  • Hi @Libertine1832

    I don't know if I understood well your question, but I would do the following if I use Tilemap
     
    engineGame = Core.class(Sprite)
     
    function engineGame:init()
     -- first 
     self.background1 = Bitmap.new(Texture(...))
     --*
     --*
     self.camera = MyCamera.new() --Your camera implementation
     
     self.tilemap1 = TileMapMultiple.new(..., ...)
     self.tilemap2 = TileMapMultiple.new(..., ...) -- If you want to do it
     
     self.camera:addChild(self.tilemap1)
     self.camera:addChild(self.tilemap2)
     self:addChild(self.camera)
     --*
     --*
     self:getInfoMap()
    ---------------------------------------------
    -- after, you must to implement in the order
    -- you want to do it, in my case
    ---------------------------------------------
     self:walls()
     self:foes()
     self:coins()
     self:hero()
     self:trees()
     --*
     --*
     
     
    end
     
    ---------------------------------------------
    -- get information about map
    ---------------------------------------------
    function engineGame:getInfoMap()
    	--storage Object Names for use it after
    	self.objNames = self.tilemap2:getObjectNames() -- I use self.tilmap2 it's my last layer otherwise you use self.tilemap1
     
    	-- check objects names
    	for i=1, #self.objNames do
     
    		-- information about walls for physic
    		if string.find(self.objNames[i],"wall")~=nil then
    			local r1, r2 = self.tilemap:getObject(self.objNames[i])
    			table.insert(self.objWall,#self.objWall+1,r1)
    			table.insert(self.proWall,#self.proWall+1,r2)
     
    		-- information about enemies for physic
    		elseif string.find(self.objNames[i],"foe")~=nil then
    			local r1, r2 = self.tilemap:getObject(self.objNames[i])
    			table.insert(self.objFoe,#self.objFoe+1,r1)
    			table.insert(self.proFoe,#self.proFoe+1,r2)
     
    		-- information about hero for physic
    		elseif string.find(self.objNames[i],"hero")~=nil then
    			local r1, r2 = self.tilemap:getObject(self.objNames[i])
    			table.insert(self.objHERO,#self.objSB+1,r1)
    			table.insert(self.proHERO,#self.proSB+1,r2)
     
    		-- information about coins or any object for physic
    		elseif string.find(self.objNames[i],"coin")~=nil then
    			local r1, r2 = self.tilemap:getObject(self.objNames[i])
    			table.insert(self.objCoin,#self.objCoin+1,r1)
    			table.insert(self.proCoin,#self.proCoin+1,r2)
     
                     -- information about trees or any object over Hero without physic in my case
    		elseif string.find(self.objNames[i],"trees")~=nil then
    			local r1, r2 = self.tilemap:getObject(self.objNames[i])
    			table.insert(self.objTrees,#self.objTrees+1,r1)
    			table.insert(self.proTrees,#self.proTrees+1,r2)
     
                    end
     
    	end
     
    end
     
    --[[
    ---------------------------------------------
    -- your functions
    notes that no matter the order in which
    they are developed, they will be executed according
    to how you have defined in
    function engineGame:init()
    ---------------------------------------------
    ]]
     
    function engineGame:walls()
    --...
    end
     
    function engineGame:foes()
    --...
    end
     
    function engineGame:hero()
    --...
    end
     
    function engineGame:coins()
    --...
    end
     
    function engineGame:trees()
    --...
    end
    in tilemap of Gideros (this is an script of them (TileMapMultiple) and you download in this forum)
    I added the following functions that I used in
     function engineGame: getInfoMap ()
    As I show below
    function TileMapMultiple:getObjectNames()
    	local names = {}
    	for i=1, #map.layers do
    		local layer = map.layers[i]
    		if (layer.type == "objectgroup") then
    			table.insert(names,#names+1,layer.name)
    		end
    	end
    	return names
    end
     
    function TileMapMultiple:getObject(name)
    	for i=1, #map.layers do
    		local layer = map.layers[i]
    		if(layer.type == "objectgroup") then
    			if layer.name == name then
    				return layer.objects, layer.properties
    			end
    		end
    	end
    end
    I hope this can help you ;)
Sign In or Register to comment.