Synchronous Animation

Hi,
One of the cool thing with the iPhone is also a bit of a bother. How can I make animation (transition.to) synchronous, how can I ensure that the next bit does not start until the current animation stops?

I tried to keep a boolean flag of isAnimating and onComplete set that to false, the problem is if I exit the function for other animations, those are lost. So how can I queue these and play them synchronously?

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 3077 reply_id: 303077[/import]

You fire the next transition.to statement inside the onComplete handler of the previous one. [import]uid: 5712 topic_id: 3077 reply_id: 9011[/import]

let’s see the example of a board game, after the player has had a turn, the AI has to move some pieces, so in a 4 player, there are 3 AI players and the user.

So I run the command

callMoveTurn(player,moveAINow)  

I have for each player,

local moveAINow = function()  
  
for ai in aiPlayers  
 callMoveTurn(ai, nextAI)  
end  
  
end  

this obviously does not work, so any suggestions on this?

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 3077 reply_id: 9013[/import]

If you want different objects to move after each other, you either have to fire the next transition.to statement after the previous is done. Or use timers which fire the transitions after a delay time. Your code sample doesn’t show what you do inside callMoveTurn(ai, nextAI) so I can’t say if it is wrong or not.

>>this obviously does not work

Judging by this tutorial

http://lua-users.org/wiki/ForTutorial

I think the syntax of your for loop could be wrong but I am not sure as I never loop like this through a table. Did you got a lua error inside the Corona terminal? [import]uid: 5712 topic_id: 3077 reply_id: 9027[/import]

Hi Mike,
This is not the actual code, but what I am trying to achieve. The for loop might be flawed, but the point here is how to have events run synchronously one after the other.

look at this example to understand what I am trying to do

local movecharacter = function(posx,posy, sprite)  
 --moves the sprite passed to x,y  
 transition.to (sprite, {x=posx, y=posy, time=500})  
end  

now, I want to call this to do the following

movecharacter(x1co,y1co, player1)  
movecharacter(x2co,y2co, player2)  
movecharacter(x3co,y3co, player3)  
movecharacter(x4co,y4co, player4)  

what I was trying to create with the for is that it is not always known how many players are selected, so in pseud-code it was a for loop iterator

for player in players  
 movecharacter(xco, yco, player)  
end  

the question here is how can I achieve that first player1 moves, then player2 moves then player3 and then player4. At the moment, on running this, they all move at the same time.

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 3077 reply_id: 9033[/import]

Hi Jayant,

I had to experiment a little. There are so many ways of doing it, but I think this solution is a nice one:

[lua]grpPlayer = display.newGroup()

function movePlayer(event)
transition.to(grpPlayer[event.count], { time=500, y=440 })
end
for i=1,4 do
local rect = display.newRect( 55*i, 40, 40, 40 )
rect:setFillColor( 255, 55*i, 0 )
grpPlayer:insert(rect)
end

timer.performWithDelay(500, movePlayer, grpPlayer.numChildren )[/lua]

Cheers
Michael Hartlef

http://www.whiteskygames.com
http://www.twitter.com/mhartlef [import]uid: 5712 topic_id: 3077 reply_id: 9071[/import]

Hi Michael,
that is neat, you know life isn’t that simple.

Now the point is that before each object is to be moved, it also needs to be checked if it CAN move or not. For example, in Monopoly, if the player is in Jail or has lose a turn, they cannot move. so movement is conditional but yet has to be run one after the other.

Thanks,

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 3077 reply_id: 9377[/import]

Yes, it is simple. :slight_smile:

Check which players can move. The ones that can you put in a table.
Then loop through that table instead of the group.

[lua]grpPlayer = display.newGroup()

tableMove = {}

function movePlayer(event)
transition.to(tableMove[event.count], { time=500, y=440 })
end

for i=1,4 do
local rect = display.newRect( 55*i, 40, 40, 40 )
rect:setFillColor( 255, 55*i, 0 )
grpPlayer:insert(rect)
end

tableMove[1] = grpPlayer[1]
tableMove[2] = grpPlayer[3]

timer.performWithDelay(500, movePlayer, #tableMove )[/lua]
Cheers
Michael [import]uid: 5712 topic_id: 3077 reply_id: 9395[/import]

Hi Michael,
Thanks for the example, learned something new today.

I was going to ask you but. how do I, then while writing the response, I got my reply, I need to have a conditional statement in the movePlayer that will determine if that player can move or not.

Thanks and kind regards,

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 3077 reply_id: 9452[/import]

Sorry, english is not my native language. Can you please ask again? I am unsure if you wanted to ask soemthing and then got the reply, or if it is an additional question. [import]uid: 5712 topic_id: 3077 reply_id: 9944[/import]

Hi Mike,
Sorry for the confusion. I was going to ask you a question, but then I realised the answer to that. I was just thinking aloud and wrote that.

Thank you for the explanation.

Sind sie Deutsch? ou etes vous Francais? what is your native language, I don’t know many langauges so just wondering.

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 3077 reply_id: 9947[/import]

I am german. [import]uid: 5712 topic_id: 3077 reply_id: 9950[/import]