Jump to content

change state issue


roby5
 Share

Recommended Posts

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

  • 2 weeks later...
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

 Share

  • Recently Browsing   0 members

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