I can't figure out why my button won't change appearance when pressed, can anyone tell me if there is something wrong with the following snippet. I am using Corona SDK
This is my complete main.lua, if i remove the event listener the buttonswap works.
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
Now i can only see the text, and not the button!
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
}
Ok i solved it! Here's how i did it.
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 );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.