Button Event Listening: Best Practice?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 -- Touch function
local function touchButton(event)
print("Button pressed!")
end
 
-- Button function
function labelButton(params)
--
-- Imagine various doses of display.newImage and display.newText
--
end
 
-- Generate the button
local myAwesomeButton = labelButton{ x=200,y=200 }

I put together a video/sample code that may help. It's called Better Buttons in Corona SDK and you can find it here:

http://gamedevnation.com/gdn-membership/better-buttons-in-corona-sdk/

That article is "members only" content so you'd have to join Game Dev Nation to see it, but membership is free so don't let that stop you. :)

You said...


At first I tried... myAwesomeButton:addEventListener("touch", touchButton)... but Corona choked on adding a listener to a function instead of an image. So I guess I'm approaching this the wrong way.

...but your code snippet doesn't look wrong -- the second parameter of addEventListener() *is* the name of a function, so whatever Corona SDK was choking on probably wasn't that.

What might be happening is that you're probably not returning a display object from labelButton(), so myAwesomeButton isn't really a display object.

Not sure we can tell what the actual problem is without more code, but hope the above helps.

Jay

This would work well :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- Touch function
local function touchButton(event)
    if event.phase == "began" then
        print("Button pressed!")
    end
end
 
-- Button function
function labelButton(params)
--
-- Imagine various doses of display.newImage and display.newText
--
end
 
-- Generate the button
local myAwesomeButton = labelButton{ x=200,y=200 }
myAwesomeButton:addEventListener("touch", touchButton)

Thanks, JA and Danny.

JA: I get what you're saying, but in the situation I was assuming it was choking because the addEventListener kept complaining that myAwesomeButton was nil. Is there anything special to return a display object?

(I really need to read a doc on how return is useful for some of these things, can't say I've used it more than once or twice.)

I'll give that video a look tonight! :)

Danny, does labelButton() have to return something specific for that to work?

You're adding an event listener to myAwesomeButton which might not be a display object - depending on what labelButton returns, right?

Or am I misunderstanding/overlooking something?

Jay

Hey Jay.

I presumed the thread creator created a function to create buttons that way. If not it wouldn't work of course.

@richard9, you have to have *something* to add an event listener to.

The way you'd pass back an object could be something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
local function weGotTouched(e)
  if e.phase == "ended" then
    print("touchy!")
  end
end
 
local function makeMyButton(imgpath, x, y)
  local btn = display.newImage(imgpath)
  btn.x = x
  btn.y = y
  btn.foo = bar -- just something else to add as a property
 
  return btn
end
 
local myAwesomeButton = makeMyButton("images/menubtn.png", 150, 175)
 
myAwesomeButton:addEventListener("touch", weGotTouched)

Hey Jay,

1. Yeah, returning the displaygroup did the trick wonderfully. It's going to take awhile to wrap my brain around what should return stuff, but thanks.

What I'm trying to grasp now is that I see a lot of function lua files out there that use an "onRelease" parameter, like so:

1
2
3
4
5
function dosomething(event)
-- (stuff)
end
 
myAwesomeButton{x=200,y=200,onRelease=dosomething}
views:1627 update:2011/10/11 8:57:46
corona forums © 2003-2011