Page 1 of 1

Moving multiple graphic simultaneously

Posted: Thu Mar 24, 2011 10:27 pm
by ColdSnap
When I execute the code:

on movePieces
move graphic "piece1" to varX,varY
move graphic "piece2" to varX,varY
end movePieces

the program will first move piece1 then move piece2. How do I tell both of them to move at the same time?

Thanks in advance.

-Jason

Re: Moving multiple graphic simultaneously

Posted: Thu Mar 24, 2011 10:33 pm
by bn
Hi Coldsnap,

welcome to the forum.

try

Code: Select all

on movePieces
lock moves
move graphic "piece1" to varX,varY without waiting
move graphic "piece2" to varX,varY without waiting
unlock moves
end movePieces
look up lock moves, unlock moves.

Kind regards

Bernd

Re: Moving multiple graphic simultaneously

Posted: Fri Mar 25, 2011 12:57 am
by ColdSnap
For some reason that freezes my program. It seems like an interesting command, I am not sure why it is not working.

on movePieces
lock moves
move graphic "piece1" to varX,varY without waiting
move graphic "piece2" to varX,varY without waiting
unlock moves
end movePieces

Re: Moving multiple graphic simultaneously

Posted: Fri Mar 25, 2011 1:14 am
by bn
Hi Coldsnap,

I did not pay attention to your syntax. Move takes two arguments: move object from location to location

This is tested and works

Code: Select all

on mouseUp
   movePieces
end mouseUp

on movePieces
   put random (200) into varX
   put  random (200) into varY
   put the loc of graphic "piece1" into tLoc1
   put the loc of graphic "piece2" into tLoc2
   lock moves
   move graphic "piece1" from tLoc1 to varX,varY without waiting
   put random (200) into varX
   put  random (200) into varY
   move graphic "piece2" to varX,varY without waiting
   unlock moves
end movePieces
Kind regards

Bernd

Re: Moving multiple graphic simultaneously

Posted: Fri Mar 25, 2011 2:50 am
by ColdSnap
Ahh, that works, thank you.

The reason that it had been freezing before was due to the loop command. Is there anyway that I can loop this without freezing the program?

The code below would work if I removed either the repeat command or both the lock and without waiting commands.

Code: Select all

on movePieces
repeat until impossibleVariable is true
//
   put random (200) into varX
   put  random (200) into varY
   lock moves
   move graphic "piece1" to varX,varY without waiting
   put random (200) into varX
   put  random (200) into varY
   move graphic "piece2" to varX,varY without waiting
   unlock moves
//
end repeat
end movePieces

Re: Moving multiple graphic simultaneously

Posted: Fri Mar 25, 2011 8:55 am
by bn
Hi ColdSnap,

the reason is that in a repeat loop you don't give the objects time to move.
Look at the moveStopped message

if you set the script of the graphics "Piece1" and "Piece2" to

Code: Select all

on moveStopped
   global gIsMoving -- global declared inside of handler
   add 1 to gIsMoving
end moveStopped
and modify your other script to

Code: Select all

on movePieces
   global gIsMoving -- declare the global outside or inside of handler
   put 0 into gIsMoving -- initialize the global
   repeat 5 -- or your other conditional
      //
         put random (200) into varX
         put  random (200) into varY
         lock moves
         move graphic "piece1" to varX,varY without waiting
         put random (200) into varX
         put  random (200) into varY
         move graphic "piece2" to varX,varY without waiting
         unlock moves
      repeat until gIsMoving = 2
         wait 100 milliseconds with messages
      end repeat
      put 0 into gIsMoving -- reset the global
      //
   end repeat
end movePieces
it works (tested). It is important to issue a 'wait with messages' command then livecode can update the screen.

There are other possibilities to handle the moveStopped command. This is one way to do it.

Kind regards

Bernd

Re: Moving multiple graphic simultaneously

Posted: Sat Mar 26, 2011 5:20 am
by ColdSnap
Very cool, thank you ever so much for your help.

-Jason