Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

100
Visualizações
Phaser 3 extend create function

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.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório

0

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();}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda