Uncaught TypeError: Cannot read property 'circle' in Phaser JS Project. Cant use functions
Uncaught TypeError: Cannot read property 'circle' in Phaser JS Project. Cant use functions
这里是编码初学者。
我想编写一个小游戏。作为测试,我想让圆圈每秒随机出现在屏幕上。如果您单击它们,它们就会消失。
对于屏幕上的外观,我想使用一秒间隔。为此,我创建了一个游戏功能。但在这个功能中,我无法使用 Phaser 特定的功能。我无法创建文本或形状。
我的问题是:这是为什么?我不明白什么?我该如何解决呢?
感谢您的帮助。
来自德国的问候
(抱歉打字和语法错误)
主游戏场景
constructor(){
super({ key: 'GameScene' })
}
preload(){
}
//---------------------------------------------------------------------------------------------------------------------
create(){
const startingMinutes = 1;
let time = startingMinutes * 60; //Zeit in Sekunden
//Test Here i can still use Phaser funktions
gameState.texttest = this.add.text(150, 250, 'This is working', { fill: '#000000', fontSize: '20px' });
setInterval(updatecountdown, 1000);
function updatecountdown(){
//Countdown
if(time > 0){
//console.log("Time: " + time);
const minutes = Math.floor(time / 60); //Math floor rundet
let seconds = time % 60;
console.log("minuten: " + minutes);
console.log("sekunden: " + seconds);
time--;
}else{
gameState.gameOn = false;
}
}
setInterval(gameplay, 1000);
function gameplay(){
if(gameState.gameOn === true){
let Xvar = Math.floor(Math.random() * 480);
console.log(Xvar);
let Yvar = Math.floor(Math.floor(Math.random()*380));
console.log(Yvar);
gameState.circle1 = this.add.circle(Xvar, Yvar, 20, 0x98fb98 );
gameState.circle1.setInteractive();
gameState.circle1.on('pointerup', function(){
gameState.circle1.destroy();
})
}else{
gameState.endtext = this.add.text(150, 250, 'The End', {fill: '#000000', fontSize: '20px'});
}
}
}
//---------------------------------------------------------------------------------------------------------------------------
update(){
}
}
配置
};
const config = {
type: Phaser.AUTO,
width: 500,
height: 400,
backgroundColor: "#b0e0e6",
scene: [StartScene, GameScene, EndScene]
};
gameState.gameOn = true;
const game = new Phaser.Game(config);
``
上下文丢失。在 javascript 回调中,我们得到 this
当前调用上下文。
使用 setInterval 时,setInterval 中的函数用作 this
而不是 Phaser.Scene
尝试使用 setInterval 这样:
setInterval(gameplay.bind(this), 1000);
这里是编码初学者。 我想编写一个小游戏。作为测试,我想让圆圈每秒随机出现在屏幕上。如果您单击它们,它们就会消失。
对于屏幕上的外观,我想使用一秒间隔。为此,我创建了一个游戏功能。但在这个功能中,我无法使用 Phaser 特定的功能。我无法创建文本或形状。
我的问题是:这是为什么?我不明白什么?我该如何解决呢?
感谢您的帮助。
来自德国的问候 (抱歉打字和语法错误)
主游戏场景
constructor(){
super({ key: 'GameScene' })
}
preload(){
}
//---------------------------------------------------------------------------------------------------------------------
create(){
const startingMinutes = 1;
let time = startingMinutes * 60; //Zeit in Sekunden
//Test Here i can still use Phaser funktions
gameState.texttest = this.add.text(150, 250, 'This is working', { fill: '#000000', fontSize: '20px' });
setInterval(updatecountdown, 1000);
function updatecountdown(){
//Countdown
if(time > 0){
//console.log("Time: " + time);
const minutes = Math.floor(time / 60); //Math floor rundet
let seconds = time % 60;
console.log("minuten: " + minutes);
console.log("sekunden: " + seconds);
time--;
}else{
gameState.gameOn = false;
}
}
setInterval(gameplay, 1000);
function gameplay(){
if(gameState.gameOn === true){
let Xvar = Math.floor(Math.random() * 480);
console.log(Xvar);
let Yvar = Math.floor(Math.floor(Math.random()*380));
console.log(Yvar);
gameState.circle1 = this.add.circle(Xvar, Yvar, 20, 0x98fb98 );
gameState.circle1.setInteractive();
gameState.circle1.on('pointerup', function(){
gameState.circle1.destroy();
})
}else{
gameState.endtext = this.add.text(150, 250, 'The End', {fill: '#000000', fontSize: '20px'});
}
}
}
//---------------------------------------------------------------------------------------------------------------------------
update(){
}
}
配置
};
const config = {
type: Phaser.AUTO,
width: 500,
height: 400,
backgroundColor: "#b0e0e6",
scene: [StartScene, GameScene, EndScene]
};
gameState.gameOn = true;
const game = new Phaser.Game(config);
``
上下文丢失。在 javascript 回调中,我们得到 this
当前调用上下文。
使用 setInterval 时,setInterval 中的函数用作 this
而不是 Phaser.Scene
尝试使用 setInterval 这样:
setInterval(gameplay.bind(this), 1000);