我已经创建了一个游戏,用户在开始游戏之前输入他们的名字,当他们在游戏中掷两个骰子,并且获得两个匹配的骰子时,他们将有机会玩另一回合,但是当我单击“滚动”按钮,当再次进行下一轮比赛时,它将再次询问用户名。
当用户获得第二轮比赛机会时,如何让我一次提示输入姓名?
我考虑过要使用布尔值,但是我不确定如何实现它。
var diceImage = ["images/1.jpg","images/2.jpg","images/3.jpg","images/4.jpg","images/5.jpg","images/6.jpg"];
var practiceDate;
var name;
var sum = 0;
document.getElementById("Dice1st").style.display = "none";
document.getElementById("Dice2nd").style.display = "none";
var rounding =1;
var db = openDatabase('RollDice','1.0','Game DB', 2* 1024 * 1024);
var msg;
function roll() {
start();
var dice1 = document.getElementById("Dice1st");
var dice2 = document.getElementById("Dice2nd");
var math1 = Math.floor(Math.random() * 6);
var math2 = Math.floor(Math.random() * 6);
var button = document.getElementById("rollButton");
document.getElementById("Dice1st").style.display = "";
document.getElementById("Dice2nd").style.display = "";
var firstValue = diceImage[math1];
var secondValue = diceImage[math2];
dice1.src = firstValue;
dice2.src = secondValue;
if(math1 == math2)
{
sum = sum + math1 +1 + math2 + 1;
document.getElementById("rollButton").style.display ="";
document.getElementById("rounding").innerHTML = "Number of Round(s): " + rounding;
document.getElementById("totalSum").innerHTML ="Sum of the Dice: " + sum;
rounding++;
}
else
{
document.getElementById("rollButton").style.display ="none";
sum = sum + math1 +1 + math2 + 1;
document.getElementById("rounding").innerHTML = "Number of Round(s): " + rounding;
document.getElementById("totalSum").innerHTML ="Sum of the Dice: " + sum;
}
db.transaction(function (transaction) {
transaction.executeSql('INSERT INTO game (name, noOfAttempts, timeOfAttempt, totalResult) VALUES (?, ?, ?, ?);',[name, rounding, practiceDate.getTime(), sum])
})
}
function start() {
name = prompt("Enter your name. ");
if (name == null) return;
practiceDate = new Date();
document.getElementById("timeDiv").innerHTML = "";
gameCount = 0;
}
function createLocalDB() {
db.transaction(
function (transaction) {
transaction.executeSql('CREATE TABLE IF NOT EXISTS game ( gID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, noOfAttempts INTEGER NOT NULL, timeOfAttempt INTEGER NOT NULL, totalResult NUMERIC NOT NULL);',[],
function dataHandler (transaction, results) {},
function errorHandler (transaction, error) {
console.error("Error creating table: "+ error.code + ": " + error.message);
});
});
}
function checkAnswer() {
//save the results into database.
db.transaction(function (transaction) {
transaction.executeSql('INSERT INTO game (name, noOfAttempts, timeOfAttempt, totalResult) VALUES (?, ?, ?, ?);',[practiceDate.getTime(), name, noOfAttempts, timeOfAttempt, totalResult])
})
}
function processResult(transaction, results) {
displayResult(results);
}
</script>
当用户单击“滚动”按钮时,它将开始游戏,如果他们获得匹配的骰子,它将允许用户进行另一轮游戏。我在WebSQL中存储数据,例如名称,回合数,尝试时间和总结果,但是当用户获得第二次机会时,Web-SQL将显示重复数据。