Hello,
I am learning Corona so I can use it at work.
I am trying just to make a quick thing to help me with what I know I am going to be doing for work later on.
I want to use buttonEvents instead of the regular buttons.
The issue I am having comes when I am changing screens, to a different scene.
the button events, and my display system clock will not be removed.
I made a regular button and it will remove.
I tried groups, and also button removal. I am at a loss.
Can some one please look at the following and help me out?
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
local widget = require( “widget” )
local buttonDevicesScene = function( event )
storyboard.gotoScene( “Devices” )
end
local buttonToolsScene = function( event )
storyboard.gotoScene( “Tools” )
end
local buttonUPScene = function( event )
end
local buttonRightScene = function( event )
end
local buttonDownScene = function( event )
end
local buttonLeftScene = function( event )
end
local buttonOffScene = function( event )
end
– NOTE:
–
– Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.
– local forward references should go here –
– BEGINNING OF YOUR IMPLEMENTATION
– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view
-----------------------------------------------------------------------------
– CREATE display objects and add them to ‘group’ here.
– Example use-case: Restore ‘group’ from previously saved state.
-----------------------------------------------------------------------------
– create embossed text
–[[ local title = display.newText( “One”, 0, 0, native.systemFont, 40 )
title.X = display.contentCenterX
title.anchorY = .5
title:setTextColor( 255 )
title.x = 160
title.y = title.y
--]]
– Clock
print( os.clock() ) – outputs CPU time to terminal
– Displays the Clock
title = display.newText( os.date( “%X” ), 0, 160, “Times New Roman”, 40 )
title.x = display.contentWidth/2 – center title
title:setTextColor( 255,255,255 )
title.y = 200
– Buttons
– Of course, buttons don’t always have labels
local buttonUp = widget.newButton
{
defaultFile = “up.png”,
--overFile = “up.png”,
onEvent = buttonUPScene,
}
local buttonRight = widget.newButton
{
defaultFile = “right.png”,
--overFile = “right.png”,
onEvent = buttonRightScene,
}
local buttonDown = widget.newButton
{
defaultFile = “down.png”,
--overFile = “down.png”,
onEvent = buttonDownScene,
}
local buttonLeft = widget.newButton
{
defaultFile = “left.png”,
--overFile = “left.png”,
onEvent = buttonLeftScene,
}
local buttonDevices = widget.newButton
{
defaultFile = “button.png”,
--overFile = “button.png”,
label = " Devices ",
labelColor =
{
default = { 51, 51, 51, 255 },
},
font = native.systemFont,
fontSize = 16,
onEvent = buttonDevicesScene,
}
local buttonTools = widget.newButton
{
defaultFile = “button.png”,
--overFile = “button.png”,
label = " Tools ",
labelColor =
{
default = { 51, 51, 51, 255 },
},
font = native.systemFont,
fontSize = 16,
onEvent = buttonToolsScene,
}
local buttonOff = widget.newButton
{
defaultFile = “blue.png”,
--overFile = “blue.png”,
label = " Off ",
labelColor =
{
default = { 51, 51, 51, 255 },
},
font = native.systemFont,
fontSize = 24,
onEvent = buttonOffScene,
}
scanButton = widget.newButton{
style = “scanLarge”,
--yOffset = buttonOffset,
label = “Scan for devices”,
--strokeColor = { 0, 0, 0, 255 },
--strokeWidth = 20,
onEvent = onDoneReleaseDevices
}
scanButton.x = display.contentWidth * 0.5
scanButton.y = 100
buttonUp.x = 40; buttonUp.y = 375
buttonRight.x = 120; buttonRight.y = 375
buttonDown.x = 200; buttonDown.y = 375
buttonLeft.x = 280; buttonLeft.y = 375
buttonOff.x = 160; buttonOff.y = 455
buttonTools.x = 50; buttonTools.y = 455
buttonDevices.x = 280; buttonDevices.y = 455
–Create a group to hold our widgets, images, text
local group = display.newGroup()
–Insert widgets, images, text into group
group:insert( title )
group:insert( buttonUp )
group:insert( buttonRight )
group:insert( buttonDown )
group:insert( buttonLeft )
group:insert( buttonOff )
group:insert( buttonTools )
group:insert( buttonDevices )
group:insert( scanButton )
end
– Called BEFORE scene has moved onscreen:
function scene:willEnterScene( event )
local group = self.view
-----------------------------------------------------------------------------
– This event requires build 2012.782 or later.
-----------------------------------------------------------------------------
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
– 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 listeners, unload sounds, etc.)
–[[
group:remove( title )
group:remove( buttonUp )
group:remove( buttonRight )
group:remove( buttonDown )
group:remove( buttonLeft )
group:remove( buttonOff )
group:remove( buttonTools )
group:remove( buttonDevices )
group:remove( scanButton )
--]]
if scanButton then --if used button
print “iffff”
scanButton:removeSelf() – then button remove self
scanButton = nil – button is done
end
if buttonDevices then --if used button
buttonDevices:removeSelf() – then button remove self
buttonDevices = nil – button is done
end --]]
--object:removeEventListener( eventName, listener )
--object:removeEventListener( buttonDevicesScene, buttonDevices )
--storyboard.removeScene( One )
print( “1: exitScene event” )
end
– Called AFTER scene has finished moving offscreen:
function scene:didExitScene( event )
local group = self.view
– This event requires build 2012.782 or later.
--storyboard.removeScene( One )
print( “1: didexitScene event” )
end
– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )
local group = self.view
– INSERT code here (e.g. remove listeners, widgets, save state, etc.)
–[[
group:remove(title)
group:remove(buttonUp)
group:remove(buttonRight)
group:remove(buttonDown)
group:remove(buttonLeft)
group:remove(buttonOff)
group:remove(buttonTools)
group:remove(buttonDevices) --]]
end
– Called if/when overlay scene is displayed via storyboard.showOverlay()
function scene:overlayBegan( event )
local group = self.view
local overlay_name = event.sceneName – name of the overlay scene
-----------------------------------------------------------------------------
– This event requires build 2012.797 or later.
-----------------------------------------------------------------------------
end
– Called if/when overlay scene is hidden/removed via storyboard.hideOverlay()
function scene:overlayEnded( event )
local group = self.view
local overlay_name = event.sceneName – name of the overlay scene
-----------------------------------------------------------------------------
– This event requires build 2012.797 or later.
-----------------------------------------------------------------------------
end
– END OF YOUR IMPLEMENTATION
– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )
– “willEnterScene” event is dispatched before scene transition begins
scene:addEventListener( “willEnterScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
– “didExitScene” event is dispatched after scene has finished transitioning out
scene:addEventListener( “didExitScene”, 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 )
– “overlayBegan” event is dispatched when an overlay scene is shown
scene:addEventListener( “overlayBegan”, scene )
– “overlayEnded” event is dispatched when an overlay scene is hidden/removed
scene:addEventListener( “overlayEnded”, scene )
return scene