How do I extend phaser 3 create function in an extended scene without overwriting the original create function?
class Level2 extends GameScene {
constructor() {
super('Level2')
this.heights = [5, 4, null, 4, 6, 4, 6, 5, 5];
// Add Level2 weather here
this.weather = 'twilight';
this.skyColor = 'sunset';
this.music = 'bass';
this.platform = 'platform2'
this.large = 'bg31';
this.medium = 'bg21';
}
create() {
console.log("HELLO CRETE FROM LEVLE 2")
gameState.bat = this.physics.add.group({
allowGravity: false
});
this.time.addEvent({
callback: this.batGenLoop,
delay: 5000,
callbackScope: this,
loop: true,
});
gameState.bat.move = this.tweens.add({
targets: gameState.bat.getChildren(),
//.map(function (c) { return c.body.velocity })
x: 0,
ease: 'Linear',
duration: 7000,
repeat: 0,
yoyo: false,
});
}//create
batGenLoop() {
console.log('batman')
const yCoord = Math.random() * 600;
gameState.bat.create(1890, yCoord, 'bat');
//.setScale(3).body.setSize(140, 320, true).setOffset(100, 20)
}
}
this is my attempt but it overwrites the original create function in gameScene. Level 2 is extended from gameScene.
If you don't want to override the create function, I see two easy options:
don't write a create in the new Scene, than when you call the function, the function of the base class is used.
or call the function of the base class, in the new function, like this:
class Level2 extends GameScene {
...
create(){
super.create(); // calling the function of the base class
...
}
}
With the second option, you can write some code after calling super.create(), to adapt the functionality.
Problem solved. In order to have different things in different scenes, instead of writing(accidentally overwritting) create(),I defined functions in the extend function. Then, in the original gameScene which all other level scenes extend, I wrote some if statements. If these functions are defined in this level, they will run.
class Level2 extends GameScene {
...
newFunction(){
//things I want for this extended scene
}
}
Then in gameScene:
if (this.newFunction) {this.newFunction();}