Jump to content

creating infinite objects


Chrischi
 Share

Recommended Posts

So I want to create an arrow in the background that keeps on repeating (and always goes from the top to the bottom) 
 
How I am trying to realise this is by first creating a group and a loop in the create method 
this.bgArrowGroup = this.add.group();    this.bgArrowGroup.enableBody = true;    this.bgArrowTimer = this.game.time.events.loop(1000,this.bgArrowCreate,this);
and bgArrowCreate() looks the following
bgArrowCreate: function(){var bgArrow = this.bgArrowGroup.getFirstExists(false);        if(!bgArrow){            bgArrow = this.bgArrowGroup.create(this.game.world.centerX,this.game.world.centerY,'bgArrow');        }                bgArrow.anchor.setTo(0.5);        bgArrow.body.velocity.y = 1000;        bgArrow.checkWorldBounds = true;        bgArrow.outOfBoundsKill = true;        bgArrow.reset(this.game.world.centerX,this.game.world.centerY);        console.log(this.bgArrowGroup);}

The idea is to recycle bgArrow after it left the screen. The problem is that the arrow only appears in the center of the screen without moving but doesn't move and phaser keeps on creating new objects so I end up with infinite objects. 

Can anyone tell me where my mistake is? Honestly cannot find it. 

 

Link to comment
Share on other sites

What happens if you change this line to grab the first existing sprite (true) instead of the first non-existing sprite (false)?

// var bgArrow = this.bgArrowGroup.getFirstExists(false);var bgArrow = this.bgArrowGroup.getFirstExists(true);

Reference: http://phaser.io/docs/2.4.2/Phaser.Group.html#getFirstExists

 

I like to explicitly tell JavaScript conditional statements what I mean when I'm programming. It just-so-happens that null is a falsy value, but I don't feel comfortable relying on the idea that now and forever in the future, a sprite will be a true value to JavaScript.

 

Try this code:

bgArrowCreate: function() {	// bgArrow is either the first existing sprite in this group, or null.	var bgArrow = this.bgArrowGroup.getFirstExists(true);	// If bgArrow is null, it must not exist. So create it:        if (bgArrow === null) {            bgArrow = this.bgArrowGroup.create(this.game.world.centerX,this.game.world.centerY,'bgArrow');        }        bgArrow.anchor.setTo(0.5);        bgArrow.body.velocity.y = 1000;        bgArrow.checkWorldBounds = true;        bgArrow.outOfBoundsKill = true;        bgArrow.reset(this.game.world.centerX,this.game.world.centerY);        console.log(this.bgArrowGroup);}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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