我這裏得到這個代碼:如果條件沒有按預期工作
var player = {
ATK: 30
}
function start(place) {
\t if (place == 'goldMine') {
\t \t canvas = document.getElementById('canvas');
\t \t ctx = canvas.getContext('2d');
\t \t var player = {
\t \t \t x: 0,
\t \t \t y: 197.5
\t \t };
\t \t var slope = {
\t \t \t x: 159,
\t \t \t y: 198.5,
\t \t \t up: false
\t \t };
\t \t var gold = {
\t \t \t x: 240,
\t \t \t y: 188,
\t \t \t mined: false,
health: 20,
\t \t \t x1: 200,
\t \t \t y1: 188,
\t \t \t mined1: false
\t \t };
\t \t ctx.font = '15px Monospace';
\t \t var move = setInterval(function(){player.x += 7;},50)
\t \t var draw = setInterval(function() {
\t \t \t ctx.clearRect(0, 0, canvas.width, canvas.height)
\t \t \t ctx.fillText('\\o/',player.x,player.y);
\t \t \t ctx.fillText("__________________",0,195.5);
\t \t \t ctx.fillText("/",slope.x,slope.y);
\t \t \t ctx.fillText("______________________________________________________",165.5,184.5);
\t \t \t ctx.fillText("/0\\",gold.x,gold.y);
\t \t \t ctx.fillText("/0\\",gold.x1,gold.y1);
\t \t \t if (player.x >= slope.x - 23 && !slope.up) {
\t \t \t \t player.y -= 10;
\t \t \t \t slope.up = true;
\t \t \t }
\t \t \t if (player.x >= gold.x - 23 && gold.mined == false) {
\t \t \t \t clearInterval(move)
console.log(gold.health)
dam = setInterval(function(){gold.health -= player.ATK}, 1000)
console.log(gold.health)
\t \t \t }
\t \t \t if (gold.health <= 0) {
\t \t \t \t \t gold.mined = true; gold.y = 210; move = setInterval(function(){player.x += 7;},50)
\t \t \t }
\t \t \t if (player.x >= gold.x1 - 23.99 && gold.mined == false) {
\t \t \t \t gold.y1 = 250;
\t \t \t \t gold.mined1 = true;
\t \t \t }
\t \t }, 50)
\t }
}
start('goldMine')
<canvas id='canvas' width='985' height='200'></canvas>
gold.health
是
player.ATK
減,則返回20則不確定。我期望的是它應該每1000ms減去
gold.health
player.ATK
。但爲什麼它返回undefined?我可以用JavaScript解決這個問題嗎?
你能提供一個https://stackoverflow.com/help/mcve – Isaac