救生栏:如何在与其他物体碰撞时增加生命条?

时间:2014-04-25 08:13:33

标签: lua corona

我已经建立了自己的生活栏(进度栏),如下所示:

function scene:enterScene(event)
    storyboard.removeScene("start")
    screenGroup = self.view 
    lifeBar = {}
    lives = 141 -- is the global width of my full lifebar
    maxLives = 141
    for i = 1, maxLives do
        lifeBar[i] = display.newImage("lifebar.png") --lifebar.png image width is : 1px
        lifeBar[i].anchorX=0
        lifeBar[i].anchorY=0.6
        lifeBar[i].x = fuel_title.x +114+13.5+(lifeBar[i].contentWidth * (i - 1))
        lifeBar[i].y = 37 -- start at 10,10
        lifeBar[i].isVisible=true
        screenGroup:insert(lifeBar[i])
    end
end

所以,多亏了你,我知道一个增加生命的功能(livesValue.text显示增加的生命),无论如何,救生员{}表似乎有一些问题因为我看不出有什么不同救生竿图像。我的问题是:我的桌子生活吧有问题吗?

 if (event.other.myName == "fuel") then
    if lives > maxLives then
        lives = maxLives
    elseif lives < #lifeBar then
        lives = lives + 1
            lifeBar[lives].isVisible=true
        end
  livesValue.text = string.format("%d", lives)                      
  local other = event.other
  timer.performWithDelay(1, function() other:removeSelf() end)
end
    Runtime:addEventListener("touch", FuelManage)

1 个答案:

答案 0 :(得分:0)

试试这个:

function onCollision( self, event )
    if event.phase == "began" then
        if event.other.myName == "fuel" then

            -- increase life bar, if slots left:
            if lives < #lifeBar then
                lives = lives + 1
                lifeBar[lives].isVisible=true
            end

            -- schedule the fuel object we collided with for removal:
            local other = event.other -- don't want to use event as upvalue
            timer.performWithDelay(1, function() other:removeSelf() end)
        end
    end
end

请注意,不允许在碰撞处理程序中删除碰撞对象,必须按照Corona Collision文档页面中的说明进行延迟操作。所以我做到了。此外,如果lifeBar中有空格,您应该只添加生命(类似地,您应修复代码以仅在生命超过0时才移除生命)。