Javascript Can';t在Phaser中的类内创建函数
我对javaScript不太熟悉,我开始在Phaser中做一个项目,目前只是一个带有按钮的标题屏幕,我遇到的问题是我试图在类中创建另一个函数,但我无法做到,每次编译时我都会得到一个错误,即函数没有定义,在本例中,函数Carro()确实需要一些帮助。多谢各位Javascript Can';t在Phaser中的类内创建函数,javascript,function,methods,phaser,Javascript,Function,Methods,Phaser,我对javaScript不太熟悉,我开始在Phaser中做一个项目,目前只是一个带有按钮的标题屏幕,我遇到的问题是我试图在类中创建另一个函数,但我无法做到,每次编译时我都会得到一个错误,即函数没有定义,在本例中,函数Carro()确实需要一些帮助。多谢各位 class Inicio extends Phaser.Scene { constructor(){ super({key: 'Inicio'}); } preload(){ this.load.image('Tit
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"); }
}
这是配置文件:
window.onload = function(){
var config = {
type: Phaser.AUTO,
width: 800,
height: 600,
backgroundColor: 0x000000,
scene: [Inicio, Formulario],
};
var game = new Phaser.Game(config);
}
carro
是Inicio
类的一种方法。您需要使用this
关键字来访问其类中的方法
this.scene.Carro();
例如:
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");
}
谢谢你的回答,不幸的是我已经试过了,但也没有成功,我再次尝试只是为了检查,但仍然没有成功。在这种情况下,我在控制台中得到的是“this.carro不是一个函数”,您调用函数的方式是错误的。我会给你一些例子非常感谢你,我注意到我的错误是因为我在button up事件中调用了this.carro。如果没有太多麻烦,您能告诉我在按钮启动事件中调用Carro方法的正确方法是什么吗?。再次感谢你,我真是笨透了。就是这样。场景。卡罗()。非常感谢你,这让我感到疯狂和沮丧。非常感谢。