It should be simple, right? All I want is a simple splash screen with five buttons. Four of the buttons *should* be able to call the same listener function.
But button problems and this lua stuff about which things must come first is really driving me nuts.
Okay. First problem.
My experimentation seems to show that declaring an id or name when you declare a button only works if you also declare the listener function at the same time. So, while this works:
local button3 = ui.newButton{
default = “buttonGray.png”,
over = “buttonBlue.png”,
onEvent = buttonHandler,
id = “button3”,
text = “Button 3 Label”,
font = “MarkerFelt-Thin”,
size = 28,
emboss = true
}
local buttonHandler = function( event )
t:setText( "id = " … event.id … ", phase = " … event.phase )
end
print("id = " … event.id)
This doesn’t:
local button3 = ui.newButton{
default = “buttonGray.png”,
over = “buttonBlue.png”,
id = “button3”,
text = “Button 3 Label”,
font = “MarkerFelt-Thin”,
size = 28,
emboss = true
}
button3:addEventListener(“touch”, buttonHandler)
print("id = " … event.id)
It produces a runtime error with event.id as a nil value. Makes no sense to me, but in playing with the buttons example in the sample code, that’s what seems to happen.
Now, actually, I lied. The first example *doesn’t* work. For it to actually work, you must move the event handler function to someplace above the button declaration code. If you don’t, then you can press the button all you want and nothing will happen.
So, I said, fine, I’ll just do it with a lot of repetitive code, shown below. I’ve left out a lot of stuff to keep this shorter – button attributes are there, handlers aren’t exactly the same, etc.
================================================
menuScreen = display.newGroup()
local button1 = ui.newButton{
}
local button2 = ui.newButton{
}
local button3 = ui.newButton{
}
local button4 = ui.newButton{
}
local function button1_event_handler(event)
button1.isVisible = false
button2.isVisible = false
button3.isVisible = false
button4.isVisible = false
menuScreen:removeEventListener( “touch”, button1_event_handler )
menuScreen:removeEventListener( “touch”, button2_event_handler )
menuScreen:removeEventListener( “touch”, button3_event_handler )
menuScreen:removeEventListener( “touch”, button4_event_handler )
menuScreen:remove(background)
end
local function button2_event_handler(event)
button1.isVisible = false
button2.isVisible = false
button3.isVisible = false
button4.isVisible = false
menuScreen:removeEventListener( “touch”, button1_event_handler )
menuScreen:removeEventListener( “touch”, button2_event_handler )
menuScreen:removeEventListener( “touch”, button3_event_handler )
menuScreen:removeEventListener( “touch”, button4_event_handler )
menuScreen:remove(background)
end
local function button3_event_handler(event)
button1.isVisible = false
button2.isVisible = false
button3.isVisible = false
button4.isVisible = false
menuScreen:removeEventListener( “touch”, button1_event_handler )
menuScreen:removeEventListener( “touch”, button2_event_handler )
menuScreen:removeEventListener( “touch”, button3_event_handler )
menuScreen:removeEventListener( “touch”, button4_event_handler )
menuScreen:remove(background)
end
local function button4_event_handler(event)
button1.isVisible = false
button2.isVisible = false
button3.isVisible = false
button4.isVisible = false
menuScreen:removeEventListener( “touch”, button1_event_handler )
menuScreen:removeEventListener( “touch”, button2_event_handler )
menuScreen:removeEventListener( “touch”, button3_event_handler )
menuScreen:removeEventListener( “touch”, button4_event_handler )
menuScreen:remove(background)
end
button1:addEventListener(“touch”, button1_event_handler)
button2:addEventListener(“touch”, button2_event_handler)
button3:addEventListener(“touch”, button3_event_handler)
button4:addEventListener(“touch”, button4_event_handler)
Now it mostly, kinda, works.
Except for one thing. Since button4_event_handler() happens after button3_event_handler(), there’s an error (a bad one, btw – bus error and simulator goes away) if I try to run that code as is.
I didn’t test the other two handlers, but I’d guess that they have the problem, only more so…
So, is there a way to get an event.id and still use addeventlistener? Is it a bug that it doesn’t work currently?
How do I determine what order things need to be in?
Thanks,
Sean.
[import]uid: 4993 topic_id: 557 reply_id: 300557[/import]