Sprite Group

Hello again,
The “basic method” of any Storyboard scene is to manually clean up your listeners, timers, etc. in the scene:didExitScene() function, then purge your scene following that process. Essentially, when you call “gotoScene()”, it will trigger the event “didExitScene” event, so you remove your “custom” stuff there. Then, in that same function, you call the .purgeScene() command to clear the scene’s view objects.

-- scene1.lua  
local storyboard = require "storyboard"  
local scene = storyboard.newScene()  
  
function scene:createScene( event )  
 local bg = display.newImage( self.view, "background.png" )  
end  
scene:addEventListener( "createScene" )  
  
function scene:didExitScene( event )  
 --your cleanup here (kill Runtime listeners, timers, transitions; dispose audio; etc.)  
 storyboard.purgeScene( "scene1" )  
end  
scene:addEventListener( "didExitScene" )  
  
return scene  

Let me know if this helps; if not, I can try to clarify more specific points for you.

Best regards,
Brent [import]uid: 200026 topic_id: 34466 reply_id: 137141[/import]

Hmmm from what you posted I think I pretty much have that covered. Here’s my splashscreen.lua
All your code is pretty much all the way at the bottom:

local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
   
-- this will only happen once when the scene is first required.   
 print ("in load splashscreen file")  
   
function scene:createScene( event )  
 local group = self.view  
   
  
  
  
   
 --BACKGROUND IMAGES   
  
 local background = display.newImage("gnomiessplashbg.png")  
 group:insert(background) -- must be in the scene's view ("group") to be managed.  
 background.x = display.contentWidth / 2  
 background.y = display.contentHeight / 2   
  
 --media.playSound( "gnomiesmusic.mp3", true )   
  
 local backgroundbg2 = display.newImage("gnomiessplashbg2.png")  
 group:insert(backgroundbg2)  
 backgroundbg2.x = display.contentWidth / 2  
 backgroundbg2.y = display.contentHeight / 2   
 --BACKGROUND IMAGES END   
  
 --BUTTONS  
   
 local worlds = display.newImage("gnomiesPlaybutton.png" )  
 group:insert(worlds)  
  
 worlds.x = display.contentWidth / 1.9  
 worlds.y = display.contentHeight / 2.1   
  
  
 local options = display.newImage("gnomiesOptionsbutton.png" )  
 group:insert(options)  
  
 options.x = display.contentWidth / 1.9  
 options.y = display.contentHeight / 1.5  
   
   
   
 local about = display.newImage("gnomiesAboutbutton.png" )  
 group:insert(about)  
  
 about.x = display.contentWidth / 1.9  
 about.y = display.contentHeight / 1.16  
   
 --END BUTTONS   
  
  
 local function playtouched (event)  
 if ("ended" == event.phase) then  
 storyboard.gotoScene( "worlds", "fade" )   
 end   
 end  
   
 worlds:addEventListener ("touch", playtouched)  
  
   
 local function optionstouched (event)  
 if ("ended" == event.phase) then  
 storyboard.gotoScene("options", "fade" )   
 end   
 end  
   
 options:addEventListener ("touch", optionstouched)  
  
   
 local function abouttouched (event)  
 if ("ended" == event.phase) then  
 storyboard.gotoScene( "about", "fade" )   
 end   
 end  
   
 about:addEventListener ("touch", abouttouched)  
end  
   
   
   
   
 --STARS  
display.setStatusBar( display.HiddenStatusBar )   
   
\_W = display.contentWidth; --Returns Screen Width  
\_H = display.contentHeight; --Returns Screen Height  
local starTable = {} -- Set up star table  
   
function initStar()  
 local star1 = {}  
 star1.imgpath = "star1.png"; --Set Image Path for Star  
 star1.movementSpeed = 10000; --Determines the movement speed of star  
 table.insert(starTable, star1); --Insert Star into starTable  
  
 local star2 = {}  
 star2.imgpath = "star2.png";  
 star2.movementSpeed = 12000;  
 table.insert(starTable, star2);   
  
 local star3 = {}  
 star3.imgpath = "star3.png";  
 star3.movementSpeed = 14000;  
 table.insert(starTable, star3);  
end --END initStar()   
   
function getRandomStar()  
 local temp = starTable[math.random(1, #starTable)] -- Get a random star from starTable  
 local randomStar = display.newImage(temp.imgpath) -- Set image path for object  
 randomStar.myName = "star" -- Set the name of the object to star  
 randomStar.movementSpeed = temp.movementSpeed; -- Set how fast the object will move  
 randomStar.x = math.random(0,\_W) -- Set starting point of star at a random X position  
 randomStar.y = -50 -- Start the star off screen  
 randomStar.rotation = math.random(0,360) -- Rotate the object  
 starMove = transition.to(randomStar, {  
 time=randomStar.movementSpeed,   
 y=\_H,  
 onComplete = function(self) self.parent:remove(self); self = nil; end  
 }) -- Move the star  
end--END getRandomStar()  
   
function startGame()  
 starTimer1 = timer.performWithDelay(1700,getRandomStar, 0)  
 starTimer2 = timer.performWithDelay(2300,getRandomStar, 0)  
 starTimer3 = timer.performWithDelay(2700,getRandomStar, 0)   
end--END startGame()  
   
initStar()  
startGame()  
--STARS  
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
function scene:enterScene( event )  
 local group = self.view  
   
end  
   
function scene:exitScene( event )  
 local group = self.view  
   
end  
   
function scene:destroyScene( event )  
 local group = self.view  
   
end  
scene:addEventListener( "createScene", scene )  
scene:addEventListener( "enterScene", scene )  
scene:addEventListener( "exitScene", scene )  
scene:addEventListener( "destroyScene", scene )  
   
return scene  

The problem however is that even though the scene has ended. In this case my “stars” continue into the next scene. I have this issue with other objects in other scenes as well. Perhaps I’m still not asking the right question.

Appreciate the help.
-FL [import]uid: 72845 topic_id: 34466 reply_id: 137146[/import]

Hi again,
I see why your stars remain: you haven’t added them to the scene’s display group. Every image you add to a scene must be placed within the scene’s “self.view” group or a child group of it, otherwise the purge function will not clear it out.

That should solve the issue; let me know if it doesn’t and I’ll inspect the code further.

Best regards,
Brent [import]uid: 200026 topic_id: 34466 reply_id: 137173[/import]

I understand what you’re saying, I’m just not 100% sure how to implement it.

 --STARS  
display.setStatusBar( display.HiddenStatusBar )   
   
\_W = display.contentWidth; --Returns Screen Width  
\_H = display.contentHeight; --Returns Screen Height  
local starTable = {} -- Set up star table  
   
function initStar()  
 local star1 = {}  
 group:insert(star1)  
 star1.imgpath = "star1.png"; --Set Image Path for Star  
 star1.movementSpeed = 10000; --Determines the movement speed of star  
 table.insert(starTable, star1); --Insert Star into starTable  
  
 local star2 = {}  
 group:insert(star2)  
 star2.imgpath = "star2.png";  
 star2.movementSpeed = 12000;  
 table.insert(starTable, star2);   
  
 local star3 = {}  
 group:insert(star3)  
 star3.imgpath = "star3.png";  
 star3.movementSpeed = 14000;  
 table.insert(starTable, star3);  
end --END initStar()   
   
function getRandomStar()  
 local temp = starTable[math.random(1, #starTable)] -- Get a random star from starTable  
 local randomStar = display.newImage(temp.imgpath) -- Set image path for object  
 randomStar.myName = "star" -- Set the name of the object to star  
 randomStar.movementSpeed = temp.movementSpeed; -- Set how fast the object will move  
 randomStar.x = math.random(0,\_W) -- Set starting point of star at a random X position  
 randomStar.y = -50 -- Start the star off screen  
 randomStar.rotation = math.random(0,360) -- Rotate the object  
 starMove = transition.to(randomStar, {  
 time=randomStar.movementSpeed,   
 y=\_H,  
 onComplete = function(self) self.parent:remove(self); self = nil; end  
 }) -- Move the star  
end--END getRandomStar()  
   
function startGame()  
 starTimer1 = timer.performWithDelay(1700,getRandomStar, 0)  
 starTimer2 = timer.performWithDelay(2300,getRandomStar, 0)  
 starTimer3 = timer.performWithDelay(2700,getRandomStar, 0)   
end--END startGame()  
   
initStar()  
startGame()  
--STARS  

I tried this but it didn’t work. Probably obvious to you why, not so much to me. [import]uid: 72845 topic_id: 34466 reply_id: 137233[/import]

Hi again,
I see you’re trying to insert them (stars) in Lines 10, 16, 22. But at that point, they aren’t yet display objects (just empty tables). Try inserting them into the display group between Lines 30 and 31, directly after you create them as display objects.

group:insert( randomStar )

Does that work?
Brent
[import]uid: 200026 topic_id: 34466 reply_id: 137239[/import]

Are you seeing any errors in the Terminal, might I ask? Lines 28-30 are using invalid variables because you used those in the function above, locally, and their scope ends in that function. Corona is probably throwing an error, which might explain the weird behavior you describe. Can you please check and let me know? Thanks!
[import]uid: 200026 topic_id: 34466 reply_id: 137280[/import]

After doing…

 --STARS  
  
display.setStatusBar( display.HiddenStatusBar )   
   
\_W = display.contentWidth; --Returns Screen Width  
\_H = display.contentHeight; --Returns Screen Height  
local starTable = {} -- Set up star table  
function initStar()  
 local star1 = {}  
 star1.imgpath = "star1.png"; --Set Image Path for Star  
 star1.movementSpeed = 10000; --Determines the movement speed of star  
 table.insert(starTable, star1); --Insert Star into starTable  
  
 local star2 = {}  
 star2.imgpath = "star2.png";  
 star2.movementSpeed = 12000;  
 table.insert(starTable, star2);   
  
 local star3 = {}  
 star3.imgpath = "star3.png";  
 star3.movementSpeed = 14000;  
 table.insert(starTable, star3);  
end --END initStar()   
   
function getRandomStar()  
 local temp = starTable[math.random(1, #starTable)] -- Get a random star from starTable  
 local randomStar = display.newImage(temp.imgpath) -- Set image path for object  
group:insert(star1)  
group:insert(star2)  
group:insert(star3)  
group:insert(randomStar)  
 randomStar.myName = "star" -- Set the name of the object to star  
 randomStar.movementSpeed = temp.movementSpeed; -- Set how fast the object will move  
 randomStar.x = math.random(0,\_W) -- Set starting point of star at a random X position  
 randomStar.y = -50 -- Start the star off screen  
 randomStar.rotation = math.random(0,360) -- Rotate the object  
 starMove = transition.to(randomStar, {  
 time=randomStar.movementSpeed,   
 y=\_H,  
 onComplete = function(self) self.parent:remove(self); self = nil; end  
 }) -- Move the star  
end--END getRandomStar()  
   
function startGame()  
 starTimer1 = timer.performWithDelay(1700,getRandomStar, 0)  
 starTimer2 = timer.performWithDelay(2300,getRandomStar, 0)  
 starTimer3 = timer.performWithDelay(2700,getRandomStar, 0)   
end--END startGame()  
   
initStar()  
startGame()  
--STARS  

The stars are all spawned at the same time with no movement in the upper left hand corner of the screen (In a pile). After clicking “Play” to move to the next scene, the pile of stars still carries over to the next scene.

Also the error in terminal says:

2013-01-03 19:09:03.530 Corona Simulator[35179:903] Runtime error
…focuslab/Desktop/Level Select Beta/splashscreen.lua:127: attempt to index global ‘group’ (a nil value)
stack traceback:
[C]: ?
…focuslab/Desktop/Level Select Beta/splashscreen.lua:127: in function ‘_listener’
?: in function <?:534>
?: in function <?:229>

I don’t understand why the ‘group’ (a nil value)
Also line 127 in my error message is line 28 in the code posted. [import]uid: 72845 topic_id: 34466 reply_id: 137276[/import]

In case you missed the edit:

2013-01-03 19:09:03.530 Corona Simulator[35179:903] Runtime error
…focuslab/Desktop/Level Select Beta/splashscreen.lua:127: attempt to index global ‘group’ (a nil value)
stack traceback:
[C]: ?
…focuslab/Desktop/Level Select Beta/splashscreen.lua:127: in function ‘_listener’
?: in function <?:534>
?: in function <?:229>

Also line 127 in my error message is line 28 in the code posted.

How would I rewrite it so the variable IS valid?
Again, thanks a lot for your help. You answering these questions is helping me get a grasp on all this new code. [import]uid: 72845 topic_id: 34466 reply_id: 137281[/import]

Got the edit, thanks!

Are all of these functions… basically lines 7 to 52… within your scene:createScene() function? [import]uid: 200026 topic_id: 34466 reply_id: 137282[/import]

This is the full .lua

[code]
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

– this will only happen once when the scene is first required.
print (“in load splashscreen file”)

function scene:createScene( event )
local group = self.view

–BACKGROUND IMAGES

local background = display.newImage(“gnomiessplashbg.png”)
group:insert(background) – must be in the scene’s view (“group”) to be managed.
background.x = display.contentWidth / 2
background.y = display.contentHeight / 2

–media.playSound( “gnomiesmusic.mp3”, true )

local backgroundbg2 = display.newImage(“gnomiessplashbg2.png”)
group:insert(backgroundbg2)
backgroundbg2.x = display.contentWidth / 2
backgroundbg2.y = display.contentHeight / 2
–BACKGROUND IMAGES END

–BUTTONS

local worlds = display.newImage(“gnomiesPlaybutton.png” )
group:insert(worlds)

worlds.x = display.contentWidth / 1.9
worlds.y = display.contentHeight / 2.1

local options = display.newImage(“gnomiesOptionsbutton.png” )
group:insert(options)

options.x = display.contentWidth / 1.9
options.y = display.contentHeight / 1.5

local about = display.newImage(“gnomiesAboutbutton.png” )
group:insert(about)

about.x = display.contentWidth / 1.9
about.y = display.contentHeight / 1.16

–END BUTTONS

local function playtouched (event)
if (“ended” == event.phase) then
storyboard.gotoScene( “worlds”, “fade” )
end
end

worlds:addEventListener (“touch”, playtouched)

local function optionstouched (event)
if (“ended” == event.phase) then
storyboard.gotoScene(“options”, “fade” )
end
end

options:addEventListener (“touch”, optionstouched)

local function abouttouched (event)
if (“ended” == event.phase) then
storyboard.gotoScene( “about”, “fade” )
end
end

about:addEventListener (“touch”, abouttouched)
end

–STARS

display.setStatusBar( display.HiddenStatusBar )

_W = display.contentWidth; --Returns Screen Width
_H = display.contentHeight; --Returns Screen Height
local starTable = {} – Set up star table
function initStar()
local star1 = {}
star1.imgpath = “star1.png”; --Set Image Path for Star
star1.movementSpeed = 10000; --Determines the movement speed of star
table.insert(starTable, star1); --Insert Star into starTable

local star2 = {}
star2.imgpath = “star2.png”;
star2.movementSpeed = 12000;
table.insert(starTable, star2);

local star3 = {}
star3.imgpath = “star3.png”;
star3.movementSpeed = 14000;
table.insert(starTable, star3);
end --END initStar()

function getRandomStar()
local temp = starTable[math.random(1, #starTable)] – Get a random star from starTable
local randomStar = display.newImage(temp.imgpath) – Set image path for object
group:insert(star1)
group:insert(star2)
group:insert(star3)
group:insert(randomStar)

randomStar.myName = “star” – Set the name of the object to star
randomStar.movementSpeed = temp.movementSpeed; – Set how fast the object will move
randomStar.x = math.random(0,_W) – Set starting point of star at a random X position
randomStar.y = -50 – Start the star off screen
randomStar.rotation = math.random(0,360) – Rotate the object
starMove = transition.to(randomStar, {
time=randomStar.movementSpeed,
y=_H,
onComplete = function(self) self.parent:remove(self); self = nil; end
}) – Move the star

end–END getRandomStar()

function startGame()
starTimer1 = timer.performWithDelay(1700,getRandomStar, 0)
starTimer2 = timer.performWithDelay(2300,getRandomStar, 0)
starTimer3 = timer.performWithDelay(2700,getRandomStar, 0)
end–END startGame()

initStar()
startGame()
–STARS

function scene:enterScene( event )
local group = self.view

end

function scene:exitScene( event )
local group = self.view

end

function scene:destroyScene( event )
local group = self.view

end
scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )

return scene
[/code] [import]uid: 72845 topic_id: 34466 reply_id: 137284[/import]

OK, got it… your createScene function starts on line 7 and ends on line 93. So, Lua doesn’t know what “group” is, because you’ve defined that locally within the createScene function. Thus, what you place below (the placement of stars and such) doesn’t recognize “group” which remains solely in the scope of the createScene function.

I hope I’m not presuming unfairly that you’re a “beginner”, but if you’d like to read this article by Rob Miracle about Lua scope, it goes into much greater detail than I’m writing here.
http://omnigeek.robmiracle.com/2011/10/14/understanding-scope-for-beginning-programmers/

Basically, you need to place your stars and all that in the createScene function. While it’s perfectly acceptable to write functions outside of Storyboard’s “control” functions, the normal practice is that anything you wish to clear (display objects) from a scene should be placed in Storyboard’s self.view display group or a sub-group of it. Persistent items, i.e. a UI health bar or something that you wish to keep around, can be defined outside of Storyboard and they’ll remain from scene to scene.

Hope this helps!
Brent
[import]uid: 200026 topic_id: 34466 reply_id: 137294[/import]

You’re 100% right about me being a beginner. I appreciate the link and will definitely be reading over it. I’m pretty sure I understand what your saying about things needing to be in the group IE. I moved the “end” from line 93 and moved it to line 155 so everything would be in the scene. However after doing so my “stars” still just appear in a cluster in the upper left hand corner. Is this because of the “group” I’m assigning them to? (global not local - from what I gather) [import]uid: 72845 topic_id: 34466 reply_id: 137305[/import]

Is the Terminal showing any new errors? Did you remove lines 127-129 where you’re trying to insert “star1”, “star2”, and “star3” into the group? Those are illegal variables in that scope, because you’ve declared them only in the function “initStar”. So, if Corona is throwing an error because of that, let me know…

Also, as a side note, you should make all of your functions local as well. Global is considered “bad practice” in Lua, although on occasion you might need to use some global variables. While, at first glance, it might seem that the Storyboard functions aren’t local because they’re not preceded by “local”, they are in fact local because they’re methods of “scene”, which is a local object.

Yes, it can seem tricky, all this local vs. global and scoping stuff, but once you get accustomed to it, it’ll make complete sense and I think you’ll enjoy Lua alot. :slight_smile:

Brent [import]uid: 200026 topic_id: 34466 reply_id: 137307[/import]

I’m starting to feel like I’m never going to get this language lol. And to be perfectly honest I have no idea where to add the groups at this point : /
Functions and globals and locals oh my! [import]uid: 72845 topic_id: 34466 reply_id: 137313[/import]

Ha ha, don’t worry… “we’re gonna get through this!”. One step at a time; we all started from the beginning at some point, myself included.
[import]uid: 200026 topic_id: 34466 reply_id: 137315[/import]

In regards to destroy scene and exit scene, I’ve got my levels all set up but if I select let’s say world1level1 and then go to my home screen, my world1level1 is just as it was left. IE not resetting. no idea why : ( [import]uid: 72845 topic_id: 34466 reply_id: 137996[/import]

Hi FL,
Which method are you using when you exit the scene, “purge” or “remove”? The difference is that “purge” will retain the scene in the background memory, unless memory gets chewed up to the point where it can’t remain in memory, then it will be removed. Purging is useful if you’re going quickly back and forth between scenes, and you want to maintain each one for fast loading.

“remove” completely clears the scene and removes the module from Lua’s memory. If you don’t intend to come back to the scene anytime soon, and you want to free up its memory, remove it instead of purging it.

Brent [import]uid: 200026 topic_id: 34466 reply_id: 138341[/import]

I’m using purge scene but I’ve figured out my problem isn’t with the purging/removal of the scene but that I have another error making things operate funny.

This is the error I get in the terminal:
2013-01-10 16:01:03.670 Corona Simulator[388:903] WARNING: Attempting to set property(y) with nil
2013-01-10 16:01:03.670 Corona Simulator[388:903] WARNING: Attempting to set property(x) with nil

which in turn is making it so every time I purge a scene my sprite appears to hover instead of falling to the ground as it should. Not really sure why this is happening but I’ll post the sprite code and maybe you can see where the problem lies.

local data = require("marios").getSpriteSheetData();  
local sheet = sprite.newSpriteSheetFromData("marios.png",data);  
local set = sprite.newSpriteSet(sheet, 1, 16);  
  
local screen = display.newGroup()  
local instance = sprite.newSprite(set);  
instance.x = display.contentWidth/70   
physics.addBody(instance, {bounce = 0.2, friction = .1})  
 group:insert(screen)  
instance.name = "mario"  
instance.angularDamping = 5000  
instance.isFixedRotation = true  
instance.isAlive = yes  

Not even sure if the sprite is the problem being that I don’t have a code line number to reference.
And thanks again Brent for helping me out. You definitely have a special thanks in my credits. [import]uid: 72845 topic_id: 34466 reply_id: 138357[/import]

In regards to destroy scene and exit scene, I’ve got my levels all set up but if I select let’s say world1level1 and then go to my home screen, my world1level1 is just as it was left. IE not resetting. no idea why : ( [import]uid: 72845 topic_id: 34466 reply_id: 137996[/import]

Hi FL,
Cool, name in the credits! More than I would deserve… I’d just like to see your game making forward progress to market. :slight_smile:

So, this could be a few things, but first we need to track it down. Start by searching your entire document for “.x” and “.y” (don’t include the quotes). This should turn up where those errors are… and which line(s) it occurs on. Once you find that, let me know what code is around it. Probably a very simple fix!

Brent [import]uid: 200026 topic_id: 34466 reply_id: 138557[/import]