timer.performWithDelay not working inside a module

Hello. I am new to Corona SDK and LUA so thank you for your help.

I am using the director class, and my code is on a screen.lua page.
When I add the sample code below it kills my program and I get a black screen in the simulator when I switch to this screen. I had the program running by itself previously, and I am now trying to move it into your director class code so I can have various screens in my app.

From testing, the problem appears to be with the timer.

timer.performWithDelay(1000, setupgrid1, 0 )

When I change the timer to just setupgrid1() the function works, but doesn’t repeat obviously.

When I change it to read
timer.performWithDelay(1000, setupgrid1() )
it also works, but doesn’t repeat.

Finally, I tried timer.performWithDelay(1000, setupgrid1(), 0 )
and it works one time, but it does not repeat even though I have added in the 0.

Any suggestions???
Sample of code…

–Set up random grid displays in each square
function setupgrid1 ()
grid1 = math.random(8)
if grid1 == 1
then grid1loc = display.newImage(bkcolor1)
elseif grid1 == 2
then grid1loc = display.newImage(bkcolor2)
elseif grid1 == 3
then grid1loc = display.newImage(bkcolor3)
elseif grid1 == 4
then grid1loc = display.newImage(bkcolor4)
elseif grid1 == 5
then grid1loc = display.newImage(bkcolor5)
elseif grid1 == 6
then grid1loc = display.newImage(bkcolor6)
elseif grid1 == 7
then grid1loc = display.newImage(bkcolor7)
elseif grid1 == 8
then grid1loc = display.newImage(bkcolor8)
end
grid1loc.x = display.stageWidth - 258
grid1loc.y = display.stageHeight - 268
group1:insert(grid1loc)
end
timer.performWithDelay(1000, setupgrid1, 0 ) [import]uid: 15669 topic_id: 6118 reply_id: 306118[/import]

An update…
I have continued working on this. It appears that timer.performWithDelay(1000, setupgrid1, 0 ) is not crashing the app, but instead causing everything that I put into the local group to disappear. (see below). In fact it appears to slide off to the right side of the screen as if its calling one of the functions that the director class uses to clear the screen.

For example, in the code below, when background is in the localgroup, my background disappears when I try to use timer.performWithDelay(1000, setupgrid1, 0 ). When it is not in the group, it stays on the screen and everything else in the group disappears.

Please help!
_________CODE SAMPLE BELOW_______
module(…, package.seeall)

– Main function - MUST return a display.newGroup()
function new()
local localGroup = display.newGroup()

local background = display.newImage(“images/background.gif”)
localGroup:insert ( background ) [import]uid: 15669 topic_id: 6118 reply_id: 21059[/import]

@tgtotu

First, pay attention on what you are doing with your timers. They are not linear programming, they are autonomous transactions.

About the objects disappearing, that’s not what is happening. When you don’t insert objects to the localGroup, these objects will be just put it on the screen without any control. Now, if you are inserting everything right, then think that groups are layers ordered by the last object inserted.

localGroup:insert(object1)  
localGroup:insert(object2)  
localGroup:insert(object3)  

With this code, object3 will be above the others and object2 will be at the middle.

If you want to choose the places, take a look at the indexes that are explained at the Docs page. [import]uid: 8556 topic_id: 6118 reply_id: 21143[/import]

Ricardo,

Thanks for the answer, but that doesn’t seem to solve my issue.

Below are two different pieces of code. The only difference is the use of the timer to repeat the function. Example 1 causes the problem I have been describing in which everything I put into your localGroup disappears from the screen and the screen goes black. In example 2, the function works properly and everything stays on the screen, but the function only runs one time, and I need it to repeat.

Please explain why the timer function is causing this problem and how I fix it.

Thank you.

EXAMPLE 1
–Set up random grid displays in each square

local function setupgrid1 ()
grid1 = math.random(8)
if grid1 == 1
then grid1loc = display.newImage(bkcolor1)
elseif grid1 == 2
then grid1loc = display.newImage(bkcolor2)
elseif grid1 == 3
then grid1loc = display.newImage(bkcolor3)
elseif grid1 == 4
then grid1loc = display.newImage(bkcolor4)
elseif grid1 == 5
then grid1loc = display.newImage(bkcolor5)
elseif grid1 == 6
then grid1loc = display.newImage(bkcolor6)
elseif grid1 == 7
then grid1loc = display.newImage(bkcolor7)
elseif grid1 == 8
then grid1loc = display.newImage(bkcolor8)
end
grid1loc.x = display.contentWidth - 258
grid1loc.y = display.contentHeight - 268
localGroupTest:insert ( grid1loc )
localGroupTest:insert ( grid )
end

timer.performWithDelay(1000, setupgrid1, 0)

EXAMPLE 2
–Set up random grid displays in each square

local function setupgrid1 ()
grid1 = math.random(8)
if grid1 == 1
then grid1loc = display.newImage(bkcolor1)
elseif grid1 == 2
then grid1loc = display.newImage(bkcolor2)
elseif grid1 == 3
then grid1loc = display.newImage(bkcolor3)
elseif grid1 == 4
then grid1loc = display.newImage(bkcolor4)
elseif grid1 == 5
then grid1loc = display.newImage(bkcolor5)
elseif grid1 == 6
then grid1loc = display.newImage(bkcolor6)
elseif grid1 == 7
then grid1loc = display.newImage(bkcolor7)
elseif grid1 == 8
then grid1loc = display.newImage(bkcolor8)
end
grid1loc.x = display.contentWidth - 258
grid1loc.y = display.contentHeight - 268
localGroupTest:insert ( grid1loc )
localGroupTest:insert ( grid )
end

setupgrid1 () [import]uid: 15669 topic_id: 6118 reply_id: 21156[/import]

I am actually experiencing this exact same problem with the Director class.

First, I have two screen set up using Director. Tapping Screen1 calls

director:changeScene(“Screen2”,“moveFromRight”)

and tapping Screen2 calls

director:changeScene(“Screen1”,“moveFromLeft”)

this all works just great. Now I add a button (rectangle) to Screen1 and set the touch handler for the button to add a new colored rectangle to the screen and set up a timer to remove this new rectangle after 5 seconds. So the code is like this:

[lua]local rect = display.addRect(0,0,100,100)
rect:setFillColor(255,0,0)
group:insert(rect)

local function removeRect()
end

timer.performWithDelay(5000, removeRect)[/lua]

Notice that the removeRect function doesn’t even do anything yet. Now when I tap the screen, the Screen2 is shown sliding from the right, but as soon as the slide animation is complete, the screen goes black.

In fact, I have learned that I don’t even need to create the new rectangle. I can get rid of all the above code and if I just call:

timer.performWithDelay(5000, removeRect)

then tap Screen1, Screen2 will slide in and then go completely black.

So there is definitely something weird going on with multiple timers here with the Director class. This is using the latest update of Corona. Please help suggest what else I should try.
[import]uid: 12529 topic_id: 6118 reply_id: 21271[/import]

Looking into this even more, I think the bug is actually caused by the core “transition.to” routine.

If I change my tap action on Screen1 from

director:changeScene(“Screen2”,“moveFromRight”)

to

director:changeScene(“Screen2”)

then when I tap Screen1 the Screen2 will be shown without any delay and will NOT turn black. But as soon as I tap Screen2 to cause the transition back to Screen1, the Screen1 will turn black when it is done sliding.

so it’s something about calling “transition.to” in director.lua that is causing interference with other timers. This is probably something only Carlos can really look into and answer, and I’m not sure if he looks in these subforums.

I will try to condense this down to something simple Carlos can use to reproduce the problem, but I’m running out of time here. I can’t be spending all of my time trying to debug Corona issues instead of working on my own app code.

So anybody else who could help with this would be appreciated! [import]uid: 12529 topic_id: 6118 reply_id: 21277[/import]

FIXED!!

First I verified that commenting out the transition.to calls in the “moveFromRight” routine would fix the problem.

After much playing around, I finally found the problem.

The delay time in the call to transition.to is fxTime. This is also the delay given in the timer.performWithDelay. So the transition completes at the exact same time the performWithDelay routine is called. This is bad. You want to ensure that the fxEnded is called only AFTER the transition is completed.

If I change the timer.performWithDelay call to this:

timer.performWithDelay( fxTime+50, fxEnded )

then the screen no longer goes black. I tried smaller values and they didn’t work reliably. I went through every call to timer.performWithDelay in the directory.lua file and added this extra delay amount and that solved my problem.

I have no idea why just adding my own timer (and not even doing anything with it) could cause this problem. But I don’t know how timers are implemented internally in Corona either.

But certainly the Director class was assuming that the transition would be finished before fxEnded should be called.

In fact, the REAL fix to all of this would be to properly use a listener function in the transition.to call and having this listener call the fxEnded routine instead of starting yet another timer within the Director class. Although when doing that, I’m not sure how you’d properly cancel an existing transition since I don’t see anywhere that transition.to returns the timerId table that can be used with timer.cancel. [import]uid: 12529 topic_id: 6118 reply_id: 21294[/import]

MPotter if I knew where you were I would be there showering you with praise! I added the +50 to the timer.performWithDelay( fxTime+50, fxEnded ) in the director code and it seems to have fixed my problem on first trial! THANK YOU!!! [import]uid: 15669 topic_id: 6118 reply_id: 21340[/import]

@MPotter

Thanks for the help! I’m just thinking in a way to solve this issue without having to add extra delay to the timers. It should work just after the process ends, not with a delay. [import]uid: 8556 topic_id: 6118 reply_id: 21384[/import]

MPotter you are a complete star! This has had me stumped for most of this evening, as I also tried to move my main.lua into a director class file. Thank you. [import]uid: 27826 topic_id: 6118 reply_id: 22055[/import]

@MPotter I love you! [import]uid: 10426 topic_id: 6118 reply_id: 22106[/import]

I tried all of these solutions and none of them worked. Still got the black screen on my iPhone 3G. I finally was able to resolve it by removing the timer.performWithDelay() call altogether to avoid this asynch timing issue. Instead I serialized the calls and invoke onComplete of the transition to call fxEnded() to guarantee that there is no overlap. Like this:
showFx = transition.to ( currView, { x=display.contentWidth, time=fxTime, onComplete=fxEnded } )

Hope this helps someone else…
[import]uid: 35190 topic_id: 6118 reply_id: 37504[/import]

Hi

I have a similar problem - after putting the level in the director class my timers stopped working. How can I fix that? I’m developing on windows, if that’s important. Please, help)

CODE:

function removelifeone()
life_one:removeSelf()
end

function removelifetwo()
life_two:removeSelf()
end

function endgame()
azeVelocity = (0);
endgame = display.newText(“GAME OVER!”, _W/4, _H/2, “Helvetica” , 40)
localGroup:insert(endgame)
restart = display.newText(“RESTART”, _W/4, _H/4, “Helvetica” , 40)
end

tmr_one = timer.performWithDelay(2000,removelifeone,1)
tmr = timer.performWithDelay(1000,removelifetwo,1)
tmr_end = timer.performWithDelay(2000,endgame,1)

THanks)
[import]uid: 40662 topic_id: 6118 reply_id: 38615[/import]