roby5 Posted August 16, 2016 Share Posted August 16, 2016 when i switch from menu state to game state and return at menu, the animation of star field that i have created is stopped. i post my code here: create: function(){ //create a 100 stars in random position this.sprites = this.game.add.spriteBatch(); for (var i = 0; i < this.max; i++) { this.xx[i] = Math.floor(Math.random() * this.game.width); this.yy[i] = Math.floor(Math.random() * this.game.height); var star=this.game.make.sprite(this.xx[i], this.yy[i], 'star'); this.sprites.addChild(star); this.stars.push(star); } // [....] other code }, update: function() { // shift the star at left for (var i = 0; i < this.max; i++) { this.xx[i] -= this.speed; if (this.xx[i] < 0) //return at right and give a new random y { this.xx[i] = this.game.width; this.yy[i] = Math.floor(Math.random() * this.game.height); } this.stars[i].x=this.xx[i]; this.stars[i].y=this.yy[i];//apply a new coords } } }; function play(obj) { obj.game.state.start('Game'); } Link to comment Share on other sites More sharing options...
TickleMeElmo Posted August 16, 2016 Share Posted August 16, 2016 will need to see the full code or more detailed explanation, don't see any reason why this shouldn't work(unless you left this.speed undefined), also out of curiosity, why use this.xx and this.yy? Link to comment Share on other sites More sharing options...
roby5 Posted August 27, 2016 Author Share Posted August 27, 2016 On 16/8/2016 at 8:45 PM, TickleMeElmo said: will need to see the full code or more detailed explanation, don't see any reason why this shouldn't work(unless you left this.speed undefined), also out of curiosity, why use this.xx and this.yy? i make 100 star on random position and save her position on this.xx and this.yy every update, i shift the star to left, e need a xx position for do it. if xx is <0 i make another random yy for star and i put back the star. when i run the code it work, but when i change state and return in menu state it not work Link to comment Share on other sites More sharing options...
symof Posted August 27, 2016 Share Posted August 27, 2016 Your general use of this. is messed up, and the scope of your variables is all over the place. Your issue is from a global variable that gets(or doesn't) reset at a wrong point. Good luck debugging. Link to comment Share on other sites More sharing options...
Recommended Posts