Jump to content

Creating sprites inside a for loop breaks the loop?


NewGuy
 Share

Recommended Posts

Hello, I am new here and I am loving Phaser!

Having a strange issue which I'm sure has a simple solution, when I try to create multiple sprites inside a for loop, after attempting to create the first sprite, it breaks the loop and doesn't even create the sprite.

 

createCoins() {    for (var i = 0; i < 5; i++) {        var coin = this.game.add.sprite(this.game.width/2, this.game.height/2, 'coin');        coin.anchor.set(0.5, 0.5);        coin.body.velocity.y = this.scrollSpeed;        }

Could it be the nature of how I call the function creating the issue?

 

create() {    ...    maingame.prototype.createCoins();    ...}

Any help would be greatly appreciated and if I'm posting this in the incorrect area or breaking some sort of forum rule please point me in the right direction. (:

Link to comment
Share on other sites

Ack, sorry! That's what I get for not reading carefully enough:

create() {...maingame.prototype.createCoins();...}

Yes. This will cause an error. You don't want to call the prototype's version of this method, you want to call your GAME instance's version of this method. By saying "maingame.createCoins();".

 

Where is "createCoins" defined?

Link to comment
Share on other sites

  • 3 weeks later...

@drhayes I think you nailed it on the head there, I was working inside a class 'maingame' so changing to this.maingame seems to work.

@jmp909 I never checked the logs but regardless, I did need to enable physics so thank you.

Thank you all for replying, I was away from the computer and never got back to this post until now, the issue was solved in the end (: I was new to TypeScript/Phaser so simple debugging methods were unknown to me at the time D;

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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