Moving multiple graphic simultaneously

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
ColdSnap
Posts: 7
Joined: Thu Mar 24, 2011 10:23 pm

Moving multiple graphic simultaneously

Post by ColdSnap » Thu Mar 24, 2011 10:27 pm

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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Moving multiple graphic simultaneously

Post by bn » Thu Mar 24, 2011 10:33 pm

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

ColdSnap
Posts: 7
Joined: Thu Mar 24, 2011 10:23 pm

Re: Moving multiple graphic simultaneously

Post by ColdSnap » Fri Mar 25, 2011 12:57 am

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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Moving multiple graphic simultaneously

Post by bn » Fri Mar 25, 2011 1:14 am

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

ColdSnap
Posts: 7
Joined: Thu Mar 24, 2011 10:23 pm

Re: Moving multiple graphic simultaneously

Post by ColdSnap » Fri Mar 25, 2011 2:50 am

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
Last edited by ColdSnap on Fri Mar 25, 2011 3:40 am, edited 1 time in total.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Moving multiple graphic simultaneously

Post by bn » Fri Mar 25, 2011 8:55 am

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

ColdSnap
Posts: 7
Joined: Thu Mar 24, 2011 10:23 pm

Re: Moving multiple graphic simultaneously

Post by ColdSnap » Sat Mar 26, 2011 5:20 am

Very cool, thank you ever so much for your help.

-Jason

Post Reply