NewGuy Posted November 5, 2015 Share Posted November 5, 2015 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 More sharing options...
jmp909 Posted November 5, 2015 Share Posted November 5, 2015 did you check your console log for errors from what i can see you haven't enabled physics on your sprite and therefore you'll be getting an error about cannot read property 'velocity' of null or something like that, since body isn't definedhttp://phaser.io/sandbox/uKPNedeP/play Link to comment Share on other sites More sharing options...
Clowerweb Posted November 5, 2015 Share Posted November 5, 2015 In addition to what jmp909 said, setting an anchor point should also be "setTo(0.5)", not "set". Link to comment Share on other sites More sharing options...
drhayes Posted November 5, 2015 Share Posted November 5, 2015 Both "set" and "setTo" work and are equivalent: https://github.com/photonstorm/phaser/blob/master/src/geom/Point.js#L97 and https://github.com/photonstorm/phaser/blob/master/src/geom/Point.js#L78. Link to comment Share on other sites More sharing options...
altorn Posted November 7, 2015 Share Posted November 7, 2015 What is the error? Is the "this" variable in the proper scope? It could be that "this" is referring to the createCoins() function instead of the game instance.Not too sure just throwing it out there. Link to comment Share on other sites More sharing options...
drhayes Posted November 9, 2015 Share Posted November 9, 2015 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? NewGuy 1 Link to comment Share on other sites More sharing options...
NewGuy Posted November 29, 2015 Author Share Posted November 29, 2015 @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 More sharing options...
Recommended Posts