Timer.performWithDelay Bug?

Hi gurus,
 
 
I am creating a board game (drag-select-break)
Basically when i first start, the function work well
 
Here my code

     

   local function delayBreak(block)

            print(‘line 388 call delayBreak(block)’)

            block:breakme()            

            – remove from instance array

            GTK.TILE_INSTANCES[block.id] = nil

        end

for k in pairs (GTK.TILE_COLLECTIVE) do

    local block = GTK.TILE_COLLECTIVE[k]
    print(‘looping block.id =’ … block.id)

    local closure    = function() return delayBreak(GTK.TILE_COLLECTIVE[k]) end
    local closure2    = function() return delaySpawn(GTK.TILE_COLLECTIVE[k]) end

    – delay block break
    BOARD.TIMER.BREAK = timer.performWithDelay(delayTime, closure, 1)
    BOARD.TIMER.SPAWN = timer.performWithDelay(delayTime + 200, closure2, 1)

    – Increase break timer
    delayTime = delayTime + delayIncr

end

These code basically to delay the break & respawn of the blocks collected (GTK.TILE_COLLECTIVE).

In my first level, it work fine. But when i come to second level (scene.reload)

When i drag-select 3 blocks, the timer.performWithDelay function run TWICE…???

Here my log file

looping block.id =74    

looping block.id =75    

looping block.id =76    

line 388 call delayBreak(block)    

call BLOCK:destroy()    

self.name = RED2 id = 74    

line 388 call delayBreak(block)    

call BLOCK:destroy()    

self.name = RED2 id = 74    

line 388 call delayBreak(block)    

call BLOCK:destroy()    

self.name = RED2 id = 75    

line 388 call delayBreak(block)    

call BLOCK:destroy()    

self.name = RED2 id = 75    

line 388 call delayBreak(block)    

call BLOCK:destroy()    

self.name = RED2 id = 76    

line 388 call delayBreak(block)    

call BLOCK:destroy()    

self.name = RED2 id = 76    

i tried debug a few days but have no clue… is it some kinda bug?

Can any gurus provide some pointer?

Thanks for your patient on my bad english.

Regards,

Weilies

Hey Weilies,

I’m not sure the error was in the code you posted, but maybe someone else might see something I missed.

What I can tell you is that I’ve had a similar issue before with scene reload before, and it was because I was adding an event listener when the scene loaded and never removing it when the scene was removed.  

Best I can do for you is suggest you take a look to make sure you’re not adding the event listener more than once by mistake.

Good luck!

Thanks for the reply Daniel,

But it’s weird… what is the timer function has to do with event listener? :frowning:

Also, before storyboard reload, i have already cleanup all listeners that i am aware 

Is there any code can scan through any unwanted listener?

Or could it caused by other reason?

It’s really hard to troubleshoot when we have many modules cross calling each other lolz

Make sure you understand the pitfalls of trying to reload a storyboard scene:

http://www.coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

Its likely you’re setting a variable that’s causing your loop to execute more than you think it should and you’re expecting some initialization at the top of your storyboard scene to reset and it doesn’t.  This tutorial explains it all.

Hi Rob,

Thanks for nice & useful video.

How i resolve this is i removeSelf for the whole board display group

BOARD.LAYER:removeSelf()

   it resolve my problem!

Thanks for all the comments

Hey Weilies,

I’m not sure the error was in the code you posted, but maybe someone else might see something I missed.

What I can tell you is that I’ve had a similar issue before with scene reload before, and it was because I was adding an event listener when the scene loaded and never removing it when the scene was removed.  

Best I can do for you is suggest you take a look to make sure you’re not adding the event listener more than once by mistake.

Good luck!

Thanks for the reply Daniel,

But it’s weird… what is the timer function has to do with event listener? :frowning:

Also, before storyboard reload, i have already cleanup all listeners that i am aware 

Is there any code can scan through any unwanted listener?

Or could it caused by other reason?

It’s really hard to troubleshoot when we have many modules cross calling each other lolz

Make sure you understand the pitfalls of trying to reload a storyboard scene:

http://www.coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

Its likely you’re setting a variable that’s causing your loop to execute more than you think it should and you’re expecting some initialization at the top of your storyboard scene to reset and it doesn’t.  This tutorial explains it all.

Hi Rob,

Thanks for nice & useful video.

How i resolve this is i removeSelf for the whole board display group

BOARD.LAYER:removeSelf()

   it resolve my problem!

Thanks for all the comments