Check my code... tell me if I "get it" ?

Hey guys, I’ve been working with corona for a few weeks now, and really starting to feel like I’m getting somewhere, but I’m also full of doubt, and would like some direction if I could be so bold :slight_smile:

I’d like to post my code here, and have those in the know, tell me how I’m doin’…

I really feel like I don’t fully grasp the whole global, local stuff, and I know my app is leaking memory between scenes, I’m totally not sure why, I’ve made sure to remove evcerything and tried to keep things local, but since I barely grasp what that means, it’s likely Im makin’ mistakes

Anyway here we go :slight_smile:

  • main.lua
display.setStatusBar( display.HiddenStatusBar )  
local storyboard = require "storyboard"  
storyboard.gotoScene( "scripts.menu" )  
  
local analytics = require "analytics"  
analytics.init( "editedjustbecause" )  
  
local monitorMem = function()  
 collectgarbage()  
 print( "MemUsage: " .. collectgarbage("count") )  
 local textMem = system.getInfo( "textureMemoryUsed" ) / 1000000  
 print( "TexMem: " .. textMem )  
end  
timer.performWithDelay(6000, monitorMem, -1)  
  
local mask = display.newImageRect ("img/mask.png",630,420)  
mask.x,mask.y = 240,160  

menu.lua

local storyboard = require "storyboard"  
local scene = storyboard.newScene()  
local sprite = require "sprite"  
local physics = require "physics"  
local widget = require "widget"  
local score = require( "scripts.score" )  
physics.start();   
physics.pause()  
  
----------------------------------------------------------------------------------------------------------------------------------------------------------------------  
   
function scene:createScene(event)  
 print("Menu Created")  
 local group = self.view  
 local splashGroup = display.newGroup()  
 local logoBar = display.newGroup()  
  
 local splashLogo = display.newImageRect("img/splash.png", 480, 320 )  
 splashLogo.x, splashLogo.y = 240, 160  
  
 local navigationSheetoptions =  
 {  
 frames = {  
 {x = 0, y = 0,width = 114,height = 40},  
 {x = 0, y = 41,width = 114,height = 40},  
 {x = 114, y = 0,width = 155,height = 40},  
 {x = 114, y = 41,width = 155,height = 40},  
 {x = 270, y = 0,width = 113,height = 40},  
 {x = 270, y = 41,width = 113,height = 40},  
 },  
  
 -- optional params; used for dynamic resolution support  
 sheetContentWidth = 383,  
 sheetContentHeight = 82  
 }  
 local navigationSheet = graphics.newImageSheet( "img/navsprite.png", navigationSheetoptions )  
  
  
 local socialSpriteSheetOptions =  
 {  
 frames = {  
 {x = 0, y = 0,width = 43,height = 42},  
 {x = 0, y = 43,width = 43,height = 42},  
  
 },  
  
 -- optional params; used for dynamic resolution support  
 sheetContentWidth = 43,  
 sheetContentHeight = 84  
 }  
 local socialspriteSheet = graphics.newImageSheet( "img/socialsprite.png", socialSpriteSheetOptions )  
  
  
  
 local fbLogo = display.newImage( socialspriteSheet, 1 )  
 fbLogo.x, fbLogo.y = 415, 30  
  
 local twitterLogo = display.newImage( socialspriteSheet, 2 )  
 twitterLogo.x, twitterLogo.y = 450, 30  
  
  
  
 local playButtonEvent = function (event )  
 print('play button pressed')  
 storyboard.gotoScene( "scripts.level1" )  
 end  
  
 local optionsButtonEvent = function (event )  
 print('options button pressed')  
 --transition.to( splashGroup, { time=400,x=(0), y=(290),easing = inOutQuad} )  
 transition.to( logoBar, { time=400,x=(0), y=(-55),easing = inOutQuad} )  
  
 end  
  
 local statsButtonEvent = function (event )  
 print('stats button pressed')  
 --storyboard.gotoScene( "scripts.level1" )  
 end  
   
 local playButton = widget.newButton{  
 sheet = navigationSheet,  
 defaultIndex = 1,  
 overIndex = 2,  
 left = 30,  
 top = 260,  
 width = 112, height =40,  
 onRelease = playButtonEvent  
 }  
  
 local optionsButton = widget.newButton{  
 sheet = navigationSheet,  
 defaultIndex = 3,  
 overIndex = 4,  
 left = 157,  
 top = 260,  
 width = 155, height =40,  
 onRelease = optionsButtonEvent  
 }  
  
 local statsButton = widget.newButton{  
 sheet = navigationSheet,  
 defaultIndex = 5,  
 overIndex = 6,  
 left = 330,  
 top = 260,  
 width = 113, height =40,  
 onRelease = statsButtonEvent  
 }  
  
 local Background = display.newImageRect("img/bgmenu.png", 480, 320 )  
 Background.x, Background.y = 240, 160  
  
 local topBg = display.newImageRect("img/topbg.png", 480, 45 )  
 topBg.x, topBg.y = 240, 23  
  
 logoBar:insert(topBg)  
 logoBar:insert(fbLogo)  
 logoBar:insert(twitterLogo)  
 group:insert(logoBar)  
 group:insert(splashGroup)  
  
 splashGroup:insert(Background)  
 splashGroup:insert(splashLogo)  
 splashGroup:insert(playButton)  
 splashGroup:insert(optionsButton)  
 splashGroup:insert(statsButton)  
  
 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------  
end  
  
function scene:enterScene( event )  
 storyboard.removeScene('scripts.level1')  
 storyboard.purgeScene('scripts.level1')   
 local group = self.view  
 score.fadeScoreOut()  
 physics.start()  
 print("Menu Entered")  
 print()  
end  
function scene:exitScene( event )  
 local group = self.view  
  
 display.remove(topBg)  
 display.remove(fbLogo)  
 display.remove(twitterLogo)  
 display.remove(logoBar)  
 display.remove(splashGroup)  
  
 display.remove( Background)  
 display.remove( splashLogo)   
 display.remove( playButton)  
 display.remove( optionsButton)  
 display.remove( statsButton)  
  
 topBg = nil   
 fbLogo = nil   
 twitterLogo = nil   
 logoBar = nil   
 splashGroup = nil   
  
 Background = nil   
 splashLogo = nil   
 playButton = nil   
 optionsButton = nil   
 statsButton = nil   
  
 navigationSheet = nil  
 socialspriteSheet = nil  
  
 physics.stop()  
  
end  
  
function scene:destroyScene( event )  
 local group = self.view  
 print("Menu Destroyed")  
  
end  
  
scene:addEventListener( "createScene", scene )  
scene:addEventListener( "enterScene", scene )  
scene:addEventListener( "exitScene", scene )  
scene:addEventListener( "destroyScene", scene )  
  
return scene  

level1.lua

local storyboard = require "storyboard"  
local scene = storyboard.newScene()  
local score = require( "scripts.score" )  
local sprite = require("sprite")  
local physics = require "physics"  
local widget = require "widget"  
system.activate( "multitouch" )  
physics.start();   
physics.pause()  
----------------------------------------------------------------------------------------------------------------------------------------------------------------------  
   
function scene:createScene( event )  
  
 print("Level 1 Created")  
  
 local group = self.view  
 local backgroundImages = display.newGroup()  
 local level = display.newGroup()  
 local controls = display.newGroup()  
  
 local runningSpeed = 7  
 local closeBackgroundSpeed = runningSpeed\*.25  
 local farBackgroundSpeed = closeBackgroundSpeed\*.5  
 local goingRight = false  
  
 physics.setGravity( 0, 60 )  
  
 local options =  
 {  
 frames = {  
 -- further background image 1  
 {x = 0, y = 0,width = 486,height = 223},  
  
 -- cloud background images 2  
 {x = 0,y = 223,width = 458,height = 200},  
  
 -- grass platform 3  
 {x = 56, y = 495, width = 740, height = 88 },  
  
 -- front background images 4  
 { x = 462, y = 232, width = 486, height = 268 },  
  
 -- Sun Image 5  
 {x = 0,y = 582,width = 154,height = 154},  
  
 -- Spiral Image 6  
 {x = 154,y = 584,width = 441,height = 441},  
  
 -- goRightButton7  
 {x = 405,y = 410,width = 55,height = 58},  
  
 -- goRightButtonHover 8  
 {x = 0,y = 410,width = 55,height = 58},  
  
 -- goLeftButton 9  
 {x = 172,y = 410,width = 56,height = 58},  
  
 -- goLeftButton 10  
 {x = 230,y = 410,width = 56,height = 58},  
  
 -- Jump 11  
 {x = 57,y = 410,width = 56,height = 58},  
  
 -- JumpHover 12  
 {x = 115,y = 410,width = 56,height = 58},  
 },  
 sheetContentWidth = 1024,  
 sheetContentHeight = 1024  
 }  
  
 local NinjaSheetoptions =  
 {  
 width = 96,  
 height = 96,  
 numFrames = 27,  
 sheetContentWidth = 864,   
 sheetContentHeight = 288   
 }  
  
 local NinjaSheet = graphics.newImageSheet( "img/walkcyclehuge.png", NinjaSheetoptions )  
  
 local ninjaSheetData = {  
 { name="standRight", start=1, count=1 },  
 { name="walkRight", start=2, count=12, time=600 },  
 { name="standLeft", start=26, count=1},  
 { name="jumpRight", start=27, count=1},  
 { name="walkLeft", start=14, count=12, time=600 },   
 }  
 local ninja = display.newSprite( NinjaSheet, ninjaSheetData )  
 local ninjaShape = { 0,-50, 35,-50, 22,25, -22,25, -33,-38 }  
 physics.addBody( ninja, { density=10.0, friction=0, bounce=0.0, shape=ninjaShape } )  
 ninja.isFixedRotation = true   
 ninja.x, ninja.y = 150, 50  
  
 local imageSheet = graphics.newImageSheet( "img/bigsprite.png", options )  
  
 --physics.setDrawMode( "hybrid" )  
 ------------------------------------------------------------------------------------------------------------------------------------------------------------------  
  
 local function standRight()   
 ninja:setSequence("standRight")  
 ninja:play()  
 goingRight = false  
 goingLeft = false  
 print("standing right")  
 end   
  
 local function walkRight()  
 local ninja = ninja   
  
 ninja:setSequence("walkRight")  
 ninja:play()  
 goingRight = true  
 goingLeft = false  
 print("running right")  
 end   
  
 local function standLeft()   
 ninja:setSequence("standLeft")  
 ninja:play()  
 goingRight = false  
 goingLeft = false  
 print("standing Left")  
 end  
  
 local function walkLeft()  
 ninja:setSequence("walkLeft")  
 ninja:play()  
 goingRight = false  
 goingLeft = true  
 print("running Left")  
 end   
  
 local function ninjaJump(event)   
 if (ninja.grounded == true) then   
 ninja.grounded = false   
 ninja:applyLinearImpulse(0, -1100, ninja.x, ninja.y)  
 print("grounded is false")  
 end  
 end   
  
 local function PauseGame(event)  
 storyboard.gotoScene( "scripts.menu" )  
 end  
  
 local goRightButton = display.newImage( imageSheet, 7 )  
 goRightButton.x, goRightButton.y = 110,300  
  
 local goLeftButton = display.newImage( imageSheet, 9 )  
 goLeftButton.x,goLeftButton.y = 40,300  
  
 local jumpButton = display.newImage( imageSheet, 11 )  
 jumpButton.x,jumpButton.y = 440,300  
  
 local pauseButton = display.newImage( imageSheet, 11 )  
 pauseButton.x,pauseButton.y = 240,10  
  
 local Background = display.newImageRect("img/backgroundlevel1.png", 680, 320 )  
 Background.x, Background.y = 240, 100  
  
 local cloudbg = display.newImage( imageSheet, 2 )  
 cloudbg.x, cloudbg.y = 240, 100  
  
 local cloudbg2 = display.newImage( imageSheet, 2 )  
 cloudbg2.x, cloudbg2.y = 720, 100  
  
 local nearBackground = display.newImage( imageSheet, 4 )  
 nearBackground.x, nearBackground.y = 240, 220  
  
 local nearBackground2 = display.newImage( imageSheet, 4 )  
 nearBackground2.x, nearBackground2.y = 720, 220  
  
 local farBackground = display.newImage( imageSheet, 1 )  
 farBackground.x, farBackground.y = 240, 200  
  
 local farBackground2 = display.newImage( imageSheet, 1 )  
 farBackground2.x, farBackground2.y = 720, 200  
  
 local spiral = display.newImage( imageSheet, 6 )  
 spiral.x, spiral.y = 40, 60  
  
 local sun = display.newImage( imageSheet, 5 )  
 sun.x, sun.y = 40, 60  
  
 local platform = display.newImage( imageSheet, 3 )  
 platform.x, platform.y = 250, 290  
 physics.addBody( platform, "static",{density=10.0, bounce=0.0})  
  
 local platform2 = display.newImage( imageSheet, 3 )  
 platform2.x, platform2.y = 1250, 290  
 physics.addBody( platform2, "static",{density=10.0, bounce=0.0})  
  
 local platform3 = display.newImage( imageSheet, 3 )  
 platform3.x, platform3.y = 2150, 270  
 physics.addBody( platform3, "static",{density=10.0, bounce=0.0})  
  
 local platform4 = display.newImage( imageSheet, 3 )  
 platform4.x, platform4.y =3150, 270  
 physics.addBody( platform4, "static",{density=10.0, bounce=0.0})  
  
 local platform5 = display.newImage( imageSheet, 3 )  
 platform5.x, platform5.y =4150, 220  
 physics.addBody( platform5, "static",{density=10.0, bounce=0.0})  
  
 local platform6 = display.newImage( imageSheet, 3 )  
 platform6.x, platform6.y =5050, 170  
 physics.addBody( platform6, "static",{density=10.0, bounce=0.0})  
  
 local platform7 = display.newImage( imageSheet, 3 )  
 platform7.x, platform7.y =6050, 170  
 physics.addBody( platform7, "static",{density=10.0, bounce=0.0})  
  
 local platform8 = display.newImage( imageSheet, 3 )  
 platform8.x, platform8.y =7050, 240  
 physics.addBody( platform8, "static",{density=10.0, bounce=0.0})  
  
 local platform9 = display.newImage( imageSheet, 3 )  
 platform9.x, platform9.y =8050, 270  
 physics.addBody( platform9, "static",{density=10.0, bounce=0.0})  
  
 ------THE NINJA STUFF ---------------------------------------------------------------------------------------------------  
  
 group:insert(backgroundImages)  
 group:insert(level)  
 group:insert(controls)  
 backgroundImages:insert(Background)  
 backgroundImages:insert(spiral)  
 backgroundImages:insert(sun)  
 backgroundImages:insert(cloudbg2)  
 backgroundImages:insert(cloudbg)  
  
 backgroundImages:insert(farBackground)  
 backgroundImages:insert(farBackground2)   
 backgroundImages:insert(nearBackground)  
 backgroundImages:insert(nearBackground2)  
  
 level:insert(platform)  
 level:insert(platform2)  
 level:insert(platform3)  
 level:insert(platform4)  
 level:insert(platform5)  
 level:insert(platform6)  
 level:insert(platform7)  
 level:insert(platform8)  
 level:insert(platform9)  
 level:insert(ninja)  
 controls:insert(goRightButton)  
 controls:insert(goLeftButton)  
 controls:insert(pauseButton)  
 controls:insert(jumpButton)  
  
 local function landOnPlatform( event )  
 ninja.grounded = true  
 print("grounded is true")  
 end  
  
 local function resetScene( self, event )  
 if event.phase == "began" then  
 elseif event.phase == "moved" then  
 elseif event.phase == "ended" or event.phase == "cancelled" then  
 PauseGame()  
 end  
 end  
 pauseButton.touch = resetScene  
  
 local function goLeftTouched( self, event )  
 if event.phase == "began" then  
 walkLeft()  
 elseif event.phase == "moved" then  
 elseif event.phase == "ended" or event.phase == "cancelled" then  
 standLeft()  
 end  
 return true -- IMPORTANT  
 end  
 goLeftButton.touch = goLeftTouched  
  
 local function goRightTouched( self, event )  
 if event.phase == "began" then  
 walkRight()  
 elseif event.phase == "moved" then  
 elseif event.phase == "ended" or event.phase == "cancelled" then  
 standRight()  
 end  
 return true -- IMPORTANT  
 end  
 goRightButton.touch = goRightTouched  
  
 local function jumpTouched()  
 ninjaJump()  
 return true -- IMPORTANT  
 end  
 jumpButton.tap = jumpTouched  
  
  
 local function updateGameplay()  
  
 cloudbg.x = cloudbg.x - (.4)  
 cloudbg2.x = cloudbg2.x - (.4)  
 spiral.rotation = spiral.rotation - (.25)  
  
 if goingRight == true then   
 nearBackground.x = nearBackground.x - (2)  
 nearBackground2.x = nearBackground2.x - (2)  
 farBackground.x = farBackground.x - (1)  
 farBackground2.x = farBackground2.x - (1)  
 level.x = level.x - (runningSpeed)  
 ninja.x = ninja.x + (runningSpeed)  
 end   
 if goingLeft == true then   
 nearBackground.x = nearBackground.x + (2)  
 nearBackground2.x = nearBackground2.x + (2)  
 farBackground.x = farBackground.x + (1)  
 farBackground2.x = farBackground2.x + (1)  
 level.x = level.x + (runningSpeed)  
 ninja.x = ninja.x - (runningSpeed)  
 end   
 end  
  
  
 local function CheckBackgrounds()   
 score.setScore (score.getScore()+12)  
 if(nearBackground.x \< -241) then   
 nearBackground.x = 720   
 end  
 if(nearBackground2.x \< -240) then  
 nearBackground2.x = 720   
 end  
 if(farBackground.x \< -240) then   
 farBackground.x = 720   
 end  
 if(farBackground2.x \< -240) then   
 farBackground2.x = 720   
 end  
 if(nearBackground.x \> 720) then   
 nearBackground.x = -240   
 end  
 if(nearBackground2.x \> 720) then   
 nearBackground2.x = -240   
 end  
 if(farBackground.x \> 720) then   
 farBackground.x = -240   
 end  
 if(farBackground2.x \> 720) then   
 farBackground2.x = -240   
 end  
 if(cloudbg.x \< -240) then  
 cloudbg.x = 720  
 end  
 if(cloudbg2.x \< -240) then  
 cloudbg2.x = 720  
 end  
 end  
 scene.game = timer.performWithDelay(1, updateGameplay, -1)  
 scene.backgrounds = timer.performWithDelay(50, CheckBackgrounds, -1)  
  
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  
  
 scene.colisionDetector = Runtime:addEventListener( "collision", landOnPlatform )  
 goRightButton:addEventListener( "touch", goRightButton )  
 pauseButton:addEventListener( "touch", pauseButton )  
 jumpButton:addEventListener( "tap", jumpButton )  
 goLeftButton:addEventListener( "touch", goLeftButton )  
  
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  
end  
function scene:enterScene( event )  
 print("Level 1 Entered")  
 local group = self.view  
  
 physics.start()  
 storyboard.removeScene('scripts.menu')  
 storyboard.purgeScene('scripts.menu')  
 score.fadeScoreIn()  
end  
  
function scene:exitScene( event )  
 local group = self.view  
 physics.stop()  
 score.setScore (0)  
  
 timer.cancel(scene.backgrounds)  
 timer.cancel(scene.game)  
 Runtime:removeEventListener( "collision", landOnPlatform )  
  
 display.remove(backgroundImages)  
 display.remove(level)  
 display.remove(controls)  
 display.remove(Background)  
 display.remove(spiral)  
 display.remove(sun)  
 display.remove(cloudbg2)  
 display.remove(cloudbg)  
  
 display.remove(farBackground)  
 display.remove(farBackground2)   
 display.remove(nearBackground)  
 display.remove(nearBackground2)  
  
 display.remove(platform)  
 display.remove(platform2)  
 display.remove(platform3)  
 display.remove(platform4)  
 display.remove(platform5)  
 display.remove(platform6)  
 display.remove(platform7)  
 display.remove(platform8)  
 display.remove(platform9)  
 display.remove(ninja)  
 display.remove(goRightButton)  
 display.remove(goLeftButton)  
 display.remove(pauseButton)  
 display.remove(jumpButton)  
  
 scene.backgrounds = nil  
 scene.game = nil  
 goingRight = nil   
 goingLeft = nil   
 standingLeft = nil   
 standingRight = nil   
 runningSpeed = nil   
  
 NinjaSheet = nil   
 imageSheet = nil   
 ninjaShape = nil   
  
 platform= nil  
 platform2= nil  
 platform3= nil  
 platform4= nil  
 platform5= nil  
 platform6= nil  
 platform7= nil  
 platform8= nil  
 platform9= nil  
 goRightButton= nil  
 goLeftButton= nil  
 pauseButton= nil  
 jumpButton= nil  
  
 print(runningSpeed)  
end  
  
function scene:destroyScene( event )  
 local group = self.view  
 package.loaded[physics] = nil  
 physics = nil  
 print("Level 1 Destroyed")  
end  
  
scene:addEventListener( "createScene", scene )  
scene:addEventListener( "enterScene", scene )  
scene:addEventListener( "exitScene", scene )  
scene:addEventListener( "destroyScene", scene )  
  
return scene  

score.lua - changed up some from peach pellin’s tutorial

local M = {}  
  
local numbers = {   
 [string.byte("0")] = "img/0.png",  
 [string.byte("1")] = "img/1.png",  
 [string.byte("2")] = "img/2.png",  
 [string.byte("3")] = "img/3.png",  
 [string.byte("4")] = "img/4.png",  
 [string.byte("5")] = "img/5.png",  
 [string.byte("6")] = "img/6.png",  
 [string.byte("7")] = "img/7.png",  
 [string.byte("8")] = "img/8.png",  
 [string.byte("9")] = "img/9.png",  
}  
  
local theBackground = display.newImage( "img/scorebg.png" )  
local theScoreGroup = display.newGroup()  
local numbersGroup = display.newGroup()  
theScoreGroup:insert( theBackground )  
theScoreGroup:insert( numbersGroup )  
local theScore = 0  
  
local function fadeScoreOut()  
 theScoreGroup.alpha = 0   
 print('Score Hidden')  
end   
M.fadeScoreOut = fadeScoreOut   
local function fadeScoreIn()  
transition.to( theScoreGroup, { delay=0, time=1600, alpha=(1),transition = easing.outQuad} )  
  
 print('Score Shown')  
end   
M.fadeScoreIn = fadeScoreIn   
  
local function updateScore()  
 theScoreGroup:remove(2)  
  
 local numbersGroup = display.newGroup()  
 theScoreGroup:insert( numbersGroup )  
  
 local scoreStr = tostring( theScore )  
 local scoreLen = string.len( scoreStr )  
 local i = scoreLen   
 local x = 442  
 local y = 25  
  
 while i \> 0 do  
 local c = string.byte( scoreStr, i )  
 local digitPath = numbers[c]  
 local numberImage = display.newImage( digitPath )  
 numbersGroup:insert( numberImage )  
 numberImage.x = x   
 numberImage.y = y  
 numberImage.width = 26  
 numberImage.height = 28  
 x = x - 18  
 i = i - 1  
 end  
end  
M.updateScore = updateScore  
local function getScore()  
 return theScore  
end  
  
M.getScore = getScore  
  
local function setScore( score )  
 theScore = score  
 updateScore()  
  
end  
  
M.setScore = setScore  
  
return M   

also, re: score.lua, if I remove
local theBackground = display.newImage( “img/scorebg.png” )

and all other references to the theBackground, my app crashes the viewer completely, and I have no idea why it’s depending on that being in there, :stuck_out_tongue:

Thanks in advance guys, I’m learning and appreciate these forums a bunch :smiley:

[import]uid: 157863 topic_id: 30590 reply_id: 330590[/import]

i’ll just insert my 2 cents in right here-

Well after scrolling through your code seeing as it was several hundred lines long i decided to just give you some tips on globals and locals. I usually us local variables more then global variables to prevent memory leeks but globals have their uses too. A local variable can be “seen” or retrieved from anywhere in that scope or scene. A global variable can be seen from any scene. Now a couple things to note is that global variables don’t get removed when changing scenes, they will stay throughout your entire app unless you “delete” them which can be done by setting the variable equal to nil. For organisation and such, whenever i use globals they always go in the same place, usually at the top. That way when I no longer need those global variables I can just copy the section of code that sets all the globals and change the values and such to nil.

So now why use globals? Globals can be very useful, like if your game had sound and that sound level can be changed from the options menu (or whatever) and then you can have that value by global and be able to access that from anywhere

if you need help or whatever just ask, and if you want to know more about preventing memory leaks go here - http://www.coronalabs.com/blog/2011/08/15/corona-sdk-memory-leak-prevention-101/

-Boxie [import]uid: 113909 topic_id: 30590 reply_id: 122570[/import]

i’ll just insert my 2 cents in right here-

Well after scrolling through your code seeing as it was several hundred lines long i decided to just give you some tips on globals and locals. I usually us local variables more then global variables to prevent memory leeks but globals have their uses too. A local variable can be “seen” or retrieved from anywhere in that scope or scene. A global variable can be seen from any scene. Now a couple things to note is that global variables don’t get removed when changing scenes, they will stay throughout your entire app unless you “delete” them which can be done by setting the variable equal to nil. For organisation and such, whenever i use globals they always go in the same place, usually at the top. That way when I no longer need those global variables I can just copy the section of code that sets all the globals and change the values and such to nil.

So now why use globals? Globals can be very useful, like if your game had sound and that sound level can be changed from the options menu (or whatever) and then you can have that value by global and be able to access that from anywhere

if you need help or whatever just ask, and if you want to know more about preventing memory leaks go here - http://www.coronalabs.com/blog/2011/08/15/corona-sdk-memory-leak-prevention-101/

-Boxie [import]uid: 113909 topic_id: 30590 reply_id: 122570[/import]