2014-04-15 38 views
1

我目前正在製作一款卡片戰鬥遊戲。在主戰場景中,我試圖展示這些牌正在互相爭鬥,他們的健康狀況正在下降,最終武器也在場邊移動。Corona在作曲家中的主動戰鬥場景

目前,當它進入戰鬥循環時,顯示屏凍結,但我一直在記錄正在發生的事情以及正在發生的戰鬥,就在現場後面。我已經把代碼頂部的戰鬥循環分成了自己的功能,並通過點擊事件來調用該功能。

我通過使用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 
+0

-1有太多的代碼,人們都忙!簡化這個例子,就像只有一張卡片一樣,你爲什麼需要6個顯示問題?此外,澄清你如何驗證它是「幕後」的工作,這是什麼意思,你怎麼知道這一點?如果您很快修復代碼,我很樂意撤銷downvote。 – Schollii

+0

現在編輯。我仍然需要至少有其他牌在那裏顯示的戰鬥序列,雖然我刪除了顯示他們與他們的健康酒吧渲染的位。 –

+0

好得多。當你接受時,你通常應該也會投票贊成或者評論答案爲什麼沒有upvote案件作者可以改善它。歡迎來到SO! – Schollii

回答

1

的問題是,你的戰鬥場景功能是循環和修改了現場,但現場引擎更新現場活動只之間處理呼叫。也就是說,如果你的水龍頭功能被調用並且你修改了它,你只有在水龍頭功能返回並且場景引擎已經處理了新的場景狀態後纔會看到改變。

所以不是這樣:

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 

此執行「做的東西」,當條件爲真,和時間表以後battleScene通話。在此之後,battleScene將立即返回,給定顯示引擎更新GUI的機會,並且2ms之後,將再次呼叫battleScene,直到最終條件爲假並且不會安排重新呼叫。請注意,我必須創建一個臨時匿名函數,因爲battleScene需要參數,而performWithDelay不會將任何參數傳遞給預定函數,但可以通過匿名函數將其作爲upvalues隱式給出。需要明確的是,如果battleScene採取無參數,你可能只是做到了這一點:

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

我有一個後續問題。當我這樣做時,我還在battleScene()函數內部使用了for循環來獲取每張卡片,但這最終導致命令發出的速度加快,從而再次凍結。你有建議讓我解決這個問題嗎? –

+0

@JustinFoley SO不是討論新聞組。這似乎是一個單獨的問題,所以請發佈一個新問題。我(我確信其他人)會很高興看一看。評論並不意味着這一點,或者顯示代碼塊,你需要展示這些代碼塊,以便我們能夠理解你正在嘗試做什麼。 – Schollii