timer.performWithDelay is not delaying

So, as many else I’m trying to spawn stuff with timer.performWithDelay. Instead of enemies, I have houses that spawns as my scrolling game moves along.
Spawning is taken care of, I just can’t get them to delay between each house.
I’ve taken code from other threads here in the forum and I’ve dissected to all my abilities but I can’t just understand what I’m doing wrong.

This is the spawning function:

function create\_house()  
 local ent = math.random(1,3) -- random enemy type  
 local houseImage = display.newImage("house" .. ent .. ".png")  
 houseImage.x = 308  
 houseImage.y = 108 -- the object already has X and Y  
 transition.to(houseImage, { time=6000, 0, x=houseImage.contentWidth - 30 } )  
 return houseImage  
end  

and this is the code I’m using to delay:

local newHouse = create\_house()  
timer.performWithDelay(7000,newHouse, 10)  

and I’ve put it in my move() function that moves the entire scene forward. I think that’s the problem to be honest since move() is called so many times my latter code gets called repeatedly so it doesn’t matter what delay I’m using.

Any ideas where to put the delay code? [import]uid: 21652 topic_id: 13493 reply_id: 313493[/import]

instead of

local newHouse = create\_house()  
timer.performWithDelay(7000,newHouse, 10)  

you mean

--local newHouse = create\_house() -- remove this line   
timer.performWithDelay(7000,create\_house, 10)  

not tested,. off my brain… but 2nd parameter to performWithDelay is a func.

.c [import]uid: 24 topic_id: 13493 reply_id: 49519[/import]

I’m going to make an assumption that your move() is either the function that gets called as part of an enterFrame event or is called from a function that’s part of enterFrame.

If that’s the case then move() is getting called 30 times a second (or 60 depending on your settings). So in this case, your timer is called 30 times (in a second) and then 7 seconds later, 30 houses are created, then 7 seconds later 30 more houses are created. Is this what you are seeing?

If it were me, if you have a startLevel() function that is outside of any event handler for the enterFrame event, put your timer there. [import]uid: 19626 topic_id: 13493 reply_id: 49524[/import]

I’ve tried both, they give the same result unfortunately. [import]uid: 21652 topic_id: 13493 reply_id: 49525[/import]

Yes robmiracle, this is what I meant and you’re absolutely correct. I have an enterFrame event that starts everything.
However, I do not have any other function that starts everything.

Are you saying I should “wrap” my move() function in a startLevel() function and then just call move() in that, and put the delay code above the call? [import]uid: 21652 topic_id: 13493 reply_id: 49526[/import]

I would use something more like:

  
function create\_house()  
...  
end  
  
function move()  
...  
end  
  
RunTime:addEventListener("enterFrame", move)  
  
timer.performWithDelay(7000,create\_house, 10)  
  

So the timer is just in your main.lua chunk, and will fire off 10 houses after 7 seconds each. Then the event handler for enterframe will deal with moving stuff.

Carlos is right, you really don’t need the local newhouse = create_house() as all thats doing is putting a houseImage object into newhouse and the timer needs to have a function passed to it.
[import]uid: 19626 topic_id: 13493 reply_id: 49536[/import]

I need to put the timer in a function then because I only want to spawn houses when the game is actually moving. In my move() function I got a bool that checks if the game is moving or not, if it’s moving(player walking) then everything is spawning, if not, everything stands still.
Hmm… also put the transition somewhere else.

It works by the way, the delay is perfect. I just need to fire out the rest. Thanks alot both of you for your help! I really appreciate it! [import]uid: 21652 topic_id: 13493 reply_id: 49544[/import]

I gotta say, I’m having trouble figuring out where to put the delay, or even how to solve this “new” problem that came with the previous solution.
Since my move() function is updating so often, putting the delay in there won’t work.
Making a function outside move() is of course desirable then, but since move() function is whats making everything move and it also checks if everything stop I can’t really spawn my houses outside move() since it won’t know if the map has stopped moving and therefor stop spawning(and moving) the houses.

Is there any events I can use? Or any other ideas?
I need to make the houses stop spawning and moving when my now existing BOOL is set to FALSE, which is placed inside move(). [import]uid: 21652 topic_id: 13493 reply_id: 49888[/import]

Noone? It’s giving me a headache.
I can’t think of any solution but to destroy the whole thing. [import]uid: 21652 topic_id: 13493 reply_id: 49975[/import]

How about not having the timer run 10 times?

in move() do something like:

[lua] if spawned then
spawned = false
timer.performWithDelay(7000, create_house(),1)
end[/lua]

Then in create_house, set spawned to true (declare it as a local variable at the top of your file and set it to true).

That way you only call the timer once for each house. When the timer fires and a new house is created, it sets spawned to “true” and the next time move is triggered (the next frame), it will trigger another house in 7 seconds.
[import]uid: 19626 topic_id: 13493 reply_id: 49979[/import]

First, thanks alot for answering and trying to help me out! It feels like this is one of the few big things before I can actually start making stuff.

It seems though that the same amount of houses are spawned in a rapid pace. I think it has time to spawn a house, set spawned to true, then go back to move() and set it to true again and spawn another house. It still spawns 30 houses/sec. [import]uid: 21652 topic_id: 13493 reply_id: 49983[/import]

Just a note to others who have had problems with timer.performWithDelay. I found that using this…

timer.performWithDelay(7000, create\_house(),1)

…would result in the 'create_house()" function being called immediately, ignoring the (in this case 7000 ms) time you have set to delay by.

Removing the empty parentheses after the function call…

timer.performWithDelay(7000, create\_house,1)

…resolves this.

It’s really simple and obvious, but for that very reason it’s also easy to overlook.
[import]uid: 84115 topic_id: 13493 reply_id: 77765[/import]