Error after camera:destroy()

I am trying to create a reload function.

camera:destroy() composer.gotoScene( "scenes.levels.1", { effect = "fade", time = 250 } ) composer.removeScene( name )

Then at the top:

camera = perspective.createView()

After I load the scene again I get this error message.

perspective.lua:201: attempt to call method 'insert' (a nil value)

I tried debugging and the problem is with the layer number:

function view:add(obj, l, isFocus) print(l)

The code works perfectly when creating the level the first time, but breaks when trying to restart level.

Thanks.

I tried

composer.removeScene( "scenes.levels.1" ) composer.gotoScene( "scenes.levels.1", { effect = "fade", time = 250 } )

 and I get no errors, but most of screen has been overlapped by a blank square.

Hi @tharek.ali, I think what may be happening is that you’re deleting your camera when changing scenes but only creating it when building the scene. You’ll need to put your camera construction code within one of your scene show code sections. By putting it at the top of your code, you’re only building the camera when the scene is first loaded (say, the first time you go to it or after you explicitly remove it), but you’re trying to use it every time. After :destroy(), the camera is just an empty shell that you can nil out. Trying to use it will result in errors because it’s no longer functional.

local perspective = require("perspective") local camera function scene:show(event) if "will" == event.phase then camera = perspective.createView() end end -- Continue as normal

Let me know if that helps.

  • Caleb

I tried

composer.removeScene( "scenes.levels.1" ) composer.gotoScene( "scenes.levels.1", { effect = "fade", time = 250 } )

 and I get no errors, but most of screen has been overlapped by a blank square.

Hi @tharek.ali, I think what may be happening is that you’re deleting your camera when changing scenes but only creating it when building the scene. You’ll need to put your camera construction code within one of your scene show code sections. By putting it at the top of your code, you’re only building the camera when the scene is first loaded (say, the first time you go to it or after you explicitly remove it), but you’re trying to use it every time. After :destroy(), the camera is just an empty shell that you can nil out. Trying to use it will result in errors because it’s no longer functional.

local perspective = require("perspective") local camera function scene:show(event) if "will" == event.phase then camera = perspective.createView() end end -- Continue as normal

Let me know if that helps.

  • Caleb