I am currently working on a base class for which all mobs and players will be inherited from. I have my base class below, followed by the Bot and Player classes, with finally the objects being called in the WorldScene class. Everything so far about this works great, except the sprites are the same on both objects, even though I have provided different ID’s for each in their new object declarations. I swapped the order I call the Player/Bot in and when I do that, both sprites are of the blue slime that I only want my bot to have. If I am declaring an entirely new object (in this case, the bot), why are the values somehow carrying over / being overwritten on the second object? I just want my player to have whatever sprite I have defined in the parameters, and for my bot to have the same functionality. I feel like I am missing something about inheritance here. Can anyone help?
class Character extends Phaser.Physics.Arcade.Sprite {
constructor (scene, x, y, sprite, frame, canMove = true)
{
/********************************************
* Pass parameters the class we are extending
* using super()
*
* This tells the physics engine to create
* a physics sprite for us
********************************************/
super (scene, x, y, sprite, frame);
/********************************************
* The below code must be done to properly get
* the sprite to display and have physics
********************************************/
scene.sys.displayList.add(this);
scene.sys.updateList.add(this);
scene.sys.arcadePhysics.world.enableBody(this, 0);
/********************************************
* this._canMove holds TRUE or FALSE
* depending on if the character can move
* or not.
********************************************/
this._canMove = canMove;
}
/********************************************
* Getter for this._canMove
* Returns the value of this._canMove
********************************************/
get canMove()
{
return this._canMove;
}
/********************************************
* Setter for this._canMove
* Sets the value of this._canMove
********************************************/
set canMove(bool)
{
this._canMove = bool;
}
}
class Bot extends Character {
constructor (scene, x, y, sprite, frame)
{
super(scene, x, y, sprite, frame);
this.moveLeft = false;
this.moveRight = false;
this.moveUp = false;
this.moveDown = false;
scene.physics.add.collider(this, scene.obstacles);
/********************************************
* The below code is for bot animations
* in all 4 directions (up, down, left, right)
********************************************/
scene.anims.create({
key: 'left',
frames: scene.anims.generateFrameNumbers(sprite, {
frames: [1, 2, 3, 4]
}),
frameRate: 10,
repeat: -1
});
scene.anims.create({
key: 'right',
frames: scene.anims.generateFrameNumbers(sprite, {
frames: [1, 2, 3, 4]
}),
frameRate: 10,
repeat: -1
});
scene.anims.create({
key: 'up',
frames: scene.anims.generateFrameNumbers(sprite, {
frames: [1, 2, 3, 4]
}),
frameRate: 10,
repeat: -1
});
scene.anims.create({
key: 'down',
frames: scene.anims.generateFrameNumbers(sprite, {
frames: [ 1, 2, 3, 4 ]
}),
frameRate: 10,
repeat: -1
});
}
patrolPath ()
{
if (this.x <= 1945) // starting point for bot, start moving right
{
this.moveLeft = false;
this.moveRight = true;
}
if (this.x >= 2500) // maximum distance to go right, time to turn around
{
this.moveRight = false;
this.moveLeft = true;
}
}
update ()
{
this.patrolPath();
this.setVelocity(0);
// update player horizontal location on map/screen based on input
if (super.canMove == true)
{
if (this.moveLeft == true)
{
this.setVelocityX(-25);
this.anims.play('left', true);
this.flipX = true; // flip animations to look like the player is moving left
}
else if (this.moveRight == true)
{
this.setVelocityX(25);
this.anims.play('right', true);
//if the player object is already flipped, we flip it back
if (this.flipX == true) {
this.flipX = false;
}
}
// update player veritical location on map/screen based on input
if (this.moveUp == true)
{
this.setVelocityY(-25);
this.anims.play('up', true);
}
else if (this.moveDown == true)
{
this.setVelocityY(25);
this.anims.play('down', true);
}
}
if ((super.velocityX == 0 && super.velocityY == 0) || (this.moveLeft == false && this.moveRight == false && this.moveUp == false && this.moveDown == false))
{
this.anims.stop();
}
}
}
//code from WorldScene.js
this.player = new Player(this, 2645, 1655, 'player', 6);
this.bot = new Bot(this, 1945, 805, 'slime-blue', 0);
this.bot should have a blue slime for it’s sprite, but it has the ‘player’ sprite.