Object:removeSelf() never ending problem

Hi!
I read too much about this subject, but i can’t find solution to my problem

function envirement()  
(...)  
 local reset = display.newImage("\_img/reset.jpg",900,100,true)  
  
 reset.id = resetuje  
 Gra:insert(reset)  
  
  
 reset:addEventListener("touch", init )  
end  
  
function level1()  
 envirement()  
 --result(solidTotal, pins)  
 setCueball(100,\_H/2)   
  
 setPinPoz(300, 150)  
 setPinPoz(300, 600)  
  
 setPockets(700,400)  
  
end  
function splash()  
 splashGroup = display.newGroup()  
  
 local splashBG = display.newImage("\_img/tlo1.png", true, 20, 1, true)  
 splashGroup:insert(splashBG)  
  
 local btnplay = display.newImage( "\_img/graj.png", 200, 550, true)  
 btnplay.id = play  
 splashGroup:insert(btnplay)  
  
 btnplay:addEventListener("touch", init)  
end  
function init( event )  
  
 mode = event.target.id  
  
 if mode == play then  
 print "splash"  
 339.splashGroup:removeSelf()  
 timer.performWithDelay(0, level1, 1)   
 elseif mode == resetuje then  
 print "level"  
 344.Gra:removeSelf()   
 timer.performWithDelay(0, splash, 1)  
 end  
  
end  
splash()  

Corona simulator output give me :

splash level Runtime error d:\aacorona\gra\main.lua:344: attempt to index global 'Gra' (a nil value ) stack traceback: [C]: ? d:\aacorona\gra\main.lua:344: in function <d:> <br>?: in function <?:229> splash Runtime error d:\aacorona\gra\main.lua:339: attempt to call method 'removeSelf' (a nil value) <br>stack traceback: <br>[C]: in function 'removeSelf' d:\aacorona\gra\main.lua:339: in function <d:> <br> ?: in function <?:229> <br>splash <br>Runtime error d:\aacorona\gra\main.lua:339: attempt to call method 'removeSelf' (a nil value) <br>stack traceback: <br>[C]: in function 'removeSelf' d:\aacorona\gra\main.lua:339: in function <d:> <br> ?: in function <?:229><br> [import]uid: 117910 topic_id: 35315 reply_id: 335315[/import] </d:></d:></d:>

A couple of problems here.

  1. You don’t show the code where Gra is created. The first error message says “Gra == nil”, which means that Corona cannot find Gra.

  2. What is 344.Gra? or 339.splashGroup? I’m not sure you can make numbers into tables…? [import]uid: 41884 topic_id: 35315 reply_id: 140368[/import]

1.I cut some code but in function envirement i have

function envirement()  
Gra = display.newGroup()  
(...)  
 local reset = display.newImage("\_img/reset.jpg",900,100,true)  
  
 reset.id = resetuje  
 Gra:insert(reset)  
   
  
 reset:addEventListener("touch", init )  
end  
  1. 344 and 339 number of line in my source, simulator put this line. [import]uid: 117910 topic_id: 35315 reply_id: 140376[/import]

The problem is basically scope then. Your functions can’t find Gra.

I would declare outside of the functions. Put local Gra outside of any function, before envirement().Near the beginning of the file if possible. That way you can guarantee all of the functions after can see it. (If you want to make Gra a global instead, say \_G.gra = nil) [import]uid: 41884 topic_id: 35315 reply_id: 140378[/import]

When i initialization gra outside function level1 don’t start with global \_G.gra simulator output give me the same problem

e:

function init( event )  
  
 mode = event.target.id  
  
 if mode == play then  
 if splashGroup == nil then  
 print "splash"  
 end  
 splashGroup:removeSelf()  
 timer.performWithDelay(0, level1, 1)  
 end   
 if mode == resetuje then  
 if Gra == nil then  
 print "level"  
 end  
 Gra:removeSelf()   
 timer.performWithDelay(0, splash, 1)  
 end   
end  

now i can see only level in console what i can do wrong? In splash in the same way it’s work [import]uid: 117910 topic_id: 35315 reply_id: 140388[/import]

Try this…I saw a bunch of different possible problems, added my fixes in comments.

[code]-- your file
local splashGroup, Gra – predeclare your group objects
local init, splash, level1, envirement – predeclare your functions. This keeps them local

function envirement()
(…) – obviously this won’t work until you put your code here
local reset = display.newImage(Gra, “_img/reset.jpg”, 900, 100, true) – put your img into displayGroup with this
reset.id = “resetuje”
reset:addEventListener(“touch”, init)
end
function level1()
envirement()
setCueball( 100, _H/2 )
setPinPoz( 300, 150 )
setPockets( 700, 400 )
end

function init( event )
local mode = event.target.id

if mode == “play” then – if you don’t use a string, play = nil
print(“splash”)
display.remove(splashGroup) – display.remove() is the same thing as :removeSelf() but it checks to make sure the obj exists first!
timer.performWithDelay(10, level1) – why were you setting a delay of zero?
elseif mode == “resetuje” then
print(“level”)
display.remove(Gra)
timer.performWithDelay(10, splash) – the 1 at the end is unnecessary
end
end

function splash()
local splashGroup = display.newGroup()
local splashBG = display.newImage(splashGroup, “_img/tlo1.png”, true, 20, 1, true) – you can insert into the display group by specifying the group first
local btnplay = display.newImage(splashGroup, “_img/graj.png”, 200, 550, true)
btnplay.id = “play” – this makes play a string
btnplay:addEventListener(“touch”, init)
end

splash()[/code] [import]uid: 41884 topic_id: 35315 reply_id: 140391[/import]

Ohh my God :slight_smile: Thanks a lot for yout time and patience! It’s work! [import]uid: 117910 topic_id: 35315 reply_id: 140393[/import]

A couple of problems here.

  1. You don’t show the code where Gra is created. The first error message says “Gra == nil”, which means that Corona cannot find Gra.

  2. What is 344.Gra? or 339.splashGroup? I’m not sure you can make numbers into tables…? [import]uid: 41884 topic_id: 35315 reply_id: 140368[/import]

1.I cut some code but in function envirement i have

function envirement()  
Gra = display.newGroup()  
(...)  
 local reset = display.newImage("\_img/reset.jpg",900,100,true)  
  
 reset.id = resetuje  
 Gra:insert(reset)  
   
  
 reset:addEventListener("touch", init )  
end  
  1. 344 and 339 number of line in my source, simulator put this line. [import]uid: 117910 topic_id: 35315 reply_id: 140376[/import]

The problem is basically scope then. Your functions can’t find Gra.

I would declare outside of the functions. Put local Gra outside of any function, before envirement().Near the beginning of the file if possible. That way you can guarantee all of the functions after can see it. (If you want to make Gra a global instead, say \_G.gra = nil) [import]uid: 41884 topic_id: 35315 reply_id: 140378[/import]

When i initialization gra outside function level1 don’t start with global \_G.gra simulator output give me the same problem

e:

function init( event )  
  
 mode = event.target.id  
  
 if mode == play then  
 if splashGroup == nil then  
 print "splash"  
 end  
 splashGroup:removeSelf()  
 timer.performWithDelay(0, level1, 1)  
 end   
 if mode == resetuje then  
 if Gra == nil then  
 print "level"  
 end  
 Gra:removeSelf()   
 timer.performWithDelay(0, splash, 1)  
 end   
end  

now i can see only level in console what i can do wrong? In splash in the same way it’s work [import]uid: 117910 topic_id: 35315 reply_id: 140388[/import]

Try this…I saw a bunch of different possible problems, added my fixes in comments.

[code]-- your file
local splashGroup, Gra – predeclare your group objects
local init, splash, level1, envirement – predeclare your functions. This keeps them local

function envirement()
(…) – obviously this won’t work until you put your code here
local reset = display.newImage(Gra, “_img/reset.jpg”, 900, 100, true) – put your img into displayGroup with this
reset.id = “resetuje”
reset:addEventListener(“touch”, init)
end
function level1()
envirement()
setCueball( 100, _H/2 )
setPinPoz( 300, 150 )
setPockets( 700, 400 )
end

function init( event )
local mode = event.target.id

if mode == “play” then – if you don’t use a string, play = nil
print(“splash”)
display.remove(splashGroup) – display.remove() is the same thing as :removeSelf() but it checks to make sure the obj exists first!
timer.performWithDelay(10, level1) – why were you setting a delay of zero?
elseif mode == “resetuje” then
print(“level”)
display.remove(Gra)
timer.performWithDelay(10, splash) – the 1 at the end is unnecessary
end
end

function splash()
local splashGroup = display.newGroup()
local splashBG = display.newImage(splashGroup, “_img/tlo1.png”, true, 20, 1, true) – you can insert into the display group by specifying the group first
local btnplay = display.newImage(splashGroup, “_img/graj.png”, 200, 550, true)
btnplay.id = “play” – this makes play a string
btnplay:addEventListener(“touch”, init)
end

splash()[/code] [import]uid: 41884 topic_id: 35315 reply_id: 140391[/import]

Ohh my God :slight_smile: Thanks a lot for yout time and patience! It’s work! [import]uid: 117910 topic_id: 35315 reply_id: 140393[/import]