Remove spawnobjects function on exitScene()?

So the function below spawns dustbins. in

function spawnDustbin() i = 0 dustbin = {}     i = i + 1     dustbin[i] = display.newImage("dustbin.png")     dustbin[i].xScale = 0.55     dustbin[i].yScale = 0.55     dustbin[i].y = 555     dustbin[i].x = (math.random(1000,1100) \* i)     dustbin[i].speed = 4     physics.addBody( dustbin[i], "static", { friction=1.0, density=1.0, bounce=0, radius=60} )     dustbin[i].enterFrame = moveDustbin     Runtime:addEventListener("enterFrame", dustbin[i]) end  

in enter scene the line:

timer.performWithDelay(1000, spawnDustbin, 14)

Calls the function but how do I remove the dustbins off the scene on exitscene  all help welcome!

Thank you 

James

If you add them to your scene view (by default this is called group in storyboard) then you don’t need to remove them manually. 

[lua]

group:insert(dustbin[i])

[/lua]

James, I gave you this:

local dustbin = {} local i = 0 local function spawnDustbin() i = i+1 dustbin[i] = display.newImage("dustbin.png") dustbin[i].xScale = 0.55 dustbin[i].yScale = 0.55 dustbin[i].y = 555 dustbin[i].x = (math.random(1000,1500) \* i) dustbin[i].speed = 4 physics.addBody( dustbin[i], "static", { friction=1.0, density=1.0, bounce=0, radius=30,} ) dustbin[i].enterFrame = moveDustbin Runtime:addEventListener("enterFrame", dustbin[i]) end timer.performWithDelay(500, spawnDustbin, 10)

and you changed it to this:

function spawnDustbin() i = 0 dustbin = {} i = i + 1 dustbin[i] = display.newImage("dustbin.png") dustbin[i].xScale = 0.55 dustbin[i].yScale = 0.55 dustbin[i].y = 555 dustbin[i].x = (math.random(1000,1100) \* i) dustbin[i].speed = 4 physics.addBody( dustbin[i], "static", { friction=1.0, density=1.0, bounce=0, radius=60} ) dustbin[i].enterFrame = moveDustbin Runtime:addEventListener("enterFrame", dustbin[i]) end

Why did you change the code there? I’m not saying you’re wrong, but you’re going to run into problems when you attempt to remove your dustbins. 

Hey no the line of code that nick provided above worked perfectly I now have no issues with the removal of dustbins! 

Thank You for your help though that saved me.

James

Sorry you  were right! when i finish the scene then go back to the scene when i click my restart button it just shows me dustbins no skater somethings wrong here…

So why did you change the code I provided? Can you describe exactly what you’re attempting to accomplish and include any pertinent code snippets so that we can identify the issue and provide assistance?

Okay so this is it:

My skateboarder is going to jump over a trash can, he does that perfectly with no issue and can jump over the other trash cans without issue. 

On collision with a trash can the skateboarder is going to crumble into pieces which will fall to the floor. At the moment Im working on that so on collison the app goes to the intro scene and then to the menu and then when i click play as soon as my skateboarder jumps the screen goes to the intro again!

I have sent you the code via private message, here are the screenshots:

everything is perfect trash cans move normally etc

qGf8f1A.png
 

The skater is stuck the background isnt scrolling the trash cans arent moving etc.

cfx1b8T.png

Thankyou so much

James

While we’re waiting for you to post your code, I can guarantee you aren’t removing your listeners when you leave your scene. Try using print statements in your gameloop/Runtime listeners to check to see if they are stopped when you leave the scene.

    Recommended reading:

        - http://coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

        - http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

I sent you my code via private message but ill have a look at this and try by myself atm

Thanks!

James

Assisting via private message isn’t really the intent of the forums. Rest assured, there are enough Flappy Bird clones and free templates that exist that no one will beat you to the punch. In order to support the community and make sure that I provide information from which everyone can benefit, posting your code in this thread is the only way I can provide assistance.

I suppose you’re right!

Okay so this is my game code:

Looking at your code, I see a lot of globals. It is of vital importance that you read through the “goodbye globals” tutorial so that you can structure your code in such a way that doesn’t breed memory leaks, and makes it easier to reload your scene. Check out the Storyboard Sample Project to see how best to declare local variables and so on.

Have you tried adding print statements to your Runtime listeners to make sure that they are in fact being removed when you leave the scene? This will print info to the terminal and tell you whether or not all of your listeners are being removed.

Ill do that now and see if it works!

If you add them to your scene view (by default this is called group in storyboard) then you don’t need to remove them manually. 

[lua]

group:insert(dustbin[i])

[/lua]

James, I gave you this:

local dustbin = {} local i = 0 local function spawnDustbin() i = i+1 dustbin[i] = display.newImage("dustbin.png") dustbin[i].xScale = 0.55 dustbin[i].yScale = 0.55 dustbin[i].y = 555 dustbin[i].x = (math.random(1000,1500) \* i) dustbin[i].speed = 4 physics.addBody( dustbin[i], "static", { friction=1.0, density=1.0, bounce=0, radius=30,} ) dustbin[i].enterFrame = moveDustbin Runtime:addEventListener("enterFrame", dustbin[i]) end timer.performWithDelay(500, spawnDustbin, 10)

and you changed it to this:

function spawnDustbin() i = 0 dustbin = {} i = i + 1 dustbin[i] = display.newImage("dustbin.png") dustbin[i].xScale = 0.55 dustbin[i].yScale = 0.55 dustbin[i].y = 555 dustbin[i].x = (math.random(1000,1100) \* i) dustbin[i].speed = 4 physics.addBody( dustbin[i], "static", { friction=1.0, density=1.0, bounce=0, radius=60} ) dustbin[i].enterFrame = moveDustbin Runtime:addEventListener("enterFrame", dustbin[i]) end

Why did you change the code there? I’m not saying you’re wrong, but you’re going to run into problems when you attempt to remove your dustbins. 

Hey no the line of code that nick provided above worked perfectly I now have no issues with the removal of dustbins! 

Thank You for your help though that saved me.

James

Sorry you  were right! when i finish the scene then go back to the scene when i click my restart button it just shows me dustbins no skater somethings wrong here…

So why did you change the code I provided? Can you describe exactly what you’re attempting to accomplish and include any pertinent code snippets so that we can identify the issue and provide assistance?

Okay so this is it:

My skateboarder is going to jump over a trash can, he does that perfectly with no issue and can jump over the other trash cans without issue. 

On collision with a trash can the skateboarder is going to crumble into pieces which will fall to the floor. At the moment Im working on that so on collison the app goes to the intro scene and then to the menu and then when i click play as soon as my skateboarder jumps the screen goes to the intro again!

I have sent you the code via private message, here are the screenshots:

everything is perfect trash cans move normally etc

qGf8f1A.png
 

The skater is stuck the background isnt scrolling the trash cans arent moving etc.

cfx1b8T.png

Thankyou so much

James

While we’re waiting for you to post your code, I can guarantee you aren’t removing your listeners when you leave your scene. Try using print statements in your gameloop/Runtime listeners to check to see if they are stopped when you leave the scene.

    Recommended reading:

        - http://coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

        - http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/