HAH! There is so much wrong with your code, I don’t know where to start 
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]