GEORGI DIMITROV
GEORGI DIMITROV

Reputation: 134

Phaser js: How to add player object to my scene?

I am new to Phaser js. I am trying make a game with OOP. The first thing that I want to do is to load my character into my scene but nothing happens. No exceptions were thrown. I am following this:

Phaser 3 Creating Class for player

Can somebody tell me what I am doing wrong here ?

Here is the code

var player
class MainLevelScene extends Phaser.Scene
{
    constructor() {
        super('MainLevelScene');
    }
    preload()
    {
        this.load.image('sky','assets/sky.png');
    }
    create()
    {
        this.add.image(400,200,'sky');
        player = this.physics.add.existing(new Player(this, 100, 450));
    }
    update()
    {

    }

}

class Player extends Phaser.Physics.Arcade.Sprite
{
    constructor(MainLevelScene, x, y) {
        super(MainLevelScene, x, y, 'assets/dude.png');

    }
}

Upvotes: 0

Views: 1976

Answers (1)

Davide Pedron
Davide Pedron

Reputation: 759

You first need load the asset in the preload method (like you are doing for 'sky'), and then create the sprite using the key that you have choose.

var player
class MainLevelScene extends Phaser.Scene
{
    constructor() {
        super('MainLevelScene');
    }
    preload()
    {
        this.load.image('sky','assets/sky.png');
        this.load.image('dude','assets/dude.png');
    }
    create()
    {
        this.add.image(400,200,'sky');
        player = this.physics.add.existing(new Player(this, 100, 450));
    }
    update()
    {

    }

}

class Player extends Phaser.Physics.Arcade.Sprite
{
    constructor(scene, x, y) {
        super(scene, x, y, 'dude');
    }
}

Upvotes: 2

Related Questions