Jump to content

My Object layer has sprites... so how do I get them to display?


owen
 Share

Recommended Posts

Sorry this is very simple newbie question - I've looked for answers but can't find a simple example of this.

 

I have a Tiled map with a Tile Layer ("Tile Layer 1") and an Object Layer ("Object Layer 1").   The Tile Layer is displaying fine but I don't know how to get anything to show up from my Object Layer.  The idea is that my Object Layer is going to be purely for moving sprites while my tile layer is for static platforms, scenery etc.

 

So far I have only attempted to add 1 sprite (a "1 ton weight") of which I've added four occurrences in the map (see screen shot).

 

In my code I have the following for my Object Layer but I don't know how to finish the job.

 

In preload() I have attempted to preload the sprite sheet:

game.load.spritesheet('sprites32x32', 'assets/ss-sprites32x32.png', 32, 32);

In create() I have attempted to initialise a group for these:

    tons = game.add.group();    tons.enableBody = true;

But what comes next? I need to add my 4 x 'ton' sprites into the group and make them appear on screen.  (I don't know the syntax, can you help?)  If it helps, I have given each of them a 'spritename' property of "ton" in Tiled.  I was thinking I could somehow add all the sprites where spritename=="ton" to the "tons" group.

 
What would be the simplest way to get them to appear?  (I can worry about their physics later but once displayed I'll want them to drop on to the platform underneath).
 
 
Any ideas how I can get these sprites to appear?
 
Thanks
Owen

post-9550-0-80877000-1408043955_thumb.gi

post-9550-0-59156600-1408043961.gif

post-9550-0-67414300-1408043965.gif

Link to comment
Share on other sites

 

Thanks I will use it.  But I have one more problem. The example includes this line:

// And now we convert all of the Tiled objects with an ID of 34 into sprites within the coins groupmap.createFromObjects('Object Layer 1', 34, 'coin', 0, true, false, coins);

But how do I know what the Object ID is for the object in question (in this example 34) so I can use it in my code?  Is it possible to see the object ID anywhere on Tiled?

 

Thanks,

Owen

Link to comment
Share on other sites

Hmm I may be wrong but I think you can actually work it out by looking at your tileset if do something like

var id = tileset.firstgid + tspos

...where tspos is the number of tiles along from the top-left of the tileset.?   Does that sound right?   I will try it later.

 

Ideally though I would like to make use of the properties of my tileset tiles. As per my original post if have a property "spritename" with vaue "ton" then surely I could add all of the objects with spritename="ton" to the game?  (Instead of relying on numeric indexes of tiles)

Link to comment
Share on other sites

Hmm I may be wrong but I think you can actually work it out by looking at your tileset if do something like

var id = tileset.firstgid + tspos

...where tspos is the number of tiles along from the top-left of the tileset.?   Does that sound right?   I will try it later.

 

Ideally though I would like to make use of the properties of my tileset tiles. As per my original post if have a property "spritename" with vaue "ton" then surely I could add all of the objects with spritename="ton" to the game?  (Instead of relying on numeric indexes of tiles)

 

As far as I know (but maybe I'm wrong), you can't do that, but you could use foreach to check every tile if is has the property you want.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...