How to remove random objects when shifting storyboard?

I have a problem on removing random objects (crates) falling from the sky when I go to menu. Whenever I’m on game.lua and shift to menu.lua, those crates are still falling from the sky like I’m still on game.lua.

I used object:removeSelf(), still nothing. Help please guys … … …

You have to insert them inside storyboard scene’s view

yes sir i placed those objects inside game.lua … i used math.random() … i tried using physics.stop , but still nothing. Those random objects just stopped and when I shift to menu.lua, they’re still there…

Ah, so you just shifted scene not purged it. So it is understandable behaviour. You have to purge scene to get it remove objects or work a way to reload scene from begining.

hello piotrz … i tried storyboard.purgeScene() and storyboard.removeScene() … those objects are still there … im trying everything just to remove these objects.

Code?  

local FallingCrates

    function spawnCrate(event)

                --rand = math.random(12)

            --if (rand < 6 ) then

            Crate1 = display.newImage (“crate.png”);

            Crate1.x = 70 + math.random(155)

            Crate1.y = -20

            Crate1.rotation = 1

            physics.addBody (Crate1, {density=0.3,bounce=0.5,friction=0.5})

            physics.setGravity (0,4)

    

    end

    

    

function scene:createScene (event)

physics.start()

    local group = self.view

—Code—

function scene:enterScene( event )

    local group = self.view

        physics.start()

    local rectangle = display.newRect( -70, 467, 1200, 5 )

        rectangle:setFillColor(0,0,0)

        rectangle.alpha = 0

        physics.addBody( rectangle, “static”,{ friction=0.5, bounce=0.3 })

        

    FallingCrates    = timer.performWithDelay (500,spawnCrate,10)

        Restart:addEventListener (“tap”,Restart)

        Menu:addEventListener (“tap”, Menu)

        storyboard.purgeScene (“restart”)

        storyboard.purgeScene (“menu”)

    print( “Game: enterScene event” )

            

end

How can I remove those random objects falling??.. it seems that I need to make another storyboard to cover the random objects whenever I shift … :frowning: … any ideas guys?

Have you considered canceling your timer in the exitScene() function?

@Rob Miracle , Yes sir I did. I used timer.cancel(FallingCrates) and still, those crates are just there with their physics stopped. But the timer of those crates was cancelled. If I pressed “menu” from the game, it goes to menu.lua with those crates still hanging on the air. While the program is executing, I again pressed the “start” button (while the crates are still in the air hanging) and the crates starts falling again from the sky but these falling crates now do not collide with those crates which were hanging in the air … but they’re still there! … If I repeated what I did (without re-executing the program) , these crates keep overlapping themselves on the screen while hanging in the air. How to remove these objects sir Rob? … The timer.cancel() is working because there were no more crates falling when it enters exitScene event. But for the crates? Still, no luck of finding the solution … 

I don’t see where you’re adding the crates to the scene’s main display group…
Try adding them to the scene’s view ‘group’

I was confused, I thought they were still generating after the scene.  If they are just still on the screen, it’s because either you are not putting them in the scene’s view (group) or if you don’t want them in the group, you are not removing them when the scene exits.   You really should use storyboard as intended and put them in the scene’s group.

Since you’re spawning them in a function outside of the event handlers, you have to setup access to the view in that function.

Add:

     local group = scene.view  – note, this is NOT self.view, the function isn’t part of scene and doesn’t have a self.

to the top of your SpawnCrates() function.  The before you return from the function after creating the crate do:

     group:insert(Crate)

That should fix you up.

Ohhhh i get it now … yes you guys are right … i did not put the function into the “group” … im so newbie i didnt even realize what I forgot to place haha … thank you thank you thank you ! @Rob Miracle @Saerothir and other guys who replied … I will do that right now… (Sorry I was late in replying because I just opened my laptop today because of the whole week’s quiz at school xD)

You have to insert them inside storyboard scene’s view

yes sir i placed those objects inside game.lua … i used math.random() … i tried using physics.stop , but still nothing. Those random objects just stopped and when I shift to menu.lua, they’re still there…

Ah, so you just shifted scene not purged it. So it is understandable behaviour. You have to purge scene to get it remove objects or work a way to reload scene from begining.

hello piotrz … i tried storyboard.purgeScene() and storyboard.removeScene() … those objects are still there … im trying everything just to remove these objects.

Code?  

local FallingCrates

    function spawnCrate(event)

                --rand = math.random(12)

            --if (rand < 6 ) then

            Crate1 = display.newImage (“crate.png”);

            Crate1.x = 70 + math.random(155)

            Crate1.y = -20

            Crate1.rotation = 1

            physics.addBody (Crate1, {density=0.3,bounce=0.5,friction=0.5})

            physics.setGravity (0,4)

    

    end

    

    

function scene:createScene (event)

physics.start()

    local group = self.view

—Code—

function scene:enterScene( event )

    local group = self.view

        physics.start()

    local rectangle = display.newRect( -70, 467, 1200, 5 )

        rectangle:setFillColor(0,0,0)

        rectangle.alpha = 0

        physics.addBody( rectangle, “static”,{ friction=0.5, bounce=0.3 })

        

    FallingCrates    = timer.performWithDelay (500,spawnCrate,10)

        Restart:addEventListener (“tap”,Restart)

        Menu:addEventListener (“tap”, Menu)

        storyboard.purgeScene (“restart”)

        storyboard.purgeScene (“menu”)

    print( “Game: enterScene event” )

            

end

How can I remove those random objects falling??.. it seems that I need to make another storyboard to cover the random objects whenever I shift … :frowning: … any ideas guys?

Have you considered canceling your timer in the exitScene() function?

@Rob Miracle , Yes sir I did. I used timer.cancel(FallingCrates) and still, those crates are just there with their physics stopped. But the timer of those crates was cancelled. If I pressed “menu” from the game, it goes to menu.lua with those crates still hanging on the air. While the program is executing, I again pressed the “start” button (while the crates are still in the air hanging) and the crates starts falling again from the sky but these falling crates now do not collide with those crates which were hanging in the air … but they’re still there! … If I repeated what I did (without re-executing the program) , these crates keep overlapping themselves on the screen while hanging in the air. How to remove these objects sir Rob? … The timer.cancel() is working because there were no more crates falling when it enters exitScene event. But for the crates? Still, no luck of finding the solution …