0

我不知道为什么我的按钮在按下时不会改变外观,谁能告诉我以下代码段是否有问题。我正在使用 Corona SDK

这是我完整的 main.lua,如果我删除事件侦听器按钮交换工作。

display.setStatusBar(display.HiddenStatusBar);

local textObj = display.newRetinaText("Click and see what happens!", 40, 40, nil, 0)
textObj:setTextColor(255,0,0);

local widget = require "widget";

local button = widget.newButton{
    default = "Button.png",
    over = "ButtonClicked.png",
    onPress = button.touch;
      }

button.x = display.contentCenterX;
button.y = display.contentCenterY+200;

local function touch(e)
if(e.phase == "began")then
        textObj.text = "Clicked!";
        textObj:setTextColor(255,255,255);
elseif(e.phase == "ended") then
        textObj.text = "Released!";
        textObj:setTextColor(255,0,0);
    end
end

现在我只能看到文字,而不是按钮!

4

2 回答 2

0

I've tested your code and button is changing when clicked. The only reason I can come up with is that the ButtonClicked.png is missing. Check the console (window named Corona Simulator Output) for any errors.

Also, cover the bases: Make sure you are running the same main.lua you are editing.

After edit:

I believe you are overriding default button behaviour with

button:addEventListener("touch", button);

Try using constructor parameter onPress.

local button = widget.newButton{
    default = "Button.png",
    over = "ButtonClicked.png",
    onPress = touch
}
于 2013-02-21T15:03:13.307 回答
0

好的,我解决了!我是这样做的。

local button = widget.newButton{
default = "Button.png",
over = "ButtonClicked.png",

}
button.x = display.contentCenterX;
button.y = display.contentCenterY+200;


function button:tap( e )
-- Do what you want to do when event occurs
end

button:addEventListener( "tap", button );
于 2013-02-22T09:32:21.773 回答