Waiting for transitions to complete

Hello, everyone. I’m hoping someone can suggest a remedy for this morning’s Frustration of the Day…hehe

I’m making a card game. After you make a match, the cards on the table move to new positions. I’m doing this in a function called move_field(), which pretty much sets up the transitions (in addition to some other minor setup things). What I’d like to do is not return from this function until all those transitions are complete.

My idea made sense to me: I’ll make a count of the current transitions. When a transition is complete, I’ll decrease the count by 1…and when the count is finally 0, return from move_field(). Unfortunately, everything I try simply hangs the Simulator, and I must force quit.

I’ve tried making a wait() function, I’ve tried re-cursively running a function until completion…

Here’s a sample of the code…I think it includes everything you’d need. Please let me know if there’s anything else you need, and I am immensely thankful in advance for any help on the subject.

transition\_count = 0  
  
local function transition\_done()  
 transition\_count = transition\_count - 1  
end  
  
local function move\_field(num)  
 transition\_count = #field\_cards - num  
 for i = num+1, #field\_cards do  
 local card = cardArray[field\_cards[i]]  
 if card.row == 1 then  
 transition.to(card, { delta = true, x = -x\_offset, y = y\_offset, onComplete = transition\_done })  
 else  
 transition.to(card, { delta = true, y = -y\_offset, onComplete = transition\_done })  
 end  
 card.row = math.abs(card.row - 3); card.in\_field = card.in\_field - 1  
 end  
 -- Don't return until the transitions are finished.  
end  

[import]uid: 21712 topic_id: 7152 reply_id: 307152[/import]

Whatever it is you are attempting to do, restructure your code so that move_field() returns immediately but whatever happens next in your game is set in motion by the transition onComplete event. [import]uid: 12108 topic_id: 7152 reply_id: 25180[/import]

The problem with that is that move_field() is essentially setting up multiple transitions…and I wouldn’t want the game to continue until after the last one. I’ll play around with that idea, though…thanks. [import]uid: 21712 topic_id: 7152 reply_id: 25189[/import]

Just an update…I found a solution. The problem, which I suppose I should have thought through and then mentioned, was that a later transition was conflicting with these. What I decided to do was save the transition move_field() creates as a property of the card…then when the second transition comes into play, I cancel it if need be.

Thanks again for your time and help. [import]uid: 21712 topic_id: 7152 reply_id: 25193[/import]