What I am doing wrong with this button?

Hello! I am a new user of corona sdk and also this is my first post here! So I have a question.

What I am doing wrong here? Do I miss something?
It seems I can’t get this button working. It loads a image file, and thats it. The button needs to change the scene to different one.

local director = require ("director")  
  
--\> Add menu button  
local button = display.newImage( "resources/buttonMenuExit.png" )  
button.x = display.contentWidth/14  
button.y = display.contentHeight/1.1  
  
function button:touch( event )  
 director:changeScene ("levelOne")   
end  
   
button:addEventListener( "touch", button )  

Also, another question.
How do you destroy audio stream (media.playSound) when you get into different scene.

Can you please help me? Thanks!!! [import]uid: 64009 topic_id: 13071 reply_id: 313071[/import]

Hello and welcome to the Corona community!
Your button function should look like this.
[lua]_W = display.contentWidth
_H = display.contentHeight

local function Button( event )
local phase = event.phase
if “began” == phase then
director:changeScene (“levelOne”)
end
end

button = display.newRect( 0, 0, _W, _H)
button.x = _W * 0.5;
button.y = _H * 0.5;
button:setFillColor(0, 0, 0, 0)
button:addEventListener( “touch”, Button )[/lua]
For your second question you can use this. [import]uid: 17138 topic_id: 13071 reply_id: 48025[/import]

Thanks for quick response! It looks like it’s still ignoring me. Maybe it has something to do with deleteMe function? This time I will post a full code.

--\> Loading director  
local director = require ("director")  
  
\_W = display.contentWidth  
\_H = display.contentHeight  
  
--\> Adding physics engine  
local physics = require("physics")  
physics.start()  
--\> Set gravity to act down  
physics.setGravity(0, 9.8)  
  
--\> Hide status bar on display  
display.setStatusBar( display.HiddenStatusBar )  
  
--\> Add background image  
local background = display.newImage("backgrounds/background\_01\_01.png")  
  
--\> Add pillow image  
local pillow = display.newImage("resources/pillow.png")  
pillow.x = display.contentWidth/1.95  
pillow.y = display.contentHeight/1.12  
---\> Turn pillow into physics body  
physics.addBody(pillow, "static", { bounce = 0.2} )  
  
--\> Add piggy image  
local piggy = display.newImage("resources/piggy\_player.png")  
piggy.x = display.contentWidth/2  
piggy.y = display.contentHeight/8  
---\> Turn piggy into physics body  
physics.addBody(piggy, { bounce = 0.2})  
  
--\> Add candy01 image  
local candy01 = display.newImage("game\_candy/candy02.png")  
candy01.x = display.contentWidth/2  
candy01.y = display.contentHeight/3.2  
--\> Turn candy01 into physics body  
physics.addBody(candy01, "static", { bounce = 0.1})  
  
--\> Add tut01 image  
local tut01 = display.newImage("resources/tut01.png")  
tut01.x = display.contentWidth/1.285  
tut01.y = display.contentHeight/2.35  
  
--\> Add tut02 image  
local tut02 = display.newImage("resources/tut02.png")  
tut02.x = display.contentWidth/5  
tut02.y = display.contentHeight/1.34  
  
--\> Add tut03 image  
local tut03 = display.newImage("resources/tut03.png")  
tut03.x = display.contentWidth/1.305  
tut03.y = display.contentHeight/1.34  
  
--\> Add object\_01\_01(ice-cream) image  
local object\_01\_01 = display.newImage("game\_objects/object\_01\_01.png")  
object\_01\_01.x = display.contentWidth/2  
object\_01\_01.y = display.contentHeight/1.33  
  
--\> Add level1\_headerTitle image  
local level1\_headerTitle = display.newImage("resources/level1\_headerTitle.png")  
level1\_headerTitle.x = display.contentWidth/11  
level1\_headerTitle.y = display.contentHeight/19  
  
--\> Add menu button  
local button = display.newImage( "resources/buttonMenuExit.png" )  
button.x = display.contentWidth/14  
button.y = display.contentHeight/1.1  
  
function Button(event)  
 local phase = event.phase  
 if "began" == phase then  
 director:changeScene ("levelOne")   
 end  
end   
  
button = display.newRect( 0, 0, \_W, \_H)  
button.x = \_W \* 0.5;  
button.y = \_H \* 0.5;  
button:setFillColor(0, 0, 0, 0)  
button:addEventListener( "touch", Button )  
  
--\> Function that calls destruction on click  
function deleteMe(event)  
 local targetObj = event.target  
 targetObj:removeSelf()  
end  
  
--\> Objects that needs to be destroyed on click, add here  
candy01:addEventListener("touch", deleteMe)  
tut01:addEventListener("touch", deleteMe)  
tut02:addEventListener("touch", deleteMe)  
tut03:addEventListener("touch", deleteMe)  
  

Thanks! [import]uid: 64009 topic_id: 13071 reply_id: 48028[/import]

I’m not fully understanding what you are trying to accomplish with your code. Do you want to remove all the display objects when the button is pressed? [import]uid: 17138 topic_id: 13071 reply_id: 48029[/import]

No. Thats two different things (two different functions). One is for removing images from screen by click(candy01, tut01, tut02 and tut03) and one is for button!
[import]uid: 64009 topic_id: 13071 reply_id: 48032[/import]

Oh I see. Are you getting any errors? Are you using the Director correctly? Use this to see if you are registering a touch, it will print something in your terminal.
[lua]function Button(event)
local phase = event.phase
if “began” == phase then
print(“Someone touched me!”)
director:changeScene (“levelOne”)
end
end [/lua]
This is a better way to write your deleteMe function because in your function it removes the object twice, once when you begin the touch and once again when you end the touch.
[lua]local function deleteMe(event)
local targetObj = event.target
if event.phase == “began” then
targetObj:removeSelf()
end
end
[import]uid: 17138 topic_id: 13071 reply_id: 48035[/import]

Yes, it gives me errors. As expected :smiley:
But they aren’t linked to this problem!

In console it prints “Someone touched me!” :smiley:

P.S. Thanks for that optimization code!

In case, here are errors:

[code]
Runtime error
…sers/xiix/Desktop/Piggy_adventures/game/director.lua:310: attempt to index a boolean value
stack traceback:
[C]: ?
…sers/xiix/Desktop/********/game/director.lua:310: in function ‘loadScene’
…sers/xiix/Desktop/********/game/director.lua:639: in function ‘changeScene’
/Users/xiix/Desktop/*******/game/page_1.lua:88: in function ‘onEvent’
/Users/xiix/Desktop/*******/game/ui.lua:107: in function
?: in function <?:214>
Runtime error
…sers/xiix/Desktop/*******/game/director.lua:345: bad argument #-2 to ‘insert’ (Proxy expected, got nil)
stack traceback:
[C]: ?
[C]: in function ‘insert’
…sers/xiix/Desktop/*******/game/director.lua:345: in function ‘_listener’
?: in function <?:441>
?: in function <?:214>

[/code] [import]uid: 64009 topic_id: 13071 reply_id: 48040[/import]

The errors are all coming from the director. You aren’t using it correctly here is a link to the video tutorial.
Your main.lua file should look like this
[lua]local director = require(“director”);

local mainGroup = display.newGroup();

local function main()

mainGroup:insert(director.directorView);
–Whatever screen you want it to change to.
director:changeScene("");

return true;
end

main()[/lua]
And you other file should look like this
[lua]module(…, package.seeall)

_W = display.contentWidth
_H = display.contentHeight

–> Adding physics engine
local physics = require(“physics”)
physics.start()
–> Set gravity to act down
physics.setGravity(0, 9.8)

–> Hide status bar on display
display.setStatusBar( display.HiddenStatusBar )

function new()

local localGroup = display.newGroup();

–> Add background image
local background = display.newImage(“backgrounds/background_01_01.png”)
localGroup:insert(background)

–> Add pillow image
local pillow = display.newImage(“resources/pillow.png”)
pillow.x = display.contentWidth/1.95
pillow.y = display.contentHeight/1.12
localGroup:insert(pillow)
—> Turn pillow into physics body
physics.addBody(pillow, “static”, { bounce = 0.2} )

–> Add piggy image
local piggy = display.newImage(“resources/piggy_player.png”)
piggy.x = display.contentWidth/2
piggy.y = display.contentHeight/8
localGroup:insert(piggy)
—> Turn piggy into physics body
physics.addBody(piggy, { bounce = 0.2})

–> Add candy01 image
local candy01 = display.newImage(“game_candy/candy02.png”)
candy01.x = display.contentWidth/2
candy01.y = display.contentHeight/3.2
localGroup:insert(candy01)
–> Turn candy01 into physics body
physics.addBody(candy01, “static”, { bounce = 0.1})

–> Add tut01 image
local tut01 = display.newImage(“resources/tut01.png”)
tut01.x = display.contentWidth/1.285
tut01.y = display.contentHeight/2.35
localGroup:insert(tut01)

–> Add tut02 image
local tut02 = display.newImage(“resources/tut02.png”)
tut02.x = display.contentWidth/5
tut02.y = display.contentHeight/1.34
localGroup:insert(tut02)

–> Add tut03 image
local tut03 = display.newImage(“resources/tut03.png”)
tut03.x = display.contentWidth/1.305
tut03.y = display.contentHeight/1.34
localGroup:insert(tut03)

–> Add object_01_01(ice-cream) image
local object_01_01 = display.newImage(“game_objects/object_01_01.png”)
object_01_01.x = display.contentWidth/2
object_01_01.y = display.contentHeight/1.33
localGroup:insert(object_01_01)

–> Add level1_headerTitle image
local level1_headerTitle = display.newImage(“resources/level1_headerTitle.png”)
level1_headerTitle.x = display.contentWidth/11
level1_headerTitle.y = display.contentHeight/19
localGroup:insert(level1_headerTitle)

–> Add menu button
local button = display.newImage( “resources/buttonMenuExit.png” )
button.x = display.contentWidth/14
button.y = display.contentHeight/1.1
localGroup:insert(button)

function Button(event)
local phase = event.phase
if “began” == phase then
director:changeScene (“levelOne”)
end
end

button = display.newRect( 0, 0, _W, _H)
button.x = _W * 0.5;
button.y = _H * 0.5;
button:setFillColor(0, 0, 0, 0)
localGroup:insert(button)
button:addEventListener( “touch”, Button )

–> Function that calls destruction on click
function deleteMe(event)
local targetObj = event.target
targetObj:removeSelf()
end

–> Objects that needs to be destroyed on click, add here
candy01:addEventListener(“touch”, deleteMe)
tut01:addEventListener(“touch”, deleteMe)
tut02:addEventListener(“touch”, deleteMe)
tut03:addEventListener(“touch”, deleteMe)

return localGroup
end[/lua]
Make sure all your other files that you are going to use with the Director look like that.
Also,you have two display objects called buttons, so either take one out or change its name. [import]uid: 17138 topic_id: 13071 reply_id: 48083[/import]

Thanks! That worked!!! So I learned a lesson. I had to include mainGroup:insert(director.directorView);

Please, can you answer another question? How do you use audio.dispose() function, when going to another scene? Thank you very much! [import]uid: 64009 topic_id: 13071 reply_id: 48092[/import]

What I do is i call a function to clean up everything when I am switching to a new screen. For your case it would look like this.

[lua]function Button(event)
local phase = event.phase
if “began” == phase then
director:changeScene (“levelOne”)
cleanUp()
end
end

button = display.newRect( 0, 0, _W, _H)
button.x = _W * 0.5;
button.y = _H * 0.5;
button:setFillColor(0, 0, 0, 0)
localGroup:insert(button)
button:addEventListener( “touch”, Button )

–Say you also have a sound playing
laserSound = audio.loadSound(“laserBlast.wav”)
audio.play( laserSound )

function cleanUp()
display.remove(localGroup) – Removes everything inserted into the group localGroup
localGroup = nil
button:removeEventListener( “touch”, Button ) – Remove Event Listener
candy01:removeEventListener(“touch”, deleteMe)-- Remove Event Listener
tut01:removeEventListener(“touch”, deleteMe)-- Remove Event Listener
tut02:removeEventListener(“touch”, deleteMe)-- Remove Event Listener
tut03:removeEventListener(“touch”, deleteMe)-- Remove Event Listener
audio.dispose( laserSound ) – This disposes laserSound
laserSound = nil – This makes sure we can’t use the handle again
end[/lua]
That should work, but there might be typos and I didn’t test the code. Let me know if there are any problems with it. [import]uid: 17138 topic_id: 13071 reply_id: 48094[/import]

Now I can finish my first game! Thank you very much!!! [import]uid: 64009 topic_id: 13071 reply_id: 48097[/import]

No problem! Glad I could help. [import]uid: 17138 topic_id: 13071 reply_id: 48099[/import]