Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Coffescript回调&;功能_Javascript_While Loop_Coffeescript - Fatal编程技术网

Javascript Coffescript回调&;功能

Javascript Coffescript回调&;功能,javascript,while-loop,coffeescript,Javascript,While Loop,Coffeescript,我有这个函数,我正在尝试使用两个由较小的函数和一个while循环组成的大函数。一个是当轮到玩家时,一个是当轮到对手时,一个是当玩家赢了时,一个是一圈。当我在浏览器中运行它时,按钮不起任何作用。怎么了 $attackButtonClick = (opponent) -> if playersTurn and not player.Win player.attack(opponent) if opponent.currentHealth <= 0

我有这个函数,我正在尝试使用两个由较小的函数和一个while循环组成的大函数。一个是当轮到玩家时,一个是当轮到对手时,一个是当玩家赢了时,一个是一圈。当我在浏览器中运行它时,按钮不起任何作用。怎么了

$attackButtonClick = (opponent) ->
    if playersTurn and not player.Win
        player.attack(opponent)
        if opponent.currentHealth <= 0
            player.Win = true;
            allOff()
            postBattle(opponent)
        else 
            playersTurn = false;
            opponentTurn(opponent)

$defendButtonClick = (opponent) ->
    if playersTurn and not player.Win
        player.defend()
        appendToBattleOutputBox "<p>Your defense has been doubled for one turn.</p>"
        if opponent.currentHealth <= 0
            player.Win = true;
            allOff()
            return postBattle(opponent)
        else 
            playersTurn = false;
            opponentTurn(opponent)

$useItemButtonClick = (opponent) ->
    if playersTurn and not player.Win
        if $useItemSelection.html().length > 15
            itemBeingUsed = $("select[name='useItemSelection'] option:selected").text()
            switch itemBeingUsed
                when "Book of Spells"
                    player.Items.bookOfSpells.use()
                    if player.Items.bookOfSpells.effect is "burn" or player.Items.bookOfSpells.effect is "poison"
                        appendToBattleOutputBox "<p>You have #{player.Items.bookOfSpells.effect}ed #{opponent.Name}.</p>"
                        if player.Items.bookOfSpells.effect is "burn"
                            opponent.Burned = true;
                        else
                            opponent.Poisoned = true;
                    else
                        appendToBattleOutputBox "<p>You have frozen #{opponent.Name}.</p>"
                        opponent.Frozen = true;
                    player.Items.bookOfSpells.used = true;
                when "Shield Charm"
                    if (player.Items.shieldCharm.used is false)
                        player.Items.shieldCharm.use()
                        appendToBattleOutputBox "<p>You will block the next attack with you shield charm.</p>"
                        player.Items.shieldCharm.used = true;
                when "Normal Potion"
                    player.Items.normalPotion.use()
            if opponent.currentHealth <= 0
                player.Win = true;
                allOff()
                postBattle(opponent)
            else 
                playersTurn = false;
                opponentTurn(opponent)
        else 
            noUsableItemP = $("<p id='noUsableItemP'>You have no usable items. Select another command.</p>")
            $battleCommandPromptDiv.empty()
            $battleCommandPromptDiv.append(noUsableItemP)

battle = (opponent) ->
    console.log(p)
    appendToBattleOutputBox "<p>You make the first move!</p>"
    battleInProgress = yes;
    playersTurn = true;
    playerTurn = (opponent) ->
        $attackButton.click -> $attackButtonClick(opponent)

        $defendButton.click -> $defendButtonClick(opponent)

        $useItemButton.click -> $useItemButtonClick(opponent)

        status.Poison("opponent", opponent) if opponent.Poisoned
        status.Burn("opponent", opponent) if opponent.Burned
        status.Freeze("opponent", opponent) if opponent.Frozen
        opponent.undefend() if opponent.defenseDoubled or opponent.defenseTripled
        refresh(opponent)

    opponentTurn = (opponent) ->
        if not playersTurn and not player.Win
            if rndmNumber(10) > 5
                thisTurnAttack = opponent.attack(opponent.Attack, opponent.Luck)
                player.currentHealth -= thisTurnAttack
                refresh(opponent)
                if thisTurnAttack is 0 then appendToBattleOutputBox "<p class='right'>You have blocked the attack.</p>" else appendToBattleOutputBox "<p class='right'>#{thisTurnAttack} damage has been inflicted upon you.</p>"
            else 
                opponent.defend()
                appendToBattleOutputBox "<p class='right'>#{opponent.Name} has doubled his defense for one turn.</p>"
            player.undefend() if player.defenseDoubled or player.defenseTripled
            player.Poisoned = true if (opponent.Type is "Snake" and rndmNumber(10) > 7)
            player.Burned = true if (opponent.Name is "Salamander" and rndmNumber(10) > 3) or opponent.Name is "Fire Dragon"
            player.Frozen = true if (opponent.Name is "Penguin" and rndmNumber(10) > 5) or (opponent.Name is "Wolf" and rndmNumber(10) > 6) or (opponent.Name is "Polar Bear" and rndmNumber(10) > 4)
            status.Poison("player") if player.Poisoned
            status.Burn("player") if player.Burned
            status.Freeze("player") if player.Frozen
            if player.currentHealth <= 0 then postBattle(opponent) else playerTurn(opponent)
            playersTurn = true;

    playerTurn(opponent)

    while (player.Win is true)
        player.Win is false;
        postBattle(opponent)

注意:请忽略true和false后面的分号。我知道他们不应该在那里。

这很难解释。您从根本上误解了Javascript(扩展为Coffeescript)的工作原理

Javascript是一种异步语言,不会阻塞io。当你这样做的时候

while (playerTurn is true and player.Win is false)
    $attackButton.click ->
        player.attack(opponent)
        ...
您正在进入一个循环,该循环为单击事件注册回调,然后再次运行该循环。由于while循环的性质,这种情况会发生数百次,甚至数千次。这就是你的浏览器崩溃的原因

检查这种情况的一个好方法是在console.log中使用调试语句,通过查看控制台中打印输出的数量,您可以看到代码的不同部分被输入的频率

你需要重新构造你的游戏逻辑,这样你就不用边循环边等待阻塞条件的发生;您有一组在事件发生时相互调用的函数

我的建议如下:

attack = ->
  # conditions go here instead
  if playerTurn and not player.Win
    player.attack(opponent)
    if opponent.currentHealth <= 0
      player.Win = true
      allOff()
      # return statements aren't needed inside callback functions
      postBattle(opponent)
    else 
      playerTurn = false;

# not inside a while loop
$attackButton.click attack
function opponentTurn (opponent)
  ...
# rather than
opponentTurn = (opponent) ->
  ... 
分别编译成

function opponentTurn(opponent){
  ...
}

var opponentTurn;
opponentTurn = function(opponent){
  ...
};
我不能推荐Livescript作为咖啡脚本的替代品


没有简单的方法来解释这一点。您从根本上误解了Javascript(扩展为Coffeescript)的工作原理

Javascript是一种异步语言,不会阻塞io。当你这样做的时候

while (playerTurn is true and player.Win is false)
    $attackButton.click ->
        player.attack(opponent)
        ...
您正在进入一个循环,该循环为单击事件注册回调,然后再次运行该循环。由于while循环的性质,这种情况会发生数百次,甚至数千次。这就是你的浏览器崩溃的原因

检查这种情况的一个好方法是在console.log中使用调试语句,通过查看控制台中打印输出的数量,您可以看到代码的不同部分被输入的频率

你需要重新构造你的游戏逻辑,这样你就不用边循环边等待阻塞条件的发生;您有一组在事件发生时相互调用的函数

我的建议如下:

attack = ->
  # conditions go here instead
  if playerTurn and not player.Win
    player.attack(opponent)
    if opponent.currentHealth <= 0
      player.Win = true
      allOff()
      # return statements aren't needed inside callback functions
      postBattle(opponent)
    else 
      playerTurn = false;

# not inside a while loop
$attackButton.click attack
function opponentTurn (opponent)
  ...
# rather than
opponentTurn = (opponent) ->
  ... 
分别编译成

function opponentTurn(opponent){
  ...
}

var opponentTurn;
opponentTurn = function(opponent){
  ...
};
我不能推荐Livescript作为咖啡脚本的替代品


用函数替换while循环是否可行?谢谢,我会试试看!酷,如果你遇到任何问题,请毫不犹豫地更新你的问题,我会尝试为你指出正确的方向。我已经更新了我的代码。我有
playerTurn
正在工作,但是功能
opponentTurn
没有被提升,因此它被认为是未定义的。有什么想法吗?我刚刚重新整理了代码。Livescript现在看起来对我来说是一个很好的学习机会,但我会记住它。谢谢用if语句替换while循环是否可行?谢谢,我会试试的!酷,如果你遇到任何问题,请毫不犹豫地更新你的问题,我会尝试为你指出正确的方向。我已经更新了我的代码。我有
playerTurn
正在工作,但是功能
opponentTurn
没有被提升,因此它被认为是未定义的。有什么想法吗?我刚刚重新整理了代码。Livescript现在看起来对我来说是一个很好的学习机会,但我会记住它。谢谢