简体   繁体   English

在Javascript中,我的onEvent没有被调用,而其他onEvents是

[英]In Javascript, my onEvent isn't being called, while other onEvents are

I'm currently creating a blackjack game. 我正在制作一个二十一点游戏。 In my game, when the user clicks "Stay", the program is suppose to find who is closer to 21, the user or the house. 在我的游戏中,当用户点击“停留”时,该程序假设找到谁更接近21,用户或房子。 When I click stay, nothing occurs in my program. 当我点击停留时,我的程序中没有任何内容。 Any help will be appreciated. 任何帮助将不胜感激。 Thanks for reading. 谢谢阅读。

onEvent("stayButton","clicked", function()   {

 if (userTotal == 21) {
      setText("userArea","Player total is: " + userTotal +" Player wins!");
 }
  houseTotal();

  while (houseTotal < 17)  {
    appendItem( houseHand,deck.pop() );
    houseHandTotal();
  }

  if (houseTotal > 21) {
   setText("houseArea","House busts, User wins");
   var userWins = betAmount * 1.2;
   TotalMoney = TotalMoney + userWins;
  }

  setText("houseArea",houseHand + "house total is: " + houseTotal);

  if ( 21 - userTotal < 21-houseTotal && userHandValue != "bust" && houseHandValue != "bust")   
  {
    setText("userArea","Player total is: " + userTotal +" Player wins!");
  }

  else if (21 - userTotal > 21-houseTotal && userHandValue != "bust" && houseHandValue != "bust") 
  {
    setText("userArea","Player total is: " + userTotal +" Player loses!");
  }

});

Assuming onEvent is a wrapper for addEventListener , then the proper event name is click , not clicked . 假设onEventaddEventListener的包装器,则click正确的事件名称,而不是clicked

Other minor clean up points: 其他小清理点:

  • Also, use === and !== for equality comparisons instead of == and != to avoid type conversion related nuances. 另外,使用===!==进行相等比较而不是==!=以避免与类型转换相关的细微差别。
  • Lastly, mostly stylistic, JS code tends to prefer single quotes for strings ' . 最后,主要是风格,JS代码往往更喜欢字符串'单引号。
  • Not JS specific, but try using a logical approach to new lines in your code to make it readable. 不是JS特定的,但尝试使用逻辑方法在代码中使用新行来使其可读。
onEvent('stayButton', 'click', () => {
  if (userTotal === 21)
    setText('userArea', 'Player total is: ' + userTotal + ' Player wins!');

  houseTotal();
  while (houseTotal < 17) {
    appendItem(houseHand, deck.pop());
    houseHandTotal();
  }

  if (houseTotal > 21) {
    setText('houseArea', 'House busts, User wins');
    let userWins = betAmount * 1.2;
    TotalMoney = TotalMoney + userWins;
  }

  setText('houseArea', houseHand + 'house total is: ' + houseTotal);

  if (21 - userTotal < 21 - houseTotal && userHandValue !== 'bust' && houseHandValue !== 'bust')
    setText('userArea', 'Player total is: ' + userTotal + ' Player wins!');
  else if (21 - userTotal > 21 - houseTotal && userHandValue !== 'bust' && houseHandValue !== 'bust')
    setText('userArea', 'Player total is: ' + userTotal + ' Player loses!');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM