Felipe Cabrera
Felipe Cabrera

Reputation: 1

Can't create functions inside a class in Phaser

I'm not familiarized with javaScript too much, and I'm starting to do a project in Phaser, at the moment is just a title screen with a button, the issue i'm having is that I'm trying to create another function inside the class but I'm unable to do it, every time I compile I get the error that the function is not defined, in this case the function Carro(), I would really use some help. Thank you.

class Inicio extends Phaser.Scene {

constructor(){

    super({key: 'Inicio'});

}

preload(){

    this.load.image('Titulo', 'Assets/Imagenes/LogoInicio.png');
    this.load.spritesheet('Empezar', 'Assets/Imagenes/EmpezarSprites.png', {frameWidth: 393, frameHeight: 117});
    this.load.audio('Dinero', 'Assets/Audio/Dinero.mp3'); }

create(){

    this.add.image(400, 300, 'Titulo');

    var botonEmpezar = this.add.sprite(400, 500, 'Empezar');
    var sonidoDinero = this.sound.add('Dinero');
    var timerCambio;

    botonEmpezar.setInteractive(/*{useHandCursor: true}*/);

    botonEmpezar.on('pointerover', function(){
        this.setFrame(1);
    });

    botonEmpezar.on('pointerout', function(){
        this.setFrame(0);
    });

    botonEmpezar.on('pointerdown', function(){
        this.setFrame(2);
        sonidoDinero.play();
    });

    botonEmpezar.on('pointerup', function(){
        this.setFrame(0);
        //timerCambio = this.scene.time.delayedCall(2000, onEvent(), [], this);
        this.scene.scene.start("Formulario");
    });
        Carro();    
}

Carro(){
    console.log("entro"); }

}

This is the config file:

window.onload = function(){

    var config = {
        type: Phaser.AUTO,
        width: 800,
        height: 600,
        backgroundColor: 0x000000,
        scene: [Inicio, Formulario],
    };

    var game = new Phaser.Game(config);

}

Upvotes: 0

Views: 625

Answers (1)

Ehsan Nazeri
Ehsan Nazeri

Reputation: 801

the carro is a method of Inicio class. you need to use this keyword to access the method in its class.

this.scene.Carro(); 

Example :

class test Extends Phaser.Scene {
    create(){
      Carro(); //ERROR
      this.Carro();// OK
      func2(); //OK
    }
    Carro(){
       console.log("Method of test class");
    }
}


 function func2 {
   console.log("function");
}

Upvotes: 1

Related Questions