button event error

Alright sweet! That means it should be working :slight_smile: Glad I could help [import]uid: 14461 topic_id: 29686 reply_id: 119755[/import]

Hi,

it’s work but function “storyboard.gotoScene” can’t use [import]uid: 169163 topic_id: 29686 reply_id: 119763[/import]

Do you get an error? How exactly is it not working?
We need a little more information I think.
[import]uid: 156990 topic_id: 29686 reply_id: 119766[/import]

Hi,

you can download Code here http://www.mediafire.com/?pny0z696lz4higp

i can’t fix it ToT [import]uid: 169163 topic_id: 29686 reply_id: 119829[/import]

Wow…I’m totally stumped. There aren’t any errors and your code looks fine, it just doesn’t go to the next scene. I actually don’t use storyBoard that much so I’m probably not the best help. If you solve this please share the answer, I’m really interested now! :smiley: [import]uid: 14461 topic_id: 29686 reply_id: 120225[/import]

fixing it right now … Hold on! Easy one [import]uid: 98393 topic_id: 29686 reply_id: 120242[/import]

HAH! There is so much wrong with your code, I don’t know where to start :smiley:

Learn Scoping. It’s essential. Your code structure is quite a mess (for me)
http://lua-users.org/wiki/ScopeTutorial
http://www.coronalabs.com/blog/2011/09/21/tutorial-scopes-for-functions/

You won’t have acces to local display objects declared in functions if there are no forward declarations.

in level3.lua you declared [lua] local screenGroup = display.newGroup() [/lua] at the top, but you don’t remove the group when going back to menu as you completely forgo the storyboard template.
http://developer.coronalabs.com/reference/index/scene-template
There are is no exit scene function in your level3.lua code and you forgot the event listener for the storyboard functions. Additionally you didn’t “return scene” at the end so there’s no way to access it via storyboard anymore.

Further hints (from what i saw a first glance):

if obj then  
 obj:removeSelf()   
 playBtn = nil  
end  

same as

display.remove(obj)  
obj = nil  
  • you don’t need to remove listeners manually if you’re using widget library (menu.lua)

  • do not use “storyboard.newScene()” in “onPlayBtnRelease()” , it only used to create new scene objects in a new lua file

  • no storyboard.loadScene as it only loads a specified scene, without initiating a scene transition. This function is similar to storyboard.gotoScene(), but will not change the currently active scene.
    -> use storyboard.gotoScene() instead!

  • why do you use widget and ui library for buttons? stick to one, i prefer supported widget library

  • [lua]“backBtnImage.touch = onObjectTouch”[/lua] what’s that for?

  • [lua]backBtnImage:addEventListener( “touch”, backMainMenu )[/lua]
    instead of
    [lua]backBtnImage:addEventListener( “backMainMenu”, backBtnImage )[/lua]

here’s my partial solution. I hope you get the idea. Fill in the rest of your functions in level3.lua.

-----------------------------------------------------------------------------------------  
--  
-- menu.lua  
--  
-----------------------------------------------------------------------------------------  
  
local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
  
-- include Corona's "widget" library  
local widget = require "widget"  
  
--------------------------------------------  
  
-- forward declarations and other locals  
local playBtn  
  
-- 'onRelease' event listener for playBtn  
local function onPlayBtnRelease()  
  
 storyboard.gotoScene("level3", "fade", 500)  
  
 return true -- indicates successful touch  
end  
  
-----------------------------------------------------------------------------------------  
-- BEGINNING OF YOUR IMPLEMENTATION  
--   
-- NOTE: Code outside of listener functions (below) will only be executed once,  
-- unless storyboard.removeScene() is called.  
--   
-----------------------------------------------------------------------------------------  
  
-- Called when the scene's view does not exist:  
function scene:createScene( event )  
 local group = self.view  
  
 -- display a background image  
 local background = display.newImageRect( "picMain/Title\_Game.png", display.contentWidth, display.contentHeight )  
 background:setReferencePoint( display.TopLeftReferencePoint )  
 background.x, background.y = 0, 0  
  
  
 -- create a widget button (which will loads level1.lua on release)  
 playBtn = widget.newButton{  
 --label="Play Now",  
 --labelColor = { default={255}, over={128} },  
 default="picMain/btn2.png",  
 over="picMain/btn2-1.png",  
 width=182, height=173,  
 onRelease = onPlayBtnRelease -- event listener function  
 }  
 playBtn:setReferencePoint( display.CenterReferencePoint )  
 playBtn.x = display.contentWidth\*0.5  
 playBtn.y = display.contentHeight - 125  
  
 -- all display objects must be inserted into group  
 group:insert( background )  
 group:insert( playBtn )  
end  
  
-- Called immediately after scene has moved onscreen:  
function scene:enterScene( event )  
 local group = self.view  
 storyboard.removeAll() -- -- you don't need this actually, it's just my thing if game get very huuuge  
  
 -- INSERT code here (e.g. start timers, load audio, start listeners, etc.)  
  
end  
  
-- Called when scene is about to move offscreen:  
function scene:exitScene( event )  
 local group = self.view  
  
  
 -- INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)  
  
end  
  
-- If scene's view is removed, scene:destroyScene() will be called just prior to:  
function scene:destroyScene( event )  
 local group = self.view  
  
  
end  
  
-----------------------------------------------------------------------------------------  
-- END OF YOUR IMPLEMENTATION  
-----------------------------------------------------------------------------------------  
  
-- "createScene" event is dispatched if scene's view does not exist  
scene:addEventListener( "createScene", scene )  
  
-- "enterScene" event is dispatched whenever scene transition has finished  
scene:addEventListener( "enterScene", scene )  
  
-- "exitScene" event is dispatched whenever before next scene's transition begins  
scene:addEventListener( "exitScene", scene )  
  
-- "destroyScene" event is dispatched before view is unloaded, which can be  
-- automatically unloaded in low memory situations, or explicitly via a call to  
-- storyboard.purgeScene() or storyboard.removeScene().  
scene:addEventListener( "destroyScene", scene )  
  
-----------------------------------------------------------------------------------------  
  
return scene  
-----------------------------------------------------------------------------------------  
--  
-- level3.lua  
--  
-----------------------------------------------------------------------------------------  
local storyboard = require "storyboard"  
local scene = storyboard.newScene()  
  
local backBtnImage  
  
local backMainMenu = function ( event )   
 storyboard.gotoScene( "menu", "fade", 500)   
 return true  
end  
function scene:createScene( event )  
 local group = self.view  
  
 backBtnImage = display.newImageRect( "picMain/btn\_menu.png", 275, 100 )  
 backBtnImage.x = display.contentWidth\*0.5 - 130 ; backBtnImage.y = display.contentHeight / 2 +200--465   
  
 group:insert( backBtnImage )   
end  
  
-- Called immediately after scene has moved onscreen:  
function scene:enterScene( event )  
 local group = self.view  
 backBtnImage:addEventListener( "touch", backMainMenu )  
  
 storyboard.removeAll() -- you don't need this actually, it's just my thing if game get very huuuge  
end  
  
-- Called when scene is about to move offscreen:  
function scene:exitScene( event )  
 local group = self.view  
 backBtnImage:removeEventListener( "backMainMenu", backBtnImage )  
  
  
  
 -- INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)  
  
end  
  
-- "createScene" event is dispatched if scene's view does not exist  
scene:addEventListener( "createScene", scene )  
  
-- "enterScene" event is dispatched whenever scene transition has finished  
scene:addEventListener( "enterScene", scene )  
  
-- "exitScene" event is dispatched whenever before next scene's transition begins  
scene:addEventListener( "exitScene", scene )  
return scene  

[import]uid: 98393 topic_id: 29686 reply_id: 120247[/import]

thanks Paul_G

i can fix it now [import]uid: 169163 topic_id: 29686 reply_id: 120439[/import]