Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Killing the player, possible to propel the sprite once?


Luis Felipe
 Share

Recommended Posts

Hello there :)

 

I'm trying to figure out a way to propel the player once it is hit by a bomb(= death). First, in the update() function I'm checking if the player is alive, if false then I call a deadHero() function where I have this:

deadHero: function(){        this.hero.body.velocity.y = -500;    }

But since, I think, the function is being called into the update function, it continuously adds velocity to the player. I get this, but looking at the Click Burst example in the Phaser docs, I see that the emitter is called from the create() function which results in the function being called only once and not continuously 60FPS. How could I do the same but having it only act once the player is dead?

 

Any help would be greatly appreciated! Thanks!!

 

Luis

Link to comment
Share on other sites

Totally doable, but I think you should subscribe to the onKilled event to do this (pub/sub is a much nicer pattern to follow). Without seeing your code its tough to write you something 100% correct, but it'd look kind of like this.

someSprite.events.onKilled.dispatch = function(){  deadHero();};// Note that im not sure what your "this" context is referring to, but if you needed to pass that context you could dosomeSprite.events.onKilled.add(function(){  deadHero.call(this);}.bind(someContext));

EDIT: Just realized this is not gonna help you because the onKilled event happens after your sprite is killed and your deadHero method references the dead sprite. The workaround here is to have your deadHero() function load a new sprite animation at the current x/y coordinate of your dead hero.

Link to comment
Share on other sites

Awesome, thanks for your reply, It helped a lot! :) I ended up changing it a wee bit to make it work:
 

this.indicator.events.onKilled.add(this.deadHero, this);

I just added an indicator that is killed once the player touches the bomb, and when this is killed. I had to do this because otherwise I had to kill the player and it would just disappear

 

Thanks!

Link to comment
Share on other sites

EDIT: Just realized this is not gonna help you because the onKilled event happens after your sprite is killed and your deadHero method references the dead sprite. The workaround here is to have your deadHero() function load a new sprite animation at the current x/y coordinate of your dead hero.

 

That also looks like a great solution. If the hero sprite is killed, would I really still have access to it's x-y coordinates when it died?

Link to comment
Share on other sites

That also looks like a great solution. If the hero sprite is killed, would I really still have access to it's x-y coordinates when it died?

 

You are correct. The sprite object still exists, but is no longer alive (this is so you can do things like sprite.revive(), rather than recreate a whole new sprite object). All of the other object attributes remain for you to play with at your leisure.

Hope all this helps!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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