gpt4 book ai didi

javascript - 如何限制else语句? - Javascript 游戏

转载 作者:行者123 更新时间:2023-11-29 20:59:30 25 4
gpt4 key购买 nike

我正在寻找有关如何正确实现您站立的“按钮”(正方形)的解释,这将改变背景颜色。我已经成功实现了 if 语句,它改变了背景,但是当我添加一个 else 语句将它改回正常时,当你不在按钮上时,它会重复运行而不会停止。

我遇到的另一个障碍物也是如此,它会让你站在上面时移动速度变慢。当您不在障碍物上时,它会不断更改变量,而不是一次将其更改回来。

var gravity = 30;
var jumpSpeed = 17;
var playerYSpeed = 7;

Player.prototype.moveY = function(step, level, keys, pos) {
this.speed.y += step * gravity;
var motion = new Vector(0, this.speed.y * step);
var newPos = this.pos.plus(motion);
var obstacle = level.obstacleAt(newPos, this.size);
var lastObstacle = null;

if (obstacle) {
if (keys.up && this.speed.y > 0)
this.speed.y = -jumpSpeed;
else
this.speed.y = 0;
} else {
this.pos = newPos;
}
if (obstacle === "lava") {
this.pos = new Vector(6.5, 18.5);
}
if (obstacle === "bouncer") {
this.pos = newPos + 20;
console.log('bounce');
}
else {
gravity = 30;
}
if (obstacle === "ice") {
playerXSpeed = 0.5;
console.log('slow speed');
}
else if (obstacle !== lastObstacle){
playerXSpeed = 7;
console.log('norm speed');
}
var changeBackground = document.getElementsByClassName('background');
if (obstacle === "blind") {
changeBackground[0].style.background = 'white';
console.log('purple');
}
else if (obstacle !== lastObstacle){
changeBackground[0].style.background = 'blue';
console.log('blue');
}
};

“盲”障碍应该只有当你站在上面时才会改变背景,当你离开时它应该恢复正常。冰也一样。

那么您如何使它更像是“打开”-“关闭”场景?

最佳答案

如果我正确理解您的代码,它在某个循环函数中。

您可以添加一个变量“lastObstacle”并检查当前障碍物是否不同。 (else if (lastObstacle !== obstacle)

var gravity = 30;
var jumpSpeed = 17;
var playerYSpeed = 7;
var lastObstacle = null;

Player.prototype.moveY = function(step, level, keys, pos) {
this.speed.y += step * gravity;
var motion = new Vector(0, this.speed.y * step);
var newPos = this.pos.plus(motion);
var obstacle = level.obstacleAt(newPos, this.size);

if (obstacle) {
if (keys.up && this.speed.y > 0)
this.speed.y = -jumpSpeed;
else
this.speed.y = 0;
} else {
this.pos = newPos;
}
if (obstacle === "lava") {
this.pos = new Vector(6.5, 18.5);
}
if (obstacle === "bouncer") {
this.pos = newPos + 20;
console.log('bounce');
}
else {
gravity = 30;
}
if (obstacle === "ice") {
playerXSpeed = 0.5;
console.log('slow speed');
}
else if (obstacle !== lastObstacle){
playerXSpeed = 7;
console.log('norm speed');
}
var changeBackground = document.getElementsByClassName('background');
if (obstacle === "blind") {
changeBackground[0].style.background = 'white';
console.log('purple');
}
else if (obstacle !== lastObstacle){
changeBackground[0].style.background = 'blue';
console.log('blue');
}
lastObstacle = obstacle;
};

(评论:片段可能不起作用。)

PS:我犯了一个小错误,我现在更正了:

var gravity = 30;
var jumpSpeed = 17;
var playerYSpeed = 7;
var lastObstacle = null;

Player.prototype.moveY = function(step, level, keys, pos) {
this.speed.y += step * gravity;
var motion = new Vector(0, this.speed.y * step);
var newPos = this.pos.plus(motion);
var obstacle = level.obstacleAt(newPos, this.size);

if (obstacle) {
if (keys.up && this.speed.y > 0)
this.speed.y = -jumpSpeed;
else
this.speed.y = 0;
} else {
this.pos = newPos;
}
if (obstacle === "lava") {
this.pos = new Vector(6.5, 18.5);
}
if (obstacle === "bouncer") {
this.pos = newPos + 20;
console.log('bounce');
}
else {
gravity = 30;
}
if (obstacle === "ice") {
playerXSpeed = 0.5;
console.log('slow speed');
}
else if (lastObstacle === "ice"){
playerXSpeed = 7;
console.log('norm speed');
}
var changeBackground = document.getElementsByClassName('background');
if (obstacle === "blind") {
changeBackground[0].style.background = 'white';
console.log('purple');
}
else if (lastObstacle === "blind"){
changeBackground[0].style.background = 'blue';
console.log('blue');
}
lastObstacle = obstacle;
};

我更改了 lastObstacle !== obstacle 的两次出现。

编辑 2:它是这样工作的:只有当你现在不在冰 block /盲 block 上并且你之前在冰 block /盲 block 上时,它才会执行 else-if block 。

关于javascript - 如何限制else语句? - Javascript 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47316114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com