Page 1 of 1

detecting movement finished

Posted: Tue Feb 07, 2012 11:12 pm
by kevin11

I have things moving around the screen, at different speeds and taking different times to finish moving. Each object starts its movement via a send message to a movement function, and the message can be delayed. That is, I end up with a load of objects moving, and starting and finishing their movement at different times.

Once all movement has stopped, I need to grab these objects into a group.
The problem is, I can't put them into a group while they are moving, well, I can, but that stops the movement.
So I need a way of finding out when things have stopped moving.
Is there any message generated when all movement has stopped, or some flag somewhere ?


Re: detecting movement finished

Posted: Tue Feb 07, 2012 11:37 pm
by sturgis
Assuming you are using the move command for this?

If so, just to test I put the following script in a card and added 2 buttons. (once set up, just execute the moveit command from the msg box)

Code: Select all

on movestopped  -- this is called each time an object finishes its move
     -- puts a line delimited list of objects that have finished moving into the msg box
   put the short id of the target && return after msg -- as you can see, the target (the object that was moved) is recognized.
   set the topleft of the target to the topleft of this card -- just moves the buttons back to the top
end movestopped

command moveit
   put empty -- clears msg box
   lock moves -- lock moves so all moves are concurrent 
   move button 2 to the bottomleft of this card without waiting -- tell the stuff where to move to
   move button 1 to the bottomright of this card without waiting
   unlock moves -- begin moving
end moveit
So it seems to me (again, if you're using 'move' to do your movements') that a) when you queue something up to move you add its name to a list.

whenever movestopped is triggered, you remove its name from the list. When the list is empty, all moves are done.

The same can be done if you're using 'set' to position things. You most likely have a condition that is met when the move is done, so a) if you again track a list of currently moving objects and then b) remove each object from the list when your condition to stop motion is met, remove it from the list. Once the list is empty, all moving items must have been stopped, so you do whatever it is you need to do under that circumstance.

Re: detecting movement finished

Posted: Wed Feb 08, 2012 1:34 am
by kevin11
Thanks for this - that does the trick.

It occurs to me that one could also just increment a move counter each time an object is told to move, and on each movestopped just decrement the counter. Zero would mean everything has stopped.

Thanks for the answer, it has shown me the way forward.

Re: detecting movement finished

Posted: Wed Feb 08, 2012 1:40 am
by kdjanz
A variation would be to have each item put it's name after a variable as it completes the move or it's counter goes to zero. Then you have everything collected and ready to group or deal with as you need to.


Re: detecting movement finished

Posted: Wed Feb 08, 2012 2:13 am
by kevin11
Thanks Kelley !

What I'm unclear about concerning handlers is the following :

A : do stuff
B : start a load of objects moving over different times...
C : do stuff
D : do more stuff
E : even more stuff

and a handler for movestopped. The movestopped handler can do what is being suggested here, but what if a move stops between steps C and D ? The handler is invoked, but upon completion, does execution continue with step D ?. What if step C is a loop - does the loop get interrupted ?

Re: detecting movement finished

Posted: Wed Feb 08, 2012 2:20 am
by sturgis
if C is a loop and you WANT it to get interrupted you can set a flag in movestopped that will stop the loop. If the loop is a repeat type make sure you have a wait with messages in there somewhere.

Depending on the outcome you want you can use the flag for c, d, e to determine if you want them to execute or not. Hard to give better solutions without a more concrete example of what you're working towards but hopefully this will get you farther along the way.

Re: detecting movement finished

Posted: Thu Feb 09, 2012 11:06 am
by kevin11
Sorry, but I'm still struggling with this.
Basically, when a bunch of objects have all stopped moving, I want to group them.
So I can use what has been suggested here, which is to handle it in a movestopped handler, which on its final invocation can do the grouping. The problem I am having is how to then carry on with the rest of the program, without having the rest of the code piled into the movestopped handler. That is, I need the equivalent of a GOTO statement at the end of the mousehandler, so the rest of the program can continue. Can't get my head round this !