仍然致力于我的算命先生游戏。我无法弄清楚如何进入“下一级”意味着下一个功能。因为onclick是为levelA()设置的。 有没有办法让onclick更改?它与以前的逻辑相同:
一个典型的游戏(基于点击的数字)将会:
或
这是JsFiddle和代码!
function startGame() {
document.getElementById("s1").innerHTML = "1";
document.getElementById("s2").innerHTML = "2";
document.getElementById("s3").innerHTML = "3";
document.getElementById("s4").innerHTML = "4";
}
function levelA(s1, s2, s3, s4) {
if (s2 || s4) {
levelB();
} else {
levelC();
}
}
function levelB() {
document.getElementById("s1").innerHTML = "5";
document.getElementById("s2").innerHTML = "6";
document.getElementById("s3").innerHTML = "7";
document.getElementById("s4").innerHTML = "8";
if (s2 || s4) {
levelB2();
} else {
levelC2();
}
}
function levelB2() {
document.getElementById("s1").innerHTML = "5";
document.getElementById("s2").innerHTML = "6";
document.getElementById("s3").innerHTML = "7";
document.getElementById("s4").innerHTML = "8";
if (s2 || s4) {
levelD();
} else {
levelD2();
}
}
function levelC() {
document.getElementById("s1").innerHTML = "9";
document.getElementById("s2").innerHTML = "10";
document.getElementById("s3").innerHTML = "11";
document.getElementById("s4").innerHTML = "12";
if (s2 || s4) {
levelC2();
} else {
levelB2();
}
}
function levelC2() {
document.getElementById("s1").innerHTML = "9";
document.getElementById("s2").innerHTML = "10";
document.getElementById("s3").innerHTML = "11";
document.getElementById("s4").innerHTML = "12";
if (s2 || s4) {
levelD();
} else {
levelD2();
}
}
function levelD(s1, s2, s3, s4) {
if (s1) {
document.getElementById("s1").innerHTML = "Good Things Will Come Your Way";
} else if (s2) {
document.getElementById("s2").innerHTML = "Outlook Doesn't Look Good";
} else if (s3) {
document.getElementById("s3").innerHTML = "Today Is Your Lucky Day";
} else if (s4) {
document.getElementById("s3").innerHTML = "Toady Is Not Your Lucky Day";
}
}
function levelD2() {
if (s1) {
document.getElementById("s1").innerHTML = "You Will Find Good Fortune";
} else if (s2) {
document.getElementById("s2").innerHTML = "Your Wish Will Come True";
} else if (s3) {
document.getElementById("s3").innerHTML = "Your Wish Will Not Come True";
} else if (s4) {
document.getElementById("s3").innerHTML = "Things Do Not Look Good";
}
}
感谢您的帮助!
答案 0 :(得分:0)
您已接受函数levelA(s1,s2,s3,s4)中的参数,但没有其他函数接受四个参数,例如: levelD2()函数你检查是否(s1)条件但是它总是假的,因为这里s1的值是"未定义"。所以在函数中添加参数以接受来自调用者的值。