【问题标题】:HowTo create a fuel bar如何创建燃料棒
【发布时间】:2014-04-28 17:20:57
【问题描述】:

我想制作一个燃料棒(从满油箱到空油箱)。当用户触摸屏幕时,燃料条会减少。 这是我的代码:

lifeBar = display.newImage("fuel_bar1.png")
lifeBar.anchorX=0
lifeBar.anchorY=0.6
lifeBar.x = fuel_title.x +114+13.5
lifeBar.y = 37
screenGroup:insert(lifeBar)

然后,每次变量“pressed”等于“true”时,我使用了一个名为 FuelConsumption() 的函数来减少油箱。当 Pressed==true 时,表示用户正在触摸屏幕:

 function FuelConsumption() 

    if lives > 0 and pressed==true then         
        lives = lives - 1
        lifeBar.width=lifeBar.width-1
        livesValue.text = string.format("%d", lives)
    end
end

这个函数在 enterFrame 事件中被激活如下:

 Runtime:addEventListener( "enterFrame", FuelConsumption )

效果很好,但我的问题是:如何每 5 秒减少一次生命条宽度(一旦变量按下==true)?

我尝试将此添加到我的触摸事件功能中:

function flyUp(event)

   if event.phase == "began" then
            pressed=true    

        if gameStarted == false then            

            gameStarted = true
            pauseBtn.isVisible=true
            opt_btn.isVisible=true
            Runtime:addEventListener("enterFrame", enterFrameListener)
            Runtime:addEventListener("enterFrame",scrollGrasses)
            timer.performWithDelay( 5000, function() Runtime:addEventListener( "enterFrame", FuelConsumption ) end,-1 )     
        end
   elseif event.phase == "ended" then
         pressed = false
         timer.performWithDelay( 1000, function() Runtime:removeEventListener( "enterFrame", FuelConsumption ) end,-1 ) 
    end                 
end

问题是代码消耗了大量内存,游戏运行速度非常慢!那么,有没有其他办法呢?

谢谢你:)

【问题讨论】:

  • 这表明努力不够,因为这有几个非常明显的问题,但它们是否与陈述的问题有关并不明显。你说的太快是什么意思:它需要多长时间/多少帧,与它应该需要多少?你试图诊断问题是什么?您是否阅读过有关 enterFrame 事件的文档?这样做并尝试在某些代码块中打印状态,以验证它们仅在您希望它们执行时才执行。)。然后用您发现的内容更新问题。
  • 谢谢...我脑子里还不清楚!
  • 首先,你可能不应该在 Lua 中从 0 开始索引。它的库例程假定基于 1 的索引,所以这最终会咬你。其次,这是一种非常低效的绘制条形图的方法。如果它看起来像一个典型的健康条、进度条等,你可以有两张图像:一张是完整的条减去右边缘,一张是右边缘。例如,如果您的条形图像宽度为 100 像素且燃料为 10%,则渲染条形的 10 个像素,然后将右边缘图像放置在 x 偏移 10 - rightEdgeImage.width 处。有很多方法可以做到这一点,不需要 30 张图片。
  • 很好的答案和很好的反思!真的很有帮助!

标签: lua coronasdk


【解决方案1】:

不要使用单个图像,而是使用精灵表。这可能会提升游戏效果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    • 2023-03-28
    • 2016-03-17
    • 2015-02-07
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多