使用onClick

时间:2016-04-26 17:40:13

标签: javascript html

仍然致力于我的算命先生游戏。我无法弄清楚如何进入“下一级”意味着下一个功能。因为onclick是为levelA()设置的。 有没有办法让onclick更改?它与以前的逻辑相同:

  • 屏幕A:1,2,3,4 //点击一个数字并获得屏幕B(2/4)或C(1/3)
  • 屏幕B:5,6,7,8 //点击一个数字并获得屏幕B(6/8)或C(5/7)
  • 屏幕C:9,10,11,12 //点击一个数字并获得屏幕B(10/12)或C(9/11)
  • 屏幕D://屏幕上有财富

一个典型的游戏(基于点击的数字)将会:

  • 屏幕A:单击2转到屏幕B
  • 屏幕B:点击6转到屏幕B
  • 屏幕B:单击8转到屏幕D
  • 屏幕D:财富

  • 屏幕A:单击1转到屏幕C
  • 屏幕B:点击10转到屏幕B
  • 屏幕B:单击5转到屏幕D
  • 屏幕D:财富

这是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";
  }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您已接受函数levelA(s1,s2,s3,s4)中的参数,但没有其他函数接受四个参数,例如: levelD2()函数你检查是否(s1)条件但是它总是假的,因为这里s1的值是"未定义"。所以在函数中添加参数以接受来自调用者的值。