Jump to content

call a function on a group's exist members


nak3ddogs
 Share

Recommended Posts

helo. pls someone correct me.

my hero will throw coins to homeless people. and i wanna add a time limit for the coin. 

the problem is i dont know how to target the event's object.

sry for my eng and ty for any advice

 

        function update() {
 
 
           if (cursors.down.isDown && timerFire == 0){
            console.log('pina');
            timerFire = 20;
            var penz = bullets.getFirstExists(false);
            penz.timeleft = 100;
 
            if (facing === 'right') {
              penz.reset(player.x +10, player.y );
              penz.body.velocity.y=-200;
              penz.body.velocity.x=200;
            } else {
              penz.reset(player.x -10, player.y);
              penz.body.velocity.y=-200;
              penz.body.velocity.x=-200;
            }
            
          }
 
          if (timerFire != 0){
            timerFire--;
          }
 
 
          bullets.callAllExists(csokkentes, true);
 
          function csokkentes(e:target){
            if (e.timeLeft != 0) {
              e.timeLeft--;
            } else {
              e.exists = false;
            }
          }
Link to comment
Share on other sites

The callAll functions are meant to call a function that exists on the sprite, so it'd only really work if you were extending the sprite and allowing each one to manage its own timer. Because you're not doing that, you instead want to use the forEach functions which iterate through the group and do something arbitrary with each one. Here's an example of how you'd do it in your game - the following would go in your update function:

  bullets.forEachExists(function(bullet) {    if (bullet.timeLeft > 0) {      bullet.timeLeft--;    }    else {      bullet.exists = false;    }  });

However Phaser also has something built-in for Sprites that will kill the sprite after a period of time, Sprite.lifespan - you just have to remember to revive the bullet and reset its lifespan each time you grab a new one from the pool.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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