Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by dmen

I have about 15 entities on screen, and set the zIndex of each when the entity is created.
So far, so good. Later on however, during game play, I spawn a new entity that needs to go above some other entities, and behind yet some others - ie it needs to go between them.
I can't get it to work though, and have tried with and without sortEntitiesDeferred() and niether works.
Essentially I create my level and set all the zIndexes. Entity A is at 55 and Entity B is at 60. In update() I create a new entity and set the zIndex to 59, but it still appears above both A and B.
I guess I am a bit unclear as to when to use sortEntitiesDeferred. I did it at the end of my level creation, and then in update when I create the new one, but that doesn't work either.
Hope this makes sense...

1 decade ago by dmen

Regarding sortEntitiesDeferred() - if I set entities zIndexes in one spot, and keep gaps between them, does the call to sortEntitiesDeferred() keep those zIndexes - or does it reorder without gaps?
In other words, if I put Entity A at zIndex 50 and Entity B at zIndex 60 and then call sort... and then later spawn a new Entity C at zIndex 55 - can it go between the two? Because it's not for me...

1 decade ago by Joncom

Sounds like you may already being doing this, however, just to be clear, this is the process:

1. Spawn your new entity.
2. Call ig.game.sortEntitiesDeferred();

If your problem persists, please post the code segment from your update function in which you are spawning and sorting entities.

1 decade ago by dmen

Yup, that is what I was doing.
In my init I spawned a few entities like so - about 15 in total, but these are the two that matter:

//pitcher
this.pitcher = ig.game.spawnEntity(EntityPitcher, 772, 318, { level: this.currentLevel });
this.pitcher.zIndex = 55;

//batter
this.batter = ig.game.spawnEntity(EntityBatterRight, 449, 423);
this.batter.zIndex = 60;

ig.game.sortEntitiesDeferred();

And then in update, if a conditional is met I do:

this.flyball = ig.game.spawnEntity(EntityBallHit, 936, 735, { hr: true });
this.flyball.zIndex = 57;
ig.game.sortEntitiesDeferred();

And the ball would be above the batter entity...


I ended up doing it a different way - I sapwn the flyball in init at it's 57 zIndex, and stick it off screen, and just call a fly method in it - and move it back off screen when I'm done. Instead of creating/destroying it I just keep it around and that is working just fine now.

thanks.

1 decade ago by Joncom

Glad you got it sorted out. Just something to note about the way spawning entities works: when you spawn an entity, no matter what the zIndex, it will appear on top of everything very briefly. This is because sortEntitiesDeferred has a short delay before it works. And because by default entities are always added to the top of the entities array in ImpactJS.

However, but the sound of it, you were not experiencing just a brief moment, but rather a persistent issue. In which case, I'm not sure what was going wrong for you.
Page 1 of 1
« first « previous next › last »