I’m having trouble getting a sprite animation to work. I’m trying to play a sprite animation the same why as in the “creating your first game” tutorial … but I’m getting this error: Cannot read property ‘play’ of undefined
Any ideas?
Play.js
import Phaser from 'Phaser';
class PlayScene extends Phaser.Scene {
constructor() {
// pass a key/identifier of this scene to super
super('PlayScene');
}
preload() {
this.load.image('background', 'assets/background.png');
// load zombie sprite
this.load.spritesheet('zombie', 'assets/zombie.png', { frameWidth: 35, frameHeight: 35 });
}
create() {
// config variables
this.width = 800;
this.height = 600;
// background config
this.add.image(0, 0, 'background').setOrigin(0,0);
// zombie config
this.zombie = this.physics.add.sprite(this.width/2, this.height/2, 'zombie')
.setOrigin(0,0)
.setBounce(0)
.setCollideWorldBounds(true)
.body.setGravityY(300)
// zombie animations
this.anims.create({
key: 'left',
frames: this.anims.generateFrameNumbers('zombie', { start: 0, end: 1 }),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'turn',
frames: [ { key: 'zombie', frame: 2 } ],
frameRate: 20
});
this.anims.create({
key: 'right',
frames: this.anims.generateFrameNumbers('zombie', { start: 3, end: 4 }),
frameRate: 10,
repeat: -1
});
// enable keyboard listeners
this.cursors = this.input.keyboard.createCursorKeys();
}
update() {
var cursors = this.cursors;
var zombie = this.zombie;
var canJump = zombie.velocity.y == 0;
var left = cursors.left.isDown;
var right = cursors.right.isDown;
var zombieAnimation = zombie.anims;
if (left) {
zombie.setVelocityX(-160);
zombieAnimation.play('left', true);
}
else if (right) {
zombie.setVelocityX(160);
zombieAnimation.play('right', true);
}
else {
zombie.setVelocityX(0);
zombieAnimation.play('turn');
}
if (cursors.up.isDown && canJump) {
zombie.setVelocityY(-530);
}
}
}
export default PlayScene;
index.js
import Phaser from "phaser";
import PlayScene from "./scenes/Play";
const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
physics: {
default: 'arcade',
arcade: {
gravity: { y: 200 }
}
},
scene: [PlayScene]
};
new Phaser.Game(config)