尝试索引全局小部件(零值)

时间:2014-09-01 12:40:30

标签: lua corona

当我运行此代码时,我收到错误消息

  

尝试索引全局widgetnil值)

local inc = 0.0
local levelUpBar, player
levelUpBar = widget.newProgressView {
   left = display.contentWidth*0.5-100,
   top = 300,
   width = 200,
   isAnimated = true }
levelUpBar:setProgress( 0 )  


local function playerHit(event)
    if(event.phase == "began") then
        inc = inc + 0.2
        if(inc > 1) then
            print('level up!')
        else
            levelUpBar:setProgress( inc )
        end
    end
end

player = display.newCircle(0,0,75)
player.x = display.contentWidth*0.5
player.y = 200
player:setFillColor(0,200,0)
player:addEventListener("touch", playerHit)

1 个答案:

答案 0 :(得分:2)

您需要添加

local widget = require("widget");

位于代码顶部