Forward function not working

Hi, I am developing a small quiz app just for fun, with a weird problem.

I have a drag and drop quiz, which uses a listener onTouch (classic), initialized by a local function setupQuiz (which follows the listener, of course).

Now, upon success, the listener calls this function:

-- Forward declaration  
local setupQuiz  
  
local function nextGame()  
 print("NEXT! ".. currentGrp.numChildren)  
 for i = 1, currentGrp.numChildren, 1 do  
 print("transition " .. tostring(currentGrp[i]))  
 transition.to(currentGrp[i], { time = 200, alpha = 0 })  
 end  
 setupQuiz()  
end  

This should in theory fade out every piece in the game, and it does it correctly, and then reinitialize everything… but the setupQuiz() isn’t called!

I find this very weird, I cannot see why it doesn’t work. I thought it could be due to some delay, so I’ve substituted the call with a timer, but even this doesn’t work.

Can anyone help me? [import]uid: 94362 topic_id: 35156 reply_id: 335156[/import]

‘setupQuiz()’ is called, but it’s called before the transition effect finishes.
I’m also not sure why you’re creating a transition for every display object in the group. You can just apply the transition to the group.
If you were using just one transition, you could use its onComplete parameter to call the setupQuiz, which is what I’ve done below, but if you really need more than one transition you would need a timer to call setupQuiz. See sample two.

Sample one:
[lua]-- Forward declaration
local setupQuiz

local function nextGame()
print("NEXT! "… currentGrp.numChildren)
print("transition " … tostring(currentGrp[i]))
transition.to(currentGrp, { time = 200, alpha = 0, onComplete = setupQuiz })
end[/lua]
Sample two (this is not wise):
[lua]-- Forward declaration
local setupQuiz

local function nextGame()
print("NEXT! "… currentGrp.numChildren)
for i = 1, currentGrp.numChildren, 1 do – don’t need to apply to every item
print("transition " … tostring(currentGrp[i]))
– because the children of currentGrp are all inside the group, you can just transition the group (see sample one, above)
transition.to(currentGrp[i], { time = 200, alpha = 0 })
end
– wait until all the transitions have finished before calling the setup func
timer.performWithDelay(200, setupQuiz, 1)
end[/lua]
[import]uid: 8271 topic_id: 35156 reply_id: 139738[/import]

Ok, I’ve tried the new approach (which is cool, I didn’t know I could apply transitions even to groups!), but setupQuiz isn’t called:

-- Next game  
local function nextGame()  
 print("NEXT! ".. currentGrp.numChildren)  
 transition.to(currentGrp, { time = 200, alpha = 0, onComplete = setupQuiz })  
end  
  
-- Create screen items  
local function setupQuiz()  
 print("init screen with " .. #currentLst)  
  
 -- Remove previous items   
 while (currentGrp.numChildren \> 0) do  
 print("removing quiz child " .. currentGrp.numChildren)  
 currentGrp.remove(1)  
 end  
...  

It does display “NEXT!” and the transition works, but the console shows no “init screen with” line.

What am I missing? [import]uid: 94362 topic_id: 35156 reply_id: 139740[/import]

You’re defining the ‘setup’Quiz’ function after your use of its name. To do what you want to do, you need to use the variable declaration of the function as you did in your original post and then assign it the function:

[lua]-- Forward declaration
local setupQuiz = nil

– Next game
local function nextGame()
print("NEXT! "… currentGrp.numChildren)
transition.to(currentGrp, { time = 200, alpha = 0, onComplete = setupQuiz })
end

– Create screen items
setupQuiz = function()
print("init screen with " … #currentLst)

– Remove previous items
while (currentGrp.numChildren > 0) do
print("removing quiz child " … currentGrp.numChildren)
currentGrp.remove(1)
end

end

nextGame()[/lua] [import]uid: 8271 topic_id: 35156 reply_id: 139741[/import]

That did the trick! Thank you! [import]uid: 94362 topic_id: 35156 reply_id: 139892[/import]

‘setupQuiz()’ is called, but it’s called before the transition effect finishes.
I’m also not sure why you’re creating a transition for every display object in the group. You can just apply the transition to the group.
If you were using just one transition, you could use its onComplete parameter to call the setupQuiz, which is what I’ve done below, but if you really need more than one transition you would need a timer to call setupQuiz. See sample two.

Sample one:
[lua]-- Forward declaration
local setupQuiz

local function nextGame()
print("NEXT! "… currentGrp.numChildren)
print("transition " … tostring(currentGrp[i]))
transition.to(currentGrp, { time = 200, alpha = 0, onComplete = setupQuiz })
end[/lua]
Sample two (this is not wise):
[lua]-- Forward declaration
local setupQuiz

local function nextGame()
print("NEXT! "… currentGrp.numChildren)
for i = 1, currentGrp.numChildren, 1 do – don’t need to apply to every item
print("transition " … tostring(currentGrp[i]))
– because the children of currentGrp are all inside the group, you can just transition the group (see sample one, above)
transition.to(currentGrp[i], { time = 200, alpha = 0 })
end
– wait until all the transitions have finished before calling the setup func
timer.performWithDelay(200, setupQuiz, 1)
end[/lua]
[import]uid: 8271 topic_id: 35156 reply_id: 139738[/import]

Ok, I’ve tried the new approach (which is cool, I didn’t know I could apply transitions even to groups!), but setupQuiz isn’t called:

-- Next game  
local function nextGame()  
 print("NEXT! ".. currentGrp.numChildren)  
 transition.to(currentGrp, { time = 200, alpha = 0, onComplete = setupQuiz })  
end  
  
-- Create screen items  
local function setupQuiz()  
 print("init screen with " .. #currentLst)  
  
 -- Remove previous items   
 while (currentGrp.numChildren \> 0) do  
 print("removing quiz child " .. currentGrp.numChildren)  
 currentGrp.remove(1)  
 end  
...  

It does display “NEXT!” and the transition works, but the console shows no “init screen with” line.

What am I missing? [import]uid: 94362 topic_id: 35156 reply_id: 139740[/import]

You’re defining the ‘setup’Quiz’ function after your use of its name. To do what you want to do, you need to use the variable declaration of the function as you did in your original post and then assign it the function:

[lua]-- Forward declaration
local setupQuiz = nil

– Next game
local function nextGame()
print("NEXT! "… currentGrp.numChildren)
transition.to(currentGrp, { time = 200, alpha = 0, onComplete = setupQuiz })
end

– Create screen items
setupQuiz = function()
print("init screen with " … #currentLst)

– Remove previous items
while (currentGrp.numChildren > 0) do
print("removing quiz child " … currentGrp.numChildren)
currentGrp.remove(1)
end

end

nextGame()[/lua] [import]uid: 8271 topic_id: 35156 reply_id: 139741[/import]

That did the trick! Thank you! [import]uid: 94362 topic_id: 35156 reply_id: 139892[/import]