我正在尝试制作一个非常简单的骰子滚动游戏,这样我就可以获得Javascript的基础知识。
这是两个骰子 - 一个红色,一个蓝色 - 以及每个骰子保持得分,以查看每个骰子赢了多少轮。
骰子滚得很好,但我对计数器没有运气。如果红色的那个更高,那么我希望它的计数器增加1.同样为蓝色。然而,目前没有任何事情发生。
这是HTML:
<div id="reds">
<div id="die1">0</div>
<div id="redCounter"></div>
</div>
<div id="blues">
<div id="die2">0</div>
<div id="blueCounter"></div>
</div>
Javascript的相关内容:
var redCounter = document.getElementById("redCounter");
var blueCounter = document.getElementById("blueCounter");
var redScore = 0;
var blueScore = 0;
if(d1 < d2) {
status.innerHTML += " <br>Blue Wins!";
var blueCounter=+1;
blueCounter.innterHTML= "Blue has " + blueScore + " points!";
}
else if(d1 > d2) {
status.innerHTML += " <br>Red Wins!";
var blueCounter=+1;
document.getElementByID("redCounter").innterHTML=redCounter;
}
任何人都可以给我任何关于我哪里出错的线索吗?
答案 0 :(得分:1)
var blueCounter = + 1例如在if块中创建一个新变量并将其设置为1.首先使用blueCounter作为dom元素。如果您想添加到之前添加的blueCounter,请执行此操作
for(var i=0;i<5;i++){
pullJSON(i);
}
function pullJSON(i) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheet = ss.getActiveSheet();
var url = "http://myapi&_page="+i; // Paste your JSON URL here
var headers = {
"Content-Type": "application/json",
"Authorization": "API Key"
};
var options = {
"method": "GET",
"headers": headers
};
var response = UrlFetchApp.fetch(url, options); // get feed
var dataAll = JSON.parse(response.getContentText()); //
var dataSet = dataAll;
Logger.log(dataAll.length);
var rows = [],
data;
for (i = 0; i < dataSet.length; i++) {
data = dataSet[i];
var utcSeconds = data.createDate;
var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
d.setUTCSeconds(utcSeconds);
rows.push([data.id, data.c_customer_id, data.firstName, data.email, d]); //your JSON entities here
}
dataRange = sheet.getRange(2, 2, rows.length, 5); // 3 Denotes total number of entites
dataRange.setValues(rows);
}
简短:您正在将您的DomElement blueCounter与您的分数变量blueScore
混合