Moving an object smoothly while sending messages

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
William Jamieson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 212
Joined: Fri Feb 01, 2013 1:31 am
Contact:

Moving an object smoothly while sending messages

Post by William Jamieson » Thu Jun 27, 2013 3:12 pm

So I have a graphic of which I wish to move automatically upon mouseup to the side of the stack. This is easy with the move function. However, I need to send messages along the way or somehow get the group that is next to it to follow at the same speed and distance. The solution I came up with is to use a repeat with the total distance divided into 50 segments, then set the graphic to move in 50 small increments while sending the message that moves the group according to the graphic. This works, but every time I simulate it, it ends up in a different place. I checked the variables and I believe that this is due to Livecode rounding the variable distance number to 2 decimal places for its calculation. I don't think that is accurate enough to get the graphic objects to align correctly after 50 moves.

Here is a really chunky and sloppy written code of what I am trying to do because I am still formulating new methods and am not quite sure what I need yet.

But my main question is, is there a smoother, more accurate way to go about this?

Code: Select all

on mouseup
##find the x location of the mouse (the graphic only scrolls left and right)
      put the mouseloc into tMouseLoc
      put item 1 of tMouseLoc into xMouseLoc
##spacing on the side
      put (the left of field "contact name" of this card + (the width of grc "slider bar" of this card/2)) into tVarWidth
##get the location of the graphic into separate variables for easy calc
      put the location of me into tMeLoc
      put item 2 of tMeLoc into yMeLoc
      put item 1 of tMeLoc into xMeLoc
##I think this line of code made it jump to 3 decimal places but did not help
      set the numberFormat to "#.00000"
      put ((the left of graphic "slider bar" of this card)-(the left of field "contact name" of this card))/50 into tInterval
      if the left of grc "slider bar" of this card < (the width of this stack * 0.5) then

      repeat with x = 1 to 50
         set the left of me to (the left of me - tInterval)
##moveControl sets a group's location according to the location of the graphic
         moveControl 
         end repeat
##I put this here to correct any differences at the end but it does not work that well
         if the left of grc "slider bar" of this card <> the left of the field "contact name" of this card then
            set the  left of grc "slider bar" of this card to the left of the field "contact name" of this card
            moveControl
            end if
##This is a repeat of the above if statement for the right side
      else if the right of grc "slider bar" of this card >= (the width of this stack * 0.5) then
         put true into gAllowMove
      repeat with x = 1 to 50
         set the left of me to (the left of me + tInterval)
         moveControl 
         end repeat
         if the right of grc "slider bar" of this card <> the right of the field "sort list" of this card then
            set the right of grc "slider bar" of this card to the right of the field "sort list" of this card
            moveControl
            end if
      else
         exit mouseup
      end if
      put false into gAllowMove
end mouseup

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Moving an object smoothly while sending messages

Post by Klaus » Thu Jun 27, 2013 3:19 pm

Hi William,
William Jamieson wrote:...However, I need to send messages along the way or somehow get the group that is next to it to follow at the same speed and distance.
doesn't something like this work for you?
...
## Prevent objects from moving immediately:
lock moves
move grp "main group" to 500,600 in 500 millisecs without waiting
move grp "the follower..." to 400,600 in 500 millisecs without waiting

## Go... NOW:
unlock moves
## :-)
...
Check "move" in the dictionary for more options!


Best

Klaus

William Jamieson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 212
Joined: Fri Feb 01, 2013 1:31 am
Contact:

Re: Moving an object smoothly while sending messages

Post by William Jamieson » Sat Jun 29, 2013 5:34 am

Yep worked like a charm! Thanks for saving the day once again Klaus.

Here is the code after I made the changes

Code: Select all

   if the left of grc "slider bar" of this card < (the width of this stack * 0.5) then
      set the lockmoves to true
       move me to round(tVarWidth), yMeLoc in 1 second without waiting
       move group "group2" of this card to gDefaultGroupLoc in 1 second without waiting
       move group "all notes" of this card to tLeftLoc in 1 second without waiting
       set the lockmoves to false
   end if

Post Reply