external module and destroy

hi, 

i would to call the red background and next destroy him and next call the green background 

i thought that with

package.loaded[“level”] = nil

my background will be destroy but not.

Can you tell me why ?

thanks in advance. 

--main.lua levelFunctions = require "level" levelFunctions[1]() --red background local function destroy() package.loaded["level"] = nil end timer.performWithDelay(4000, destroy() ) levelFunctions[2]() --green background local character =  display.newCircle( 100, 100, 30 ) background.touch = function( event )         if event.phase == "began" then transition.to( character, {time=10, x=event.x, y=event.y} )         end end Runtime:addEventListener( "touch", background.touch)

    

--level.lua local levelFunctions = {}; levelFunctions[1] = function() background = display.newRect( 0, 0, 320, 480 ) background:setFillColor( 1, 0, 0 ) background.x=160 background.y=240 return background end levelFunctions[2] = function() background = display.newRect( 0, 0, 320, 480 ) background:setFillColor( 0, 1, 0 ) background.x=160 background.y=240 return background end return levelFunctions

packages are loaded modules of lua code ; if you remove it from the package all it does is make it unavaileble. To make a background disappear you have to physically remove it e.g. background:removeSelf()

ok thanks for your help 

I understand the difference i put the solution if never it’s help something else

--main.lua levelFunctions = require "level" levelFunctions[1]() --red background local function listener( event ) display.remove( background ) -- by the doc it's faster than background:removeSelf() end timer.performWithDelay( 1000, listener ) levelFunctions[2]() --green background local character =  display.newCircle( 100, 100, 30 ) background.touch = function( event )         if event.phase == "began" then transition.to( character, {time=10, x=event.x, y=event.y} )         end end Runtime:addEventListener( "touch", background.touch)

--level.lua local levelFunctions = {}; --redbackground levelFunctions[1] = function() background = display.newRect( 0, 0, 320, 480 ) background:setFillColor( 0, 1, 0 ) background.x=160 background.y=240 return background end --green background levelFunctions[2] = function() background = display.newRect( 0, 0, 320, 480 ) background:setFillColor( 1, 0, 0 ) background.x=160 background.y=240 return background end return levelFunctions

packages are loaded modules of lua code ; if you remove it from the package all it does is make it unavaileble. To make a background disappear you have to physically remove it e.g. background:removeSelf()

ok thanks for your help 

I understand the difference i put the solution if never it’s help something else

--main.lua levelFunctions = require "level" levelFunctions[1]() --red background local function listener( event ) display.remove( background ) -- by the doc it's faster than background:removeSelf() end timer.performWithDelay( 1000, listener ) levelFunctions[2]() --green background local character =  display.newCircle( 100, 100, 30 ) background.touch = function( event )         if event.phase == "began" then transition.to( character, {time=10, x=event.x, y=event.y} )         end end Runtime:addEventListener( "touch", background.touch)

--level.lua local levelFunctions = {}; --redbackground levelFunctions[1] = function() background = display.newRect( 0, 0, 320, 480 ) background:setFillColor( 0, 1, 0 ) background.x=160 background.y=240 return background end --green background levelFunctions[2] = function() background = display.newRect( 0, 0, 320, 480 ) background:setFillColor( 1, 0, 0 ) background.x=160 background.y=240 return background end return levelFunctions