尝试索引本地“按钮”(零值)

时间:2013-06-19 10:22:05

标签: lua corona

我正在阅读以下教程 http://www.youtube.com/watch?v=Y5Oz72MO4JI

但由于某些原因我收到了错误Attempt to index local 'button' (a nil value) 我已经在我的代码中重新检查了所有内容,并且它在视频中正确接缝

完整错误:

ui.lua:130 Attempt to index local 'button' (a nil value)
stack traceback:
[C]:?

ui.lua:130 in function 'newButton'

代码:

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
local ui = require("ui")
local background = display.newImage("background.jpg")
local gear = display.newImage("gear.png")
local gear2 = display.newImage("gear.png")

gear.x = display.stageWidth / 2 + 26
gear.y = display.stageHeight / 2 - 50
gear2.x = display.stageWidth * .63
gear2.y = display.stageHeight * .60

local rotateAmount = 1

local function animate( event )
    gear.rotation = gear.rotation + rotateAmount
    gear2.rotation = gear2.rotation - rotateAmount
end

Runtime:addEventListener( "enterFrame", animate )

local buttonHandler = function( event )
    if event.phase == "release" then
        if event.id == "stop" then
            rotateAmount = 0
        elseif event.id == "+1" then
            rotateAmount = rotateAmount + 1
        elseif event.id == "-1" then
            rotateAmount = rotateAmount - 1
        elseif event.id == "reverse" then
            rotateAmount = rotateAmount * -1
        end
    end
end

local button1 = ui.newButton{
    default = "button.png",
    over = "buttonOver.png",
    onEvent = buttonHandler,
    text = "Stop",
    id = "stop",
    size = 12,
    emboss = true
}

1 个答案:

答案 0 :(得分:0)

这是一个旧图书馆。您应该使用新的Corona小部件。

local widget = require( "widget" )

local myButton = widget.newButton
{
   left = 10,
   top = 80,
   label = "Default",
   labelAlign = "center",
   font = "Arial",
   fontSize = 18,
   labelColor = { default = {0,0,0}, over = {255,255,255} },
   onEvent = onButtonEvent
}

更多信息:http://www.coronalabs.com/blog/2013/02/26/new-widgets-part-2/