Created
March 3, 2016 21:24
-
-
Save photonstorm/c4b07550ea77a6c9187d to your computer and use it in GitHub Desktop.
How to keep the same Phaser Sprite through a change in State
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var PewPew = { | |
// Our global Sprite, shared between States | |
spaceship: null | |
}; | |
PewPew.Preloader = function () {}; | |
PewPew.Preloader.prototype = { | |
preload: function () { | |
this.load.image('chunk', 'assets/sprites/chunk.png'); | |
this.load.image('arrow', 'assets/sprites/asteroids_ship.png'); | |
}, | |
create: function () { | |
PewPew.spaceship = this.make.sprite(32, 450, 'arrow'); | |
PewPew.spaceship.anchor.set(0.5); | |
this.physics.arcade.enable(PewPew.spaceship); | |
PewPew.spaceship.body.collideWorldBounds = true; | |
PewPew.spaceship.body.bounce.set(0.8); | |
this.game.state.start('State1'); | |
} | |
}; | |
PewPew.State1 = function (game) {}; | |
PewPew.State1.prototype = { | |
create: function () { | |
this.stage.backgroundColor = '#124184'; | |
// If the spaceship is already in the World we don't add it again | |
if (!PewPew.spaceship.parent) | |
{ | |
this.add.existing(PewPew.spaceship); | |
} | |
this.launch(); | |
this.input.onDown.addOnce(this.changeState, this); | |
}, | |
launch: function () { | |
if (this.input.x < PewPew.spaceship.x) | |
{ | |
PewPew.spaceship.body.velocity.setTo(-200, -200); | |
} | |
else | |
{ | |
PewPew.spaceship.body.velocity.setTo(200, -200); | |
} | |
}, | |
update: function () { | |
PewPew.spaceship.rotation = PewPew.spaceship.body.angle; | |
}, | |
changeState: function () { | |
this.game.state.start('State2', false, false); | |
} | |
}; | |
PewPew.State2 = function (game) {}; | |
PewPew.State2.prototype = { | |
create: function () { | |
this.stage.backgroundColor = '#844112'; | |
// If the spaceship is already in the World we don't add it again | |
if (!PewPew.spaceship.parent) | |
{ | |
this.add.existing(PewPew.spaceship); | |
} | |
this.input.onDown.addOnce(this.changeState, this); | |
}, | |
update: function () { | |
PewPew.spaceship.rotation = PewPew.spaceship.body.angle; | |
}, | |
changeState: function () { | |
this.game.state.start('State1', false, false); | |
} | |
}; | |
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example'); | |
game.state.add('Preloader', PewPew.Preloader); | |
game.state.add('State1', PewPew.State1); | |
game.state.add('State2', PewPew.State2); | |
game.state.start('Preloader'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi. REally nice code, I was looking for this exactly. But I got a problem. I'm trying to do the same but with p2 physics, and it looks like it doest work well. Do I need to sabe the physic as a global variable to or where should I starter? Thanks for the help