Hey Everyone,
First let me say that I love the Corona community so far and that I would definitely appreciate all of your opinions on the best practices here.
Being relatively new I’m sort of stumbling through some best techniques and I definitely want to practice performance optimization as much as I can. (I’ve read A LOT but this part still eludes me)
My question is about creating local variables, where to do so, and where to removeSelf()/nil them if applicable. I’ll sort-of split this into two different methods to make it a little easier to understand (I’ll also show code for both)… Is one better than the other? (Please see Method1 and Method2 below):
Method 1 (Game button still exists in scene:destroyScene):
Specifically, I’ve noticed that if I create local variables at the top of my file in a “Forward Declarations” section, say a UI button, initialize that button in the scene:createScene function and add it to the screen group, I have access to it and can removeSelf()/nil it in the scene:destroyScene function.
VERY Stripped down example of the code for Method 1 (I’ve left out irrelevant code/functions):
---------------------------------------------------------------------------------
-- Forward Declarations
---------------------------------------------------------------------------------
local newGameButton;
---------------------------------------------------------------------------------
-- Main Menu functions
---------------------------------------------------------------------------------
-- These are the functions triggered by the buttons
local function newGameButtonReleased( event )
if event.phase == "ended" then
storyboard.gotoScene( "nextscene", "slideLeft", 500 );
end
return true;
end
---------------------------------------------------------------------------------
-- Storyboard functions
---------------------------------------------------------------------------------
function scene:createScene( event )
local screenGroup = self.view;
-- Create buttons
newGameButton = ui.newButton{
default = "images/redbutton.png",
over = "images/redbuttonover.png",
onRelease = newGameButtonReleased,
id = "newGameButton",
text = "New Game",
textColor = { 51, 51, 51, 255 },
size = 22,
emboss = true
}
screenGroup:insert( newGameButton );
newGameButton.x = display.contentWidth/2;
newGameButton.y = display.contentHeight/2 - 115;
end
function scene:destroyScene( event )
-- Remove display items: NOTICE newGameButton DOES exist in this way of doing it:
if newGameButton then newGameButton:removeSelf(); newGameButton=nil; end
end
... etc.
Method 2 (Game button DOES NOT exist in scene:destroyScene):
On the otherhand, if I don’t create the local button in at the top of my file and assign it to a new button in the scene:createScene function but rather create and assign it in that function (and add to local screengroup) that I do not have access to it.
VERY Stripped down example of the code for Method 2 (I’ve left out irrelevant code/functions):
---------------------------------------------------------------------------------
-- Main Menu functions
---------------------------------------------------------------------------------
-- These are the functions triggered by the buttons
local function newGameButtonReleased( event )
if event.phase == "ended" then
storyboard.gotoScene( "nextscene", "slideLeft", 500 );
end
return true;
end
---------------------------------------------------------------------------------
-- Storyboard functions
---------------------------------------------------------------------------------
function scene:createScene( event )
local screenGroup = self.view;
-- Create buttons
local newGameButton = ui.newButton{
default = "images/redbutton.png",
over = "images/redbuttonover.png",
onRelease = newGameButtonReleased,
id = "newGameButton",
text = "New Game",
textColor = { 51, 51, 51, 255 },
size = 22,
emboss = true
}
screenGroup:insert( newGameButton );
newGameButton.x = display.contentWidth/2;
newGameButton.y = display.contentHeight/2 - 115;
end
function scene:destroyScene( event )
-- Remove display items: NOTICE newGameButton DOES NOT exist in this way of doing it which makes checking and removing the newGameButton irrelevant:
-- if newGameButton then newGameButton:removeSelf(); newGameButton=nil; end
end
... etc.
Thank you for any help you can provide you lovely Corona experts!
[import]uid: 141828 topic_id: 29249 reply_id: 329249[/import]