sprite as a button

Hello. Thank you for reading.

Can anyone please give me a solid way of using a sprite sheet as a button?

I already tried to create the sprite which works, then add  -

myDispObj:addEventListener ( “tap”, myFunction )

I am encapsulating this within the storyboard api, so when user taps myDispObj, myFunction does this -

local function myFunction(event)
    
    storyboard.gotoScene ( “level”, {effect = “slideUp” } )
return true
end

 

So far, the above does not work. I just want a practical sample that uses a sprite as a button within the storyboard api.

Thank you for any help.

Wanted to add everything works as far as storyboard module, sprite sheets, other code, etc., when I comment out this -

myDispObj:addEventListener ( “tap”, myFunction )

If I include the above line though I get errors…

Which comes first in your code:   local function myFunction() or myDispObj:addEventListener()?

This sounds like a scope issue.

local function myFunction()

can you post your code. Also what is the error message you are getting?

Wanted to add everything works as far as storyboard module, sprite sheets, other code, etc., when I comment out this -

myDispObj:addEventListener ( “tap”, myFunction )

If I include the above line though I get errors…

Which comes first in your code:   local function myFunction() or myDispObj:addEventListener()?

This sounds like a scope issue.

local function myFunction()

can you post your code. Also what is the error message you are getting?

I had the same issue, but I deleted the ‘local’ infront of the sprite variable, and it fixes it. Will this be a problem later in the program or create ram issues, seeing as it is now not a local variable?

Hi @jumbub1,

I suggest that you make this a local variable again, and confirm why the scope was not working when it was previously local. Global variables and functions should be avoided unless truly necessary. If you’d like to view some video tutorials on this, please check out what we have available in Corona University on the website:

http://www.coronalabs.com/resources/tutorials/corona-basics/

Best regards,

Brent Sorrentino

I had the same issue, but I deleted the ‘local’ infront of the sprite variable, and it fixes it. Will this be a problem later in the program or create ram issues, seeing as it is now not a local variable?

Hi @jumbub1,

I suggest that you make this a local variable again, and confirm why the scope was not working when it was previously local. Global variables and functions should be avoided unless truly necessary. If you’d like to view some video tutorials on this, please check out what we have available in Corona University on the website:

http://www.coronalabs.com/resources/tutorials/corona-basics/

Best regards,

Brent Sorrentino