with
@nicodev_23 @dwaksman
so hold tight and...set phasers to stun!
Phaser.Game(width, height, renderer, htmlElt)
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'gameDiv');
Code:
Game.state.add(stateName, stateObject);
// Create a new game instance.
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'gameDiv');
// Add some states to the game.
game.state.add('menu', MenuState);
game.state.add('play', PlayState);
// Initialize the game in the 'menu' state.
game.state.start('menu');
Code:
// Create a PlayState class.
var PlayState = function () {};
// Create the preload, create and update methods.
PlayState.prototype = {
preload: function () {
// Called while loading the state.
},
create: function () {
// Called once after loading the state.
},
update: function () {
// Called 60 times per second.
}
};
Game.load.image(stateName, stateObject);
Game.load.audio(stateName, stateObject);
// Create the preload, create and update methods.
PlayState.prototype = {
preload: function () {
this.game.load.image('player', 'assets/player.png');
this.game.load.audio('music', 'assets/music.ogg');:
},
create: function () { ... },
update: function () { ... }
};
Code:
Game.add.sprite(width, height, assetName);
// Create the preload, create and update methods.
PlayState.prototype = {
preload: function () {
this.game.load.image('player', 'assets/player.png');
this.game.load.audio('music', 'assets/music.ogg');:
},
create: function () {
// Add the player sprite at (50, 50).
this.game.add.sprite(50, 50, 'player');
},
update: function () { ... }
};
Code:
// Create the preload, create and update methods.
PlayState.prototype = {
preload: function () {
this.game.load.image('player', 'assets/player.png');
// Enable physics.
this.game.physics.startSystem(Phaser.Physics.ARCADE);
},
create: function () {
this.player = this.game.add.sprite(50, 50, 'player');
// Add the player to the physics system and add gravity.
this.game.physics.arcade.enable(this.player);
this.player.body.gravity.y = 1000;
},
update: function () {
var UpArrow = this.game.input.keyboard.addKey(Phaser.Keyboard.UP);
if (UpArrow.isDown())
this.player.body.velocity.y = -300;
}
};
// Create the preload, create and update methods.
PlayState.prototype = {
preload: function () {
this.game.load.audio('music', 'assets/music.ogg');
},
create: function () {
// Add some music to the game.
this.music = this.game.add.audio('music');
this.music.loop = true;
// Play the music.
this.music.play();
},
update: function () { ... }
};
// Create the preload, create and update methods.
PlayState.prototype = {
preload: function () { ... },
create: function () {
// Create a player and wall.
this.player = this.game.add.sprite(50, 50, 'player');
this.wall = this.game.add.sprite(150, 50, 'player');
// Make the player move to the right (in order to colide with the wall).
this.player.body.velocity.x = 50;
// Enable physics for both and blabla...
},
update: function () {
// Make them collide.
this.game.physics.arcade.collide(this.player, this.wall);
}
};