Search code examples
luacoronasdkscenelua-tablecorona-storyboard

A table does not get deleted on scene change, CoronaSDK Storyboard


Basically, I have a table that has images that move horizontally, when I change scenes it stays there, even though I didn't call for it.. As in, on there is no code for it in offlinemode.lua.. I'm thinking that for some reason it's a global element..

Here is main.lua:

    local storyboard = require "storyboard"
    local options =
    {
        effect = "slideLeft",
        time = 800
    }
    storyboard.gotoScene( ".Lua.MainMenu", options )

This MainMenu.lua

--//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
--/////// Unamed game (so far) main.lua is GUI/MENU
--            ////////////////////////////////////////////////////////////////////////////////////////////    //////////////////////////////////////////////
--implements the storybord then deletes it upon completeion--
local storyboard = require "storyboard"
storyboard.purgeOnSceneChange = true
--You may began now-- (incorrectly speeleed corrctly)
local scene = storyboard.newScene()

--makes a container for the background--
-- createScene event simply creates a background image
function scene:createScene( event )
local bg = display.newImage( self.view, "background.png" )
end
scene:addEventListener( "createScene" )
--end of aking a container for the background--

--disables the status bar--
display.setStatusBar( display.HiddenStatusBar ) 
--end of disabling of status bar--

--Returns Screen Width and Screen Height--
_W = display.contentWidth; 
_H = display.contentHeight;
--end of returning screen width and height--


--////////////////////////////////////////////////end of the background\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\--
--**********************************************************************************************************************--

--////////////////////////////////////////////////Menu Buttons\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\--
--Set up a local container for the buttons--
local widget = require( "widget" )
local PlayBtn

local function onPlayBtnRelease()

    -- go to level1.lua scene
    storyboard.gotoScene( ".Lua.offlinemode", "slideLeft", 500 )
    display.remove(initstar)
    return true -- indicates successful touch
end
    local PlayBtn = widget.newButton
{
   left = 160,
   top = 80,
   label = "Offline",
   labelAlign = "center",
   font = "Arial",
   fontSize = 18,
   labelColor = { default = {0,0,0}, over = {255,255,255} },
   onRelease = onPlayBtnRelease
  }
-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )
--********************************************************************************************************************--
--///////////////////////////////////////Setting up         background\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\--
--set up the table to store the clouds in--
    local starTable = {}
--end of the table to store the clouds in--

--creates three containers with a picture of a cloud--
function initStar()
    local star1 = {}
    star1.imgpath = "/images/Cloud1.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 = "/images/cloud2.png";
    star2.movementSpeed = 12000;
    table.insert(starTable, star2);             

    local star3 = {}
    star3.imgpath = "/images/cloud3.png";
    star3.movementSpeed = 14000;
    table.insert(starTable, star3);
end
--ends the function--

--Gets random stars from the table, positioning them randomly--
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.y = math.random(10,_H) -- Set starting point of star at a random X position
    randomStar.x = -40; -- Start the star off screenm
    starMove = transition.to(randomStar, { --Move the Clouds
        time=randomStar.movementSpeed, --sets the speed relative to it's lifetime
        onComplete = function(self) self.parent:remove(self); self = nil; end, --        removes itself
        x=500 --speed of ?
        }) -- Move the Clouds
end
--The end of this function--

--Starts the timer before the Clouds "spawn"--
function startGame()
        starTimer1 = timer.performWithDelay(1070,getRandomStar, 0)
        starTimer2 = timer.performWithDelay(2030,getRandomStar, 0)
        starTimer3 = timer.performWithDelay(2070,getRandomStar, 0)      
end
--ends the function--

initStar()
startGame()
-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )

-- "exitScene" event is dispatched whenever before next scene's transition begins
scene:addEventListener( "exitScene", scene )

-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
function scene:didExitScene( event )
storyboard.purgeScene( "scene1" )
end
scene:addEventListener( "didExitScene" )
return scene
--end the Online function--

and finally offlinemode.lua:

local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local background = display.newImage("background.png")
local image = display.newImage("/images/ButtonStartOffline.png")
function scene:createScene( event )

end

function scene:enterScene( event )

end  

function scene:exitScene( event )

end

function scene:destroyScene( event )

end

scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )


return scene

If it works, tell me why it didn't for me please..


Solution

  • you should put the image to the display group either in createScene or enterScene so when you go to another scene it will remove all the image within that scene here's the implementation of the it see the code

    function scene:createScene( event )
        local group = self.view
    
     --/////////////////////Setting up background\\\\\\\\\\\\\\\\\\--
            --set up the table to store the clouds in--
         local starTable = {}
            --end of the table to store the clouds in--
    
        --creates three containers with a picture of a cloud--
        function initStar()
            local star1 = {}
            star1.imgpath = "/images/Cloud1.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 = "/images/cloud2.png";
            star2.movementSpeed = 12000;
            table.insert(starTable, star2);             
    
            local star3 = {}
            star3.imgpath = "/images/cloud3.png";
            star3.movementSpeed = 14000;
            table.insert(starTable, star3);
        end
        --ends the function--
    
        --Gets random stars from the table, positioning them randomly--
        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.y = math.random(10,_H) -- Set starting point of star at a random X position
            randomStar.x = -40; -- Start the star off screenm
            group:insert(randomStart) **-- inserting random star into Group**
            starMove = transition.to(randomStar, { --Move the Clouds
                time=randomStar.movementSpeed, --sets the speed relative to it's lifetime
                onComplete = function(self) self.parent:remove(self); self = nil; end, --        removes itself
                x=500 --speed of ?
                }) -- Move the Clouds
        end
        --The end of this function--
    
        --Starts the timer before the Clouds "spawn"--
        function startGame()
                starTimer1 = timer.performWithDelay(1070,getRandomStar, 0)
                starTimer2 = timer.performWithDelay(2030,getRandomStar, 0)
                starTimer3 = timer.performWithDelay(2070,getRandomStar, 0)      
        end
        --ends the function--
    
        initStar()
        startGame()
    
    
    end