Hello I am trying to build phaser using another approach and I got an error. can you check it out for me please.
import 'phaser';
class MyGame extends Phaser.Scene
{
constructor ()
{
super();
}
preload () {
this.load.image('sky', require('./assets/sky.png'));
this.load.image('ground', require('./assets/platform.png'));
this.load.image('star', require('./assets/star.png'));
this.load.image('bomb', require('./assets/bomb.png'));
this.load.spritesheet('dude',
require('./assets/dude.png'),
{ frameWidth: 32, frameHeight: 48 });
}
create ()
{
this.add.image(400, 300, 'sky');
const platforms = this.physics.add.staticGroup();
platforms.create(400, 568, 'ground').setScale(2).refreshBody();
platforms.create(600, 400, 'ground');
platforms.create(50, 250, 'ground');
platforms.create(750, 220, 'ground');
const player = this.physics.add.sprite(100, 450, 'dude');
player.setBounce(0.2);
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
});
player.body.setGravityY(300);
this.cursors = this.input.keyboard.createCursorKeys();
this.physics.add.collider(player, platforms);
}
update () {
var cursors = this.cursors;
var player = this.player;
if (cursors.left.isDown)
{
player.setVelocityX(-160);
player.anims.play('left', true);
}
else if (cursors.right.isDown)
{
player.setVelocityX(160);
player.anims.play('right', true);
}
else
{
player.setVelocityX(0);
player.anims.play('turn');
}
if (cursors.up.isDown && player.body.touching.down)
{
player.setVelocityY(-330);
}
}
}
const config = {
type: Phaser.AUTO,
parent: 'phaser-example',
width: 800,
height: 600,
physics: {
default: 'arcade',
arcade: {
gravity: { y: 300 },
debug: false
}
},
scene: MyGame
};
const game = new Phaser.Game(config);
I get an error that body and setVelocityX() are undefined.