Javascript Phaser 3 sprite keyboard.isDown动作奇怪
我正在尝试在Phaser 3中创建一个蛇游戏。箭。(钥匙)。我的行为很奇怪,我不明白为什么。如果你按下一个键,不管它是向上、向下、向左还是向右,它都不会改变方向。如果你按下相反的键,它只会继续移动。如果我按下按钮,即使我按下按钮,它也会继续下降。我已经试着调试它好几个小时了。所有的例子都是以同样的方式设置的 这是到目前为止我的代码Javascript Phaser 3 sprite keyboard.isDown动作奇怪,javascript,phaser-framework,Javascript,Phaser Framework,我正在尝试在Phaser 3中创建一个蛇游戏。箭。(钥匙)。我的行为很奇怪,我不明白为什么。如果你按下一个键,不管它是向上、向下、向左还是向右,它都不会改变方向。如果你按下相反的键,它只会继续移动。如果我按下按钮,即使我按下按钮,它也会继续下降。我已经试着调试它好几个小时了。所有的例子都是以同样的方式设置的 这是到目前为止我的代码 class mainScene { preload() { this.load.image("fruit", "images
class mainScene {
preload() {
this.load.image("fruit", "images/fruit.png");
this.load.image("snake", "images/snake.png");
}
create() {
this.score = 0;
this.snake = this.physics.add.sprite(20, 20, "snake");
this.fruit = this.physics.add.sprite(
Phaser.Math.Between(10, 590),
Phaser.Math.Between(10, 590),
"fruit"
);
this.scoreText = this.add.text(500, 5, `Score: ${this.score}`);
this.arrow = this.input.keyboard.createCursorKeys();
//debug
}
update() {
if (this.physics.overlap(this.snake, this.fruit)) {
this.hit();
}
this.snake.setVelocity(0);
if (this.arrow.right.isDown) {
this.snake.setVelocityX(50);
if (this.snake.x > 600) {
this.outOfBounds();
}
} else if (this.arrow.left.isDown) {
this.snake.setVelocityX(-50);
if (this.snake.x < 0) {
this.outOfBounds();
}
}
if (this.arrow.down.isDown) {
this.snake.setVelocityY(50);
if (this.snake.y > 600) {
this.outOfBounds();
}
} else if (this.arrow.up.isDown) {
this.snake.setVelocityY(-50);
if (this.snake.y < 0) {
this.outOfBounds();
}
}
}
hit() {
this.fruit.x = Phaser.Math.Between(10, 590);
this.fruit.y = Phaser.Math.Between(10, 590);
this.score += 1;
this.scoreText.setText(`Score: ${this.score}`);
this.tweens.add({
targets: this.snake,
duration: 200,
scaleX: 1.2,
scaleY: 1.2,
yoyo: true,
});
}
outOfBounds() {
this.score = 0;
this.scoreText.setText(`Score: ${this.score}`);
this.gameOverText = this.add.text(
100,
250,
"You went out of bounds.\nGame Over\nPress Space to continue.",
{ fontSize: "24px" }
);
if (this.arrow.space.isDown) {
this.gameOverText.destroy();
this.scene.restart();
}
}
}
const config = {
type: Phaser.CANVAS,
width: 600,
height: 600,
backgroundColor: 0x000040,
scene: mainScene,
physics: {
default: "arcade",
debug: true,
setBounds: { x: 0, y: 0, width: 600, height: 600 },
},
parent: "game",
};
new Phaser.Game(config);
class主场景{
预载{
this.load.image(“fruit”,“images/fruit.png”);
this.load.image(“snake”,“images/snake.png”);
}
创建(){
这个分数=0;
this.snake=this.physics.add.sprite(20,20,“snake”);
this.fruit=this.physics.add.sprite(
相位器数学介于(10590)之间,
相位器数学介于(10590)之间,
“水果”
);
this.scoreText=this.add.text(500,5,`Score:${this.Score}`);
this.arrow=this.input.keyboard.CreateCursorWorkeys();
//调试
}
更新(){
if(this.physics.overlap(this.snake,this.fruit)){
这个.hit();
}
这个.snake.setVelocity(0);
如果(此箭头右箭头为isDown){
这个.snake.setVelocityX(50);
如果(此.snake.x>600){
这是。超出边界();
}
}else if(此.arrow.left.isDown){
this.snake.setVelocityX(-50);
if(this.snake.x<0){
这是。超出边界();
}
}
if(本箭头下箭头为isDown){
本.snake.setVelocityY(50);
如果(此.snake.y>600){
这是。超出边界();
}
}else if(此.arrow.up.isDown){
this.snake.setVelocityY(-50);
if(this.snake.y<0){
这是。超出边界();
}
}
}
hit(){
this.fruit.x=Phaser.Math.Between(10590);
this.fruit.y=Phaser.Math.Between(10590);
这是一个分数+=1;
this.scoreText.setText(`Score:${this.Score}`);
这是我的({
目标:这条蛇,
持续时间:200,
scaleX:1.2,
斯卡利:1.2,
yoyo:没错,
});
}
边界外(){
这个分数=0;
this.scoreText.setText(`Score:${this.Score}`);
this.gameOverText=this.add.text(
100,
250,
“您出界了。\n请暂停\n按空格继续。”,
{fontSize:“24px”}
);
if(this.arrow.space.isDown){
this.gameOverText.destroy();
this.scene.restart();
}
}
}
常量配置={
类型:Phaser.CANVAS,
宽度:600,
身高:600,
背景颜色:0x000040,
场景:主场景,
物理学:{
默认值:“拱廊”,
是的,
立根:{x:0,y:0,宽:600,高:600},
},
家长:“游戏”,
};
新的Phaser.Game(配置);
在几个不同的浏览器上查看后,这似乎是Brave浏览器上的问题,这很奇怪,因为它是chrome浏览器,并且在chrome上工作。在几个不同的浏览器上查看后,这似乎是Brave浏览器上的问题,这很奇怪,因为它是chrome浏览器,它可以在铬上工作。