Published
Edited
Sep 20, 2020
7 stars
Insert cell
Insert cell
Insert cell
Insert cell
class Scene extends Phaser.Scene {
preload() {
this.load.baseURL = 'https://examples.phaser.io/assets/';
this.load.crossOrigin = 'anonymous';

this.load.image('atari', 'demoscene/atari.png');
}
create() {
const { width, height } = this.sys.game.config;
this.add.sprite(width/2,height/2,"atari")

}
update() {

}
}
Insert cell
Insert cell
Insert cell
Insert cell
class dragonHunter extends Phaser.Scene {

constructor() {
super({type:"mainScene"})
this.playerSpeed = 1.5;
this.enemyMaxY = 280;
this.enemyMinY = 80;
}

preload() {
// this.load.image('background', 'assets/background.png')
// this.load.image('background', 'assets/background.png');
// this.load.image('player', 'assets/player.png');
// this.load.image('dragon', 'assets/dragon.png');
// this.load.image('treasure', 'assets/treasure.png');
}

create() {
const bg = this.add.sprite(0,0,"background")
bg.setOrigin(0,0)

this.player = this.add.sprite(40, this.sys.game.config.height / 2, 'player');
this.player.setScale(0.5);

this.treasure = this.add.sprite(this.sys.game.config.width - 80, this.sys.game.config.height / 2, 'treasure');
this.treasure.setScale(0.6);

this.enemies = this.add.group({
key: 'dragon',
repeat: 5,
setXY: {
x: 110,
y: 100,
stepX: 80,
stepY: 20
}
});
Phaser.Actions.ScaleXY(this.enemies.getChildren(), -0.5, -0.5);

Phaser.Actions.Call(this.enemies.getChildren(), function(enemy) {
enemy.speed = Math.random() * 2 + 1;
}, this);

this.isPlayerAlive = true;
this.cameras.main.resetFX();
}

update() {
if (!this.isPlayerAlive) {
return;
}

if(this.input.activePointer.isDown) {
this.player.x += this.playerSpeed
}

if (Phaser.Geom.Intersects.RectangleToRectangle(this.player.getBounds(), this.treasure.getBounds())) {
this.add.text(10,10, "Found treasure")
this.gaveOver()
}

let enemies = this.enemies.getChildren();
let numEnemies = enemies.length;
for (let i = 0; i < numEnemies; i++) {
// move enemies
enemies[i].y += enemies[i].speed;
// reverse movement if reached the edges
if (enemies[i].y >= this.enemyMaxY && enemies[i].speed > 0) {
enemies[i].speed *= -1;
} else if (enemies[i].y <= this.enemyMinY && enemies[i].speed < 0) {
enemies[i].speed *= -1;
}

if (Phaser.Geom.Intersects.RectangleToRectangle(this.player.getBounds(), enemies[i].getBounds())) {
this.gameOver();
break;
}
}
}

gameOver() {

this.isPlayerAlive = false;

// shake the camera
this.cameras.main.shake(500);

// fade camera
this.time.delayedCall(250, function() {
this.cameras.main.fade(250);
}, [], this);
// restart game
this.time.delayedCall(500, function() {
this.scene.restart();
}, [], this);
}
}
Insert cell
Insert cell
Insert cell
Insert cell
class FirstGame extends Phaser.Scene {
constructor() {
super({type:"FirstGame"})
this.score = 0
}
preload() {
this.load.baseURL = 'https://labs.phaser.io/src/games/firstgame/assets/';
this.load.crossOrigin = 'anonymous';
this.load.spritesheet('dude', 'dude.png', { frameWidth: 32, frameHeight: 48 });
this.load.image('ground', 'platform.png');
this.load.image('sky', 'sky.png');
this.load.image('star', 'star.png');
this.load.image('bomb', 'bomb.png');
}
create() {
this.add.image(400, 300, 'sky');

this.platforms = this.physics.add.staticGroup();

this.platforms.create(400, 568, 'ground').setScale(2).refreshBody();

this.platforms.create(600, 400, 'ground');
this.platforms.create(50, 250, 'ground');
this.platforms.create(750, 220, 'ground');
this.player = this.physics.add.sprite(100,450, "dude");
this.player.setBounce(0.2);
this.player.setCollideWorldBounds(true);
this.anims.create({
key: 'left',
frames: this.anims.generateFrameNumbers('dude', { start: 0, end: 3 }),
frameRate: 10,
repeat: -1
});

this.anims.create({
key: 'turn',
frames: [ { key: 'dude', frame: 4 } ],
frameRate: 20
});

this.anims.create({
key: 'right',
frames: this.anims.generateFrameNumbers('dude', { start: 5, end: 8 }),
frameRate: 10,
repeat: -1
});

this.physics.add.collider(this.player,this.platforms);
this.cursors = this.input.keyboard.createCursorKeys();
// this.player.body.setGravityY(300); // unnecessary b/c we already put it into the game config
this.stars = this.physics.add.group({
key: 'star',
repeat: 8,
setXY: { x: 12, y: 0, stepX: 70 }
});
this.stars.children.iterate(function (child) {
child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
})
this.physics.add.collider(this.stars, this.platforms);
function collectStar (player, star)
{
star.disableBody(true, true);
this.score += 10;
this.scoreText.setText("Score: " + this.score);
if(this.stars.countActive(true) === 0) {
this.stars.children.iterate(function (child) {
child.enableBody(true, child.x, 0, true, true);
});
var x = (player.x < 400) ? Phaser.Math.Between(400, 800) : Phaser.Math.Between(0, 400);

var bomb = this.bombs.create(x, 16, 'bomb');
bomb.setBounce(1);
bomb.setCollideWorldBounds(true);
bomb.setVelocity(Phaser.Math.Between(-200, 200), 20);
bomb.allowGravity = false;
}
}
this.physics.add.overlap(this.player, this.stars, collectStar, null, this);
this.scoreText = this.add.text(16, 16, "Score: " + this.score, { fontSize:'32px', fill:'#000' });
this.bombs = this.physics.add.group();
this.physics.add.collider(this.bombs, this.platforms);
function hitBomb(player, bomb) {
this.physics.pause;
player.setTint(0xff0000);
player.anims.play('turn');
this.gameOver = true;
}
this.physics.add.collider(this.player, this.bombs, hitBomb, null, this);
}
update() {
if(this.cursors.left.isDown) {
console.log("WTF");
this.player.setVelocityX(-160)
this.player.anims.play("left", true);
}
else if (this.cursors.right.isDown)
{
this.player.setVelocityX(160);
this.player.anims.play('right', true);
}
else
{
this.player.setVelocityX(0);
this.player.anims.play('turn');
}

if (this.cursors.up.isDown && this.player.body.touching.down)
{
this.player.setVelocityY(-330);
}
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more