2016-09-01 44 views
-1

我增加了更復雜的遊戲更新。我試圖爲我的第一個javascript項目創建一個簡單的小型瀏覽器RPG遊戲,並且對當前代碼有幾個擔憂:使這個Javascript遊戲正確地循環

1)我希望遊戲根據輪次數進行循環。遊戲將從5輪開始,在擊中0之後結束。很明顯,在我的函數中添加for循環可以解決當前的邊界問題,但由於某種原因,頁面上沒有顯示任何內容?

2)由於我的間隔計時器,整個函數會一次循環1秒,但我怎樣才能得到它,因此我的函數中的每一行都需要1秒,因此看起來它實際上正在發生與作爲一個整體功能,每隔1秒就會有一段文字?

這裏是我的代碼迄今:

var yourHP = 100; 
var dragonHP = 100; 
var yourattk = Math.floor((Math.random() * 100) +1); 
var dragonattk = Math.floor((Math.random() * 100) +1); 
var totalRounds = 5; 

function startGame() { 

    for (totalRounds === 5; totalRounds > 0; totalRounds--) { 
    document.write("You begin the attack!<br>"); 
    document.write("You attacked the dragon for " + yourattk + "!<br>"); 
    var dbattlehp = dragonHP - yourattk; 
    document.write("The dragon takes a hit! Dragon's HP is now " + dbattlehp + "<br>"); 
    document.write("Dragon rears it's head to strike!<br>"); 
    var yourbattlehp = yourHP - dragonattk; 
    document.write("Dragon attacked you for" + dragonattk + " , your remaining hp:" + yourbattlehp + "<br>"); 
    document.write("Congrads! You survived the attack... Your current HP Is:" + yourbattlehp + ", and the dragons:" + dbattlehp + "<br>"); 
    var yourChangedAttk = Math.floor((Math.random() * 100) +1); 
    var dchangeAttk = Math.floor((Math.random() * 100) +1); 
    yourChangedAttk = yourattk; 
    dchangeAttk = dragonattk; 
    } 
    else { 
     document.write("Game's Over!"); 
    } 

setInterval(startGame, 1000); 

startGame(); 
+0

爲什麼你使用document.write? – Robusto

+1

最後一個'document.write()'中存在拼寫錯誤/語法錯誤。字符串''龍攻擊你''和變量'龍頭'不能出現在彼此旁邊沒有他們之間的操作員 - 很可能是一個'+'。 –

+0

@JonathanLonowski你改變了整個問題,現在所有的評論和回答都沒有意義。 – Arjun

回答

3

從startGame中的setInterval刪除引號()函數:

setInterval(startGame, 1000); 

而且還由喬納森和喬希強調語法錯誤:

document.write("Dragon attacked you for" + dragonattk + " , your remaining hp:" + yourbattlehp); 

所以你的代碼應該是這樣的:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>Fun with Programming</title> 
<script> 

var yourHP = 100; 
var dragonHP = 100; 
var yourattk = Math.floor((Math.random() * 100) +1); 
var dragonattk = Math.floor((Math.random() * 100) +1); 
var yourLives = 5; 


</script> 

</head> 
<body> 

<script type="text/javascript"> 

setInterval(startGame, 1000); 

function startGame() { 
    document.write("You begin the attack!"); 
    document.write("You attacked the dragon for " + yourattk + "!"); 
    var dbattlehp = dragonHP - yourattk; 
    document.write("The dragon takes a hit! Dragon's HP is now " + dbattlehp); 
    document.write("Dragon rears it's head to strike!"); 
    var yourbattlehp = dragonattk - yourHP; 
    document.write("Dragon attacked you for"+ dragonattk + " , your remaining hp:" + yourbattlehp); 

} 

startGame(); 
+0

這是答案的一部分。其餘的是有一個語法錯誤:'document.write(「龍襲擊了你的」dragonattk +「,' –

0

您在那裏遇到了一些問題。首先,你在dragonattk之前錯過了一個+號。其次,您在setInterval中的方法名稱周圍有一些引號。

以下代碼有效。檢出片段並運行它以查看所有正在運行的內容。

var yourHP = 100; 
 
var dragonHP = 100; 
 
var yourattk = Math.floor((Math.random() * 100) +1); 
 
var dragonattk = Math.floor((Math.random() * 100) +1); 
 
var yourLives = 5; 
 

 
function startGame() { 
 
    document.write("You begin the attack!"); 
 
    document.write("You attacked the dragon for " + yourattk + "!"); 
 
    var dbattlehp = dragonHP - yourattk; 
 
    document.write("The dragon takes a hit! Dragon's HP is now " + dbattlehp); 
 
    document.write("Dragon rears it's head to strike!"); 
 
    var yourbattlehp = dragonattk - yourHP; 
 
    document.write("Dragon attacked you for" + dragonattk + " , your remaining hp:" + yourbattlehp); 
 

 
} 
 

 
setInterval(startGame, 1000);
<html> 
 
<head> 
 
    <meta charset="utf-8"> 
 
    <title>Fun with Programming</title> 
 
</head> 
 
<body> 
 

 
</body> 
 
</html>

0
在最後文件撰寫

你錯過了一個 「+」。 (「Dragon襲擊了你的」+ dragonattk +「,你剩下的hp:」+ yourbattlehp);「

在鉻,火狐和IE檢查和工作正常