Something wrong with timer.performWithDelay()

Hello everyone!
I think I stumbled into a bug.

Check this code out:

[lua]function newBall()
local randomPosition = 100 + math.random(200)
ballPosition = display.newImage(“ballPosition.png”)
ballPosition.x = randomPosition
timer.performWithDelay(2000, spawnBall(randomPosition), 1)
end[/lua]

Basically, I want balls to come from above, but I want to create an image showing the location of the incoming ball BEFORE it shows up.

So, I decided to create these two functions: newBall and spawnBall.

If I try to call the function “spawnBall” after two seconds with the delay as it is, the delay doesn’t occur and the ball is spawned imediately.

However, if I remove the “spawnBall” parameter - and use a fixed value in the function, so it looks like this:
[lua]timer.performWithDelay(2000, spawnBall, 1)[/lua]
It works out fine.

Is this a bug?
What can I do to avoid this, and how do I report it?

Thanks in advance!!! [import]uid: 11130 topic_id: 3698 reply_id: 303698[/import]

no it’s not a bug.

spawnBall is a callback function to be used later (at the end of the delay)

if you use spawnBall(…) you’re calling that function directly

try this. i’m not near my machine so can’t verify it works… just guessing basically :wink:

[lua]spawnBall = {}

function spawnBall:timer(event)

print(self.randomPostion)

end

function newBall()
local randomPosition = 100 + math.random(200)
ballPosition = display.newImage(“ballPosition.png”)
ballPosition.x = randomPosition

local fn = spawnBall
fn.randomPosition = randomPosition

timer.performWithDelay(2000, fn, 1)
end[/lua]

if not, maybe you could try

[lua]function spawnBall(rndpos)

print(rndpos)

end

function newBall()
local randomPosition = 100 + math.random(200)
ballPosition = display.newImage(“ballPosition.png”)
ballPosition.x = randomPosition

timer.performWithDelay(2000, function()spawnBall(randomPosition);end, 1)
end[/lua]
[import]uid: 6645 topic_id: 3698 reply_id: 11253[/import]

Wow!
First method worked beautifully!

Now I have to understand what just happened… Can you please explain it to me? :slight_smile:

I mean…
1- Why the empty braces?
2- Why is the “timer(event)” needed? [import]uid: 11130 topic_id: 3698 reply_id: 11256[/import]

really? first method failed for me. have you got it in a module or anything

second method worked tho [import]uid: 6645 topic_id: 3698 reply_id: 11260[/import]

Well you mispelled “position” in line 5 :stuck_out_tongue:

Method two seems cleaner and more like what I’m used to do, but I still don’t understand it as well :stuck_out_tongue:
EDIT:
I mean… writing a function in there… Are we allowed to do that?! :smiley: [import]uid: 11130 topic_id: 3698 reply_id: 11261[/import]

the braces creates a table/object (their the core fundamentals of lua structures)
the :timer means that object responds to the timer event

anyway i don’t think what i did there should have worked for you. maybe you’ve got a global item somewhere that it’s using which may mess things up if 2 things try and use it… don’t know without seeing your code. if you run it in a self contained file it just comes out as “nil”

however this works and is simpler

[lua]local function spawnBall(event)

print("spawnBall: "… event.source.something)

end

local function newBall(someVal)

local randomPosition = 100 + math.random(200)
print("newBall: "…randomPosition)

local t2 = timer.performWithDelay(2000, spawnBall, 1)
t2.something = someVal…", "…randomPosition

end

newBall(“blah”)
newBall(“whatever”)[/lua] [import]uid: 6645 topic_id: 3698 reply_id: 11263[/import]

jmp909, thanks a lot for those examples!

I checked my code and I’m using everything as local so far. If I find anything I’ll post it here.

I still gotta learn a lot of LUA and Corona to understand the events and braces used in the SDK. I’ve been working with this for less than a week now, with little experience in programming. :slight_smile: [import]uid: 11130 topic_id: 3698 reply_id: 11266[/import]

oh heh. thanks for spotting my error.

yes most languages would let you put a function in the timer call.

[import]uid: 6645 topic_id: 3698 reply_id: 11269[/import]