我是JavaScript新手。 我已經通過學習JavaScript(o'reilly)工作我的工作,但我只是想做我的第一個JavaScript。初學者JavaScript
我認爲最好在我感興趣的事情上工作,事實證明它相當複雜。
我基本上是試圖模擬(最終)在太空綠巨人(Boardgame)中一個Genestealer在他和太空海洋之間有12個步驟的情況。 第一步它的6個骰子殺死Genestealer,之後5或6殺死。 如果骰子上的數字相同,則槍支卡塞。
我只是想模仿這裏的第一步。我認爲問題是與jamCheck。
基本上這個輸出總是正確的,即使我改變它爲!=它總是顯示槍塞。
我想知道是否需要將變量傳遞到另一個局部變量,但它適用於killCheck而無需執行此操作。 (我試過了,雖然我可能做錯了)
這是完全可能的,這裏有一些非常簡單的錯誤。
我希望你能幫忙,或者指點我正確的方向。
非常感謝!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>SH</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function diceRoll1() {
iValue = Math.random(); // random number between 0 and 1
iValue *= 6; // multiply by 6 to move the decimal
iValue = Math.floor(iValue)+1; // round to nearest integer. +1 to 1-6.
var roll1 = iValue;
document.getElementById('result1').innerHTML = 'Dice roll 1 : ' + roll1;
killCheck (roll1);
jamCheck (roll1);
return;
}
function diceRoll2() {
iValue = Math.random(); // random number between 0 and 1
iValue *= 6; // multiply by 6 to move the decimal
iValue = Math.floor(iValue)+1; // round to nearest integer. +1 to 1-6.
var roll2 = iValue;
document.getElementById('result2').innerHTML = 'Dice roll 2 : ' + roll2;
killCheck (roll2);
jamCheck (roll2);
return;
}
function killCheck(roll1,roll2){
if (roll1==6 || roll2==6)
{
document.getElementById('kill').innerHTML = 'GS KILLED';
}
return;
}
function jamCheck(roll1,roll2){
if (roll1 == roll2)
{
document.getElementById('jam').innerHTML = 'GUN JAMMED';
}
return;
}
//]]>
</script>
</head>
<body onload="diceRoll1();diceRoll2();killCheck();jamCheck();">
<p id="result1">Dice roll 1</p>
<p id="result2">Dice roll 2</p>
<p id="kill">GS ALIVE</p>
<p id="jam">GUN FINE</p>
</body>
</html>
編輯:我最終與很多來自朋友的幫助了那裏;這裏是當前的代碼:
...
function getDiceValue() {
var diceValue = Math.random();
diceValue *= 6;
diceValue = Math.floor(diceValue) + 1;
return diceValue;
}
function killCheck(roll1, roll2) {
if (roll1 === 6 || roll2 === 6) {
document.getElementById('kill').innerHTML = 'GS KILLED';
}
return;
}
function jamCheck(roll1, roll2){
if (roll1 === roll2) {
document.getElementById('jam').innerHTML = 'GUN JAMMED';
}
return;
}
function rollDice() {
var roll1 = getDiceValue(),
roll2 = getDiceValue();
document.getElementById('result1').innerHTML = 'Dice roll 1 : ' + roll1;
document.getElementById('result2').innerHTML = 'Dice roll 2 : ' + roll2;
killCheck (roll1, roll2);
jamCheck (roll1, roll2);
}
//]]>
...
<body onload="rollDice();">
只是一個不相關的註釋,不要在函數末尾使用空返回(它們將不會返回任何內容)。你爲什麼要使用空迴歸的唯一原因是停止函數(類似於循環中斷) – ajax333221
如果你想清理一些東西,這裏有一個方便的模具卷功能:function rollDie(min,max ){ return Math.floor(Math.random()* max)+ min; } 6面例子:rollDie(1,6); – pdizz
@ ajax333221我讀過它會將它返回給killCheck和JamCheck,是否根本不需要? – snoopyj