buttons driving me crazY!!!!

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]

Hello Sean!

Without looking too detailed into your example code (my family may wakeup every moment and I may have to stop writing then), just let me give you the following explanations:

1: think of “function” definitions as commands (better: expressions), which produce a function - this is in contrast to other languages (such as JavaScript/ActionScript, f.e.) where you may define functions wherever you like (and those functions will be available from the very beginning of your script/block/function etc.) This means: a function will not be available before you have executed the corresponding function expression!

Therefore: just define your eventhandler definitions *before* you define everything else which refers to it - this should already solve your problem

2: a “function” is able to access (local) variables of the “chunk” (this is a Lua term, it’s either the main script or a function) within which it has been created (this feature leads to the concept of “closures” similar to JavaScript/ActionScript) If you are not familiar with that functionality, please ignore it for the moment (i.e. please, do NOT access local variables of a function’s outer chunk as that may lead to problems which may be hard to debug!) But, if you know how to handle Lua “upvalues”, you may use them to write ONE single (generic) button event handler and create specific handlers for every button you have just by using a local variable to refer to the “target” butten and evaluating the function expression (which internally uses that outer local variable)

Without advertising it too much herein: albeit it’s really easy to start programming in Lua, do not understimate its potential!!! Lua’s “metamechanisms” have been carefully chosen during a process which took years(!) and is still going on. As a consequence, you will have to gain a lot of experience in Lua programming in order to benefit from its enormous built-in power. To speed up the process of learning the (not necessarily appearant) details, I recommend reading the book “Programming in Lua” (just visit the Lua web site for details) and (afterwards), perhaps, even “Lua Gems”. They will help you understanding the concepts behind and the potential of Lua (there are good reasons, why Lua is quite popular in the game industry - there, it’s often used to implement AI features)

Hopefully this explanation will help a bit…

Kind regards,

Andreas Rozek [import]uid: 4331 topic_id: 557 reply_id: 1094[/import]