Lua 《作曲家》中的电晕活跃战斗场景

Lua 《作曲家》中的电晕活跃战斗场景,lua,coronasdk,Lua,Coronasdk,我目前正在做一个纸牌战斗游戏。在主战场景中,我试图展示卡牌之间的积极战斗,它们的生命值降低,最终武器也在场景中移动 目前,当它进入战斗循环时,显示屏冻结,但我一直在记录正在发生的事情,战斗仍在发生,就在幕后。我已经在代码顶部将battle循环分离为它自己的函数,并使用tap事件调用该函数 我使用while循环中的print语句验证它是否正在运行,while循环将卡的当前运行状况和卡的名称打印到控制台。当前卡片的健康状况正在改变,只是没有改变场景,而是冻结在旧的卡片上,没有主动显示正在发生的事情

我目前正在做一个纸牌战斗游戏。在主战场景中,我试图展示卡牌之间的积极战斗,它们的生命值降低,最终武器也在场景中移动

目前,当它进入战斗循环时,显示屏冻结,但我一直在记录正在发生的事情,战斗仍在发生,就在幕后。我已经在代码顶部将battle循环分离为它自己的函数,并使用tap事件调用该函数

我使用while循环中的print语句验证它是否正在运行,while循环将卡的当前运行状况和卡的名称打印到控制台。当前卡片的健康状况正在改变,只是没有改变场景,而是冻结在旧的卡片上,没有主动显示正在发生的事情

以下是整个场景的代码:

function battleScene(playerCards, enemyCards, allCards, cardHealth)
  while not checkIfDead(playerCards) and not checkIfDead(enemyCards) do
    for i=1, 6 do
      if allCards[i]~=0 then
        allCards[i]:battle()
      end
      print( allCards[i]:getCurHealth().." "..allCards[i]:getName() )--The test to see current health of card
      cardHealth[i]:setHealth(allCards[i]:getCurHealth(),allCards[i]:getHealth())
      if checkIfDead(playerCards) or checkIfDead(enemyCards) then
        break
      end
      usleep(2000)
    end
  end
end
---------------------------------------------------------------------------------

-- "scene:show()"
function scene:show( event )

  local sceneGroup = self.view
  local phase = event.phase

  if ( phase == "will" ) then
     -- Called when the scene is still off screen (but is about to come on screen).
  elseif ( phase == "did" ) then
     --The current health of each card is set to max 
     --and then the card is rendered along with health bars

     local card1=test1:render()
     card1.x=display.contentCenterX-100
     card1.y=display.contentCenterY-100
     sceneGroup:insert(card1)

     local card1Health=HealthBar:new()
     card1Health.x=display.contentCenterX-100
     card1Health.y=display.contentCenterY-40
     card1Health:setHealth(test1:getCurHealth(), test1:getHealth())
     sceneGroup:insert(card1Health)

     playerCards={test4, test5, test6}

     enemyCards={test1, test2, test3}

     for i=1, 3 do
       if playerCards[i]:getClass()=="Tank" or playerCards[i]:getClass()=="Damage" then
         playerCards[i]:setBattleSet(enemyCards)
       else
         playerCards[i]:setBattleSet(playerCards)
       end
     end

    for i=1, 3 do
      if enemyCards[i]:getClass()=="Tank" or enemyCards[i]:getClass()=="Damage" then
        enemyCards[i]:setBattleSet(playerCards)
      else
        enemyCards[i]:setBattleSet(enemyCards)
      end
    end

    local allCards={test1, test2, test3, test4, test5, test6}

    bubbleSort(allCards)

    local cardHealth=     {card1Health,card2Health,card3Health,card4Health,card5Health,card6Health}

    local startBattleButton=display.newText( "Start Battle", 0, 0, globals.font.regular, 18 )
    startBattleButton.x = display.contentCenterX
    startBattleButton.y = display.contentCenterY

    local function onTap(event)
      startBattleButton.isVisible=false
      battleScene(playerCards, enemyCards, allCards, cardHealth)
    end

    startBattleButton:addEventListener( "tap", onTap )

    sceneGroup:insert(startBattleButton)

    if checkIfDead(playerCards) then
      win=false
    end
  end
end

问题在于,您的战斗场景函数正在循环和修改场景,但是场景引擎仅在事件处理调用之间更新场景。也就是说,如果调用了tap函数并对其进行了修改,则只有在tap函数返回且场景引擎处理了新场景状态后,才能看到更改

因此,与其这样做,不如:

function battleScene(args)
  while condition do
     do stuff
  end
end
取而代之

function battleScene(args)
  if condition then
     do stuff
     timer.performWithDelay(2, function() battleScene(args) end)
  end
end
当条件为真时,它会执行“do stuff”,并计划稍后调用
battleScene
。鉴于显示引擎有机会更新GUI,
battleScene
将立即返回,2 ms后,将再次调用
battleScene
,直到最终条件为false且不会安排重新调用。请注意,我必须创建一个临时匿名函数,因为
battleScene
接受参数,而
performWithDelay
不向调度函数传递任何参数,但它们可以通过匿名函数隐式地作为upvalue提供。要明确的是,如果
战场
没有使用args,您可以这样做:

function battleScene()
  if condition then
     do stuff
     timer.performWithDelay(2, battleScene)
  end
end

-1代码太多了,大家都很忙!简化这个示例,就像只有一张卡一样,为什么需要6张卡来显示问题?此外,请澄清您是如何验证它是否在“幕后”工作的,这意味着什么,您是如何知道的?如果您很快修复代码,我很乐意撤消downvote。它现在已被编辑。我仍然需要至少有其他卡在那里显示战斗序列,虽然我删除了显示他们渲染和他们的健康栏的位。更好。当你接受的时候,你通常也应该向上投票,或者评论一下为什么没有向上投票,以防作者能改进它。欢迎来到SO!当时我本来会投赞成票,但当时我没能投,但在你最后一次评论后,我被赋予了特权。没问题。我最近看到了很多令人惊讶的“忘记”,不知道是SO界面还是什么。我有一个后续问题。当我这样做时,我还在battleScene()函数中使用for循环来访问每张卡,但这最终会加快命令发出的速度,使其再次冻结。你有什么建议让我来解决这个问题吗?@JustinFoley不是一个讨论新闻组。这似乎是一个单独的问题,所以请发布一个新问题。我(我相信其他人)会很乐意看一看。注释不是为了这个目的,也不是为了显示代码块,您需要显示这些代码块,以便我们能够理解您试图做什么。