Jump to content

Sprite lifespan problem.


Arcanorum
 Share

Recommended Posts

Greetings.

 

So I decided to try using sprite's lifespan property to dictate how far a projectile could go before being killed, as outOfBoundsKill wasn't suitable for my purposes (and I got some undesired results from it).

 

But using lifespan is giving me very unreliable/inconsistent results.

 

I have a guy firing projectiles that move towards wherever the user clicks. The projectiles are stored in a group of sprites, which I have used setAll('lifespan', 2000); on.

 

The guy starts firing, a new projectiles appearing every X amount of time, but not all of the sprites get killed after the lifespan time has elapsed. Some do get killed as expected, but most of them just fly off forever. It appears to be random which ones behave properly.

 

Anyone else found this problem or a solution to it?

 

Also, does sprite.reset restore a sprites lifespan?

Link to comment
Share on other sites

http://docs.phaser.io/Phaser.Sprite.html#reset

 

Resets the Sprite. This places the Sprite at the given x/y world coordinates and then sets alive, exists, visible and renderable all to true. Also resets the outOfBounds state and health values. If the Sprite has a physics body that too is reset.

 

It's not explicitely said in the doc that it resets lifespan.

So, I guess you'll have to set it again manually, which sounds weird but that's just one var to set after the reset call, so...

Link to comment
Share on other sites

So this is how it is implemented in the source:

    if (this.lifespan > 0)    {        this.lifespan -= this.game.time.elapsed;        if (this.lifespan <= 0)        {            this.kill();            return false;        }    }

Which makes sense for having to set the lifespan to a positive number to enable the life timer again.

 

What I'm still stuck on is why some sprites that have gone over the allotted time are just not being checked and killed after they have exceeded the lifespan threshold.

Link to comment
Share on other sites

Have you got a for iteration loop doing the checks and lifespan on the group of sprites to be killed at the alotted period you set?

Have you any code examples or snippets on what you are doing? I don't personally use lifespan but to achieve the same result you require would be simple in the terms of creating a custom sprite timer but that is just overlooking what lifespan does for you in phaser.

Link to comment
Share on other sites

Why do you need to use setAll? Wouldn't that just override all of the previously fired bullets' lifespans every time you fire again? Hard to know what you did without seeing code but could you try when creating a new fired bullet just set its' lifespan individually and see how it goes?

Link to comment
Share on other sites

Yea, setting lifespan again when each new projectile is fired does give the desired results. I realized I will have to do this regardless if I want to be able to keep reusing sprites.

 

When I initialize my projectiles I use setAll for some common values that I want all the sprites to have.

initialise      :   function(){        this.projectiles = game.add.group();        this.projectiles.enableBody = true;        this.projectiles.physicsBodyType = Phaser.Physics.ARCADE;        this.projectiles.createMultiple(10, 'fireball', 0, false);        this.projectiles.setAll('anchor.x', 0.5);        this.projectiles.setAll('anchor.y', 0.5);        this.projectiles.setAll('lifespan', 2000);    }

What I'm concerned about is why some of the first round of projectiles aren't killed when they all do in fact have a lifespan given to them at the start.

 

I'm happy to move on with the current solution, even if there is something that bugs me about this.

Link to comment
Share on other sites

Well I haven't used lifespan before and now I'm on mobile but when you're setting the lifespan on initialize is it possible that all ten are being killed after two seconds? Then when you fire them again they're being brought back to life but don't have a lifespan anymore because they already went through their original setting of it? If that's the case it could explain your issues with it working at first and then not working but the workaround would be the same as I mentioned before.

Link to comment
Share on other sites

Just another thought: lifespan is decremented whenever the Sprite updates. If you've created a sprite, but not yet fired it, it's still updating, so its lifespan is still going down. If when you eventually fire it its lifespan has expired, it won't kill itself again. Basically you should set the lifespan at the point the sprite is fired, not created.

Link to comment
Share on other sites

  • 1 year later...

I am using Phaser 2.4.7 and am experiencing something similar with lifespan. Either the value isn't decrementing, or the kill method isn't firing after it reaches zero. The result is bullets that last forever and bring the game to a crawl. And yes, I am setting lifespan upon firing, not upon creation. 

I've had to manually kill the sprite with a timer: 

setTimeout(function() {
    bullet.kill(); 
},bullet.lifespan); 

Of course, this isn't a permanent solution because I don't want bullets killed when the game is paused. Wonder if anyone has experienced the same problem? 

I will note that these are physics-enabled bullets. Wonder if some wires are getting crossed with arcade physics. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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