Jump to content

Creating a group of tiles and then doing something to that group


owen
 Share

Recommended Posts

Hi there.

Another easy one for you.

 

I have created a group of tiles so that I can do stuff to that entire group all at once.

 

I initialise it as follows:

 

yellowPadlocks = game.add.group();yellowPadlocks.enableBody = true;
Then I populated it with my tile objects like this:
 
 yellowPadlocks.create(tile);
And then at a certain point in my game I try doing something like this on the entire group:
 
yellowPadlocks.alpha = 0.1;  // make all the yellow padlocks translucent
Problem? It just doesn't work.  There is no error but it doesn't do anything.  Which makes me think I've got my syntax wrong when referring trying to do this last bit.   Any ideas?
 
(BTW I have confirmed the group is definitely populated with tiles)
 
I was hoping that I need not convert the tiles to sprites in order to do something simple like this. They are tiles because they are part of the map.
 
 
Thanks!
Owen
Link to comment
Share on other sites

To work around it I have written this function - but I'm certain it's the least efficient way of doing it and I am pretty sure I could perform an action upon an entire group in 1 line of code if I could figure out how :-)

 

function hideTilesWithIndex(ti) {    try {        map.forEach(function (t) {                    if ((t) && t.index==ti) {                console.log("Hiding tile with index " + ti);                t.alpha=0.1;                t.collideDown = false;                t.collideLeft = false;                t.collideUp = false;                t.collideRight = false;                t.destroy();             }        }, this, 0, 0, map.width, map.height, layer);    } catch (err) {        console.log(err.message);    }}
Link to comment
Share on other sites

I'm a little unsure but I kinda feel like groups aren't intended to support tiles like this. Tiles from my limited knowledge are not like sprites, they are more abstract and more like a data format than a display object. I'm sure you can add tiles to groups and see them in the groups, but I don't think you can do things such as set the opacity or even position of the group and have it filter down to its children like you would a group full of sprites, because the tiles don't sit neatly in the scene graph like other objects.

 

Please if anyone knows better correct me if I'm wrong, but I think this is roughly the case?

Link to comment
Share on other sites

Cheers guys.

 

Just to explain, I need this because I want impassable tiles to become passable when user collects a key. To see it working (using forEach loop instead of group method) see what happens when you grab a key here:

 

 http://www.owensouthwood.com/mrgoggles

 

@Lewster - Yep I get what you mean but for a platform game the platforms are all tiles (not sprites).  That's the thing: IMHO tiles do have logical properties and behaviour often need to be changed 'en mass'.  Then again I'm able to replace tiles with sprites now, so if that's the way forward, I can do it.  Just seems overkill to 'have to' use a sprite instead of a tile.

 

I plan to include things like:  Platforms that move up/down (elevators), left-to-right, and platforms that weaken and disintegrate as the player moves over them.  So I guess that's why I need this ability to apply behaviours to tiles, not just sprites.   Yes I can swap the tiles for sprites in some situations - maybe that is the accepted practice here?

 

I would be interested to know the view of anyone who's used Phaser for a platform game.  The self-made function I wrote (see above) works fine but I'm sure there's a simpler/better way built in to Phaser.

 

 

Thanks

Owen

Link to comment
Share on other sites

For moving platforms I'd say yes, you should definitely be using sprites for those - tiles shouldn't really move. Weakening/vanishing should be okay though I'd probably still want to use sprites simply because I'd be able to do some kind of falling animation easily. Tiles in Phaser occupy a bit of a niche really, where you want evenly sized and spaced, easily edited static objects with a fast collision routine.

Link to comment
Share on other sites

For moving platforms I'd say yes, you should definitely be using sprites for those - tiles shouldn't really move. Weakening/vanishing should be okay though I'd probably still want to use sprites simply because I'd be able to do some kind of falling animation easily. Tiles in Phaser occupy a bit of a niche really, where you want evenly sized and spaced, easily edited static objects with a fast collision routine.

 

Yes Lewster I think you hit the nail on the head with animation because that's definitely the realm of sprites in my mind.  So it seems that tiles are meant to be static unchanged things but it's confusing because I design a map of tiles and expect to be able to move/adjust those tiles 'in game'.

 

I like to be able to 'see' the sprites positioned in my map when I design it in Tiled.  So what I'm doing for some things (like collectible treasure) is using a tile as a 'marker'.  This 'marker' is the first frame of my sprite animation (just to make it obvious on the map design).  Then when the game runs it 'hides' the marker tile and generates a sprite in its place.  I think this approach works OK actually but I will probably need to improve the way I do it.

 

Based on what's been said here I will likely use the 'tile-to-sprite' approach for moving platforms.

Link to comment
Share on other sites

I think this is what object layers in Tiled are perfect for, but again I'm speaking entirely from the standpoint of someone who's not used Tiled or Tilemaps yet :)

 

I think you are right but I cannot figure for the life of me how to add sprites into the Object Layer using Tiled....  

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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