Forward function not working

4 replies [Last post]
milicchio
User offline. Last seen 2 days 9 hours ago. Offline
Joined: 29 Sep 2011

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:

1
2
3
4
5
6
7
8
9
10
11
-- 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?

Replies

horacebury
User offline. Last seen 2 hours 3 min ago. Offline
Joined: 17 Aug 2010

'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:

1
2
3
4
5
6
7
8
-- 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

Sample two (this is not wise):

1
2
3
4
5
6
7
8
9
10
11
12
13
-- 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

milicchio
User offline. Last seen 2 days 9 hours ago. Offline
Joined: 29 Sep 2011

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-- 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?

horacebury
User offline. Last seen 2 hours 3 min ago. Offline
Joined: 17 Aug 2010

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
-- 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()

milicchio
User offline. Last seen 2 days 9 hours ago. Offline
Joined: 29 Sep 2011

That did the trick! Thank you!

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.