<html>
<head>
<title>Phaser mini run</title>
</head>
<body>
<script src="src/game.js"></script>
</body>
</html>
import Phaser from 'phaser'
import config from './config'
new Phaser.Game(config)
import Phaser from 'phaser'
export default {
type: Phaser.auto,
title: 'Game',
width: 800,
height: 600,
physics: {
default: 'arcade',
arcade: {
gravity: { y: 1200 }
}
},
scene: [
]
}
https://github.com/photonstorm/phaser-examples
import Phaser from 'phaser'
+import FirstScene from './scenes/FirstScene'
+
export default {
type: Phaser.auto,
title: 'Game',
width: 800,
height: 600,
physics: {
default: 'arcade',
arcade: {
gravity: { y: 1200 }
}
},
scene: [
+ FirstScene,
]
}
import Phaser from 'phaser'
import space from '../assets/space3.png'
import logo from '../assets/phaser.png'
export default class FirstScene extends Phaser.Scene {
preload() {
this.load.image('space', space);
this.load.image('logo', logo);
}
create() {
this.add.image(400, 300, 'space');
var logo = this.physics.add.image(400, 100, 'logo');
logo.setVelocity(100, 200);
logo.setBounce(1, 1);
logo.setCollideWorldBounds(true);
}
}
Used to load all the media files (assets) that will be used:
You can add a scene to preload the most common files of your game.
https://github.com/sourabhv/FlapPyBird
// Import the image and give it a js name.
import background from '../assets/background-day.png'
// Load the image into the game and give it an ID.
this.load.image('background', background)
// Add the image to the scene.
// The position coordinates are based on the center of the image.
this.add.image(144, 256, 'background');
// Import the sprite and give it a js name.
import bird from '../assets/bird.png'
// Load the sprite into the game and give it an ID.
const frameSize = { frameWidth: 34, frameHeight: 24 }
this.load.spritesheet('bird', bird, frameSize)
// Add the spri to the scene.
this.bird = this.add.sprite(144, 256, 'bird')
// Animate it.
this.anims.create({
key: 'fly',
frames: this.anims.generateFrameNumbers('bird', { start: 0, end: 3 }),
frameRate: 10,
repeat: -1
})
this.bird.anims.play('fly', true)
import Phaser from 'phaser'
import background from '../assets/background-day.png'
import logo from '../assets/logo.png'
import bird from '../assets/bird.png'
import pipe from '../assets/pipe-green.png'
export default class Preload extends Phaser.Scene {
preload() {
const bg = this.add.rectangle(144, 256, 200, 40, 0x666666)
const bar = this.add.rectangle(
bg.x, bg.y, bg.width, bg.height, 0xffffff
).setScale(0, 1)
this.load.image('background', background)
this.load.image('logo', logo)
const frameSize = { frameWidth: 34, frameHeight: 24 }
this.load.spritesheet('bird', bird, frameSize)
this.load.image('pipe', pipe)
this.load.on('progress', progress => bar.setScale(progress, 1))
}
}
import Phaser from 'phaser'
import Preload from './scenes/Preload'
export default {
type: Phaser.auto,
title: 'Game',
width: 288,
height: 512,
physics: {
default: 'arcade',
arcade: {
gravity: { y: 1200 }
}
},
scene: [
Preload,
]
}
import Phaser from 'phaser'
export default class Menu extends Phaser.Scene {
constructor() {
super({ key: 'menu' })
}
create() {
this.add.image(144, 256, 'background');
this.add.image(144, 200, 'logo');
this.bird = this.add.sprite(144, 256, 'bird')
this.anims.create({
key: 'fly',
frames: this.anims.generateFrameNumbers('bird', { start: 0, end: 3 }),
frameRate: 10,
repeat: -1
})
this.bird.anims.play('fly', true)
}
}
export default class Preload extends Phaser.Scene {
// ...
update() {
this.scene.start('menu')
}
}
import Phaser from 'phaser'
export default class Level extends Phaser.Scene {
constructor() {
super({ key: 'level' })
}
create() {
this.add.image(144, 256, 'background');
this.bird = this.physics.add.sprite(144, 256, 'bird')
this.anims.create({
key: 'fly',
frames: this.anims.generateFrameNumbers('bird',
{ start: 0, end: 3 }),
frameRate: 10,
repeat: -1
})
this.bird.anims.play('fly', true)
}
}
import Phaser from 'phaser'
export default class Menu extends Phaser.Scene {
// ...
create() {
// ...
this.input.on('pointerup', () => this.scene.start('level'))
}
}
import Phaser from 'phaser'
export default class Level extends Phaser.Scene {
// ...
update() {
const position = this.bird.body.position
if (position.y > 512) {
this.scene.start('menu')
}
}
}
import Phaser from 'phaser'
export default class Level extends Phaser.Scene {
// ...
create() {
// ...
this.bird.setCollideWorldBounds(true)
this.input.on('pointerup', () => this.fly())
this.fly()
}
fly() {
this.bird.setVelocityY(-500)
}
}
export default class Level extends Phaser.Scene {
create() {
// ...
const config = {velocityX: -100, gravityY: -1200}
this.pipes = this.physics.add.group(config)
this.createPipe()
}
// ...
createPipe() {
const topPosition = Math.random() * (160 + 120) - 120
const bottomPosition = topPosition + 440
const topPipe = this.add.image(288, topPosition, 'pipe')
topPipe.setFlipY(true)
const bottomPipe = this.add.image(288, bottomPosition, 'pipe')
this.pipes.add(topPipe)
this.pipes.add(bottomPipe)
}
}
export default class Level extends Phaser.Scene {
create() {
// ...
- this.createPipe()
+ this.this.time.delayedCall(3000, this.createPipe, [], this)
}
// ...
createPipe() {
// ...
+ this.time.delayedCall(3000, this.createPipe, [], this)
}
}
export default class Level extends Phaser.Scene {
create() {
// ...
const config = {velocityX: -100, gravityY: -1200, immovable: true}
this.pipes = this.physics.add.group(config)
this.time.delayedCall(3000, this.createPipe, [], this)
this.physics.add.collider(this.bird, this.pipes, () => this.die())
}
die() {
this.scene.start('menu')
}
// ...
}
import wingOgg from '../assets/wing.ogg'
import wingWav from '../assets/wing.wav'
export default class Preload extends Phaser.Scene {
preload() {
this.load.audio('wing', [wingOgg, wingWav])
}
}
export default class Level extends Phaser.Scene {
create() {
this.wing = this.sound.add('wing')
}
}
export default class Level extends Phaser.Scene {
fly() {
this.bird.setVelocityY(-500)
this.sound.stopAll()
this.wing.play()
}
}
Sample repository
Assets: