我正在制作一个简单的Vue游戏应用程序,其中有按钮可以在点击时以范围内的随机数字减少玩家或“恶魔”的健康值。在我的Vue实例中有一个名为
attack
的方法来计算伤害并检查游戏是否结束(如果结果导致玩家或恶魔的健康值降至/低于零)。当前,如果健康值达到或低于零,则调用JS内置的确认函数来宣布玩家赢得或输掉比赛,并询问他们是否想开始新游戏。然而,我想首先检查玩家或恶魔的健康状况是否小于等于0,如果是,则将其设置为0,以便在“健康条”中反映出来,因为现在它显示的是在健康值降至/低于0之前的值。 checkWin: function() {
if (this.demonHealth <= 0) {
// this.demonHealth = 0; <-- applied after confirm();
if (confirm('You won! New game?')) {
this.startGame();
} else {
this.gameIsRunning = false;
}
return true;
} else {
// this.playerHealth = 0; <-- applied after confirm();
if (this.playerHealth <= 0) {
if (confirm('You lost. New game?')) {
this.startGame();
} else {
this.gameIsRunning = false;
}
return true;
}
}
}
完整代码: https://codepen.io/nataliecardot/pen/XWrMedm
下面是完整的Vue实例:
new Vue({
el: '#app',
data: {
playerHealth: 100,
demonHealth: 100,
gameIsRunning: false
},
methods: {
startGame: function() {
this.gameIsRunning = true;
this.playerHealth = 100,
this.demonHealth = 100
},
attack: function() {
this.demonHealth -= this.calculateDamage(3, 10);
if (this.checkWin()) {
return;
}
this.playerHealth -= this.calculateDamage(5, 12);
this.checkWin();
},
calculateDamage: function(min, max) {
return Math.max(Math.floor(Math.random() * max) + 1, min);
},
checkWin: function() {
if (this.demonHealth <= 0) {
// this.demonHealth = 0; <-- applied after confirm();
if (confirm('You won! New game?')) {
this.startGame();
} else {
this.gameIsRunning = false;
}
return true;
} else {
// this.playerHealth = 0; <-- applied after confirm();
if (this.playerHealth <= 0) {
if (confirm('You lost. New game?')) {
this.startGame();
} else {
this.gameIsRunning = false;
}
return true;
}
}
}
}
});
我也尝试了删除checkWin()函数,添加一个新的方法检查得分是否<=0,如果是则将其设置为零,并将原本在checkWin()中的代码放入回调函数中。这导致零被应用,但confirm()没有被调用,游戏仍在进行:
methods: {
startGame: function() {
this.gameIsRunning = true;
this.playerHealth = 100,
this.demonHealth = 100
},
scoreCheck: function() {
if (this.demonHealth < 0) {
this.demonHealth = 0;
}
if (this.playerHealth < 0) {
this.playerHealth = 0;
}
},
attack: function() {
this.demonHealth -= this.calculateDamage(3, 10);
this.scoreCheck(() => {
if (this.demonHealth === 0) {
if (confirm('You won! New game?')) {
this.startGame();
} else {
this.gameIsRunning = false;
}
return;
}
});
this.playerHealth -= this.calculateDamage(5, 12);
this.scoreCheck(() => {
if (this.playerHealth === 0) {
if (confirm('You lost. New game?')) {
this.startGame();
} else {
this.gameIsRunning = false;
}
}
});
},
calculateDamage: function(min, max) {
return Math.max(Math.floor(Math.random() * max) + 1, min);
},
}
编辑:我也尝试在原始代码中使用$nextTick(),但结果与之前相同:
checkWin: function() {
if (this.demonHealth <= 0) {
this.demonHealth = 0;
this.$nextTick(function() {
if (confirm('You won! New game?')) {
this.startGame();
} else {
this.gameIsRunning = false;
}
return true;
});
} else {
if (this.playerHealth <= 0) {
this.playerHealth = 0;
this.$nextTick(function() {
if (confirm('You lost. New game?')) {
this.startGame();
} else {
this.gameIsRunning = false;
}
return true;
});
}
}
}
$nextTick
回调中执行confirm
,例如this.$nextTick(() => { if (confirm(...)) { ... } })
。请参阅 https://vuejs.org/v2/api/#vm-nextTick。 - Phil