Scene handling error bad argument #-2 to 'insert'

I have isolated an error I am getting whilst changing between scenes in my code. The error I am receiving happens at the following point -

Load main.lua > change to red scene > click red rect > change to green scene > click green rect > change to red scene *ERROR*

The error I am getting is -

[bash]

Runtime error
/Users/adambutler/Sites/coronadebug/main.lua:24: bad argument #-2 to ‘insert’ (Proxy expected, got nil)
stack traceback:
[C]: ?
[C]: in function ‘insert’
/Users/adambutler/Sites/coronadebug/main.lua:24: in function ‘changeScene’
/Users/adambutler/Sites/coronadebug/green.lua:31: in function
?: in function <?:226>

[/bash]

Here are my files as a gist (https://gist.github.com/3414734) also shown below -

main.lua
[lua]DISPLAY = display.newGroup();
function changeScene(sceneFile, data)

local mainScene = require(sceneFile); – Returns init

for i=1, DISPLAY.numChildren do

for p=1, DISPLAY[i].numChildren do
DISPLAY[i][p]:removeSelf();
end

DISPLAY[i]:removeSelf();
end

DISPLAY = display.newGroup();

local newScene = mainScene(data); – Returns scene

print(newScene);
DISPLAY:insert( newScene );
end

changeScene(‘red’, {target = “green”});[/lua]

red.lua
[lua]local scene = display.newGroup();
local target;

local groupBox = display.newGroup();

function init(data)

addGroups();
addBox();

target = data.target;

return scene;
end

function addGroups()
scene:insert(groupBox)
end

function addBox()
local box = display.newRect(groupBox, 0,0,display.contentWidth, display.contentHeight);
box:setFillColor(255,0,0);
box.alpha = 0.5;
box:addEventListener(“tap”, changeSceneEvent)
end

function changeSceneEvent(event)

print(target);

changeScene(target, {target=‘red’})
end

return init;[/lua]

green.lua
[lua]local scene = display.newGroup();
local target;

local groupBox = display.newGroup();

function init(data)

addGroups();
addBox();

target = data.target;

return scene;
end

function addGroups()
scene:insert(groupBox)
end

function addBox()
local box = display.newRect(groupBox, 0,0,display.contentWidth, display.contentHeight);
box:setFillColor(0,0,255);
box.alpha = 0.5;
box:addEventListener(“tap”, changeSceneEvent)
end

function changeSceneEvent(event)

print(target);

changeScene(target, {target=‘green’})
end

return init;[/lua]

Any help would be appreciated. [import]uid: 150415 topic_id: 30097 reply_id: 330097[/import]

when you call removeSelf on the objects in the display, you are effectively calling scene:removeSelf(). This means that red’s scene is no longer a valid display object after you switch from red to green. You have a local reference to it, but its not valid anymore. You can just recreate the scene in your init function.

However, you should look into using the storyboard module for handling scenes…it helps you with a lot of stuff and does nice things like memory management and transitions. [import]uid: 122310 topic_id: 30097 reply_id: 120479[/import]

when you call removeSelf on the objects in the display, you are effectively calling scene:removeSelf(). This means that red’s scene is no longer a valid display object after you switch from red to green. You have a local reference to it, but its not valid anymore. You can just recreate the scene in your init function.

However, you should look into using the storyboard module for handling scenes…it helps you with a lot of stuff and does nice things like memory management and transitions. [import]uid: 122310 topic_id: 30097 reply_id: 120479[/import]