> I'm currently looking at storing the animation progress of each of my ten
> objects and moving each one bit by bit within a repeat loop.
> Does anybody else know of a better method of performing this task? Is there
> a series of commands that already exist in lingo that allow me to say "Move
> 200 pixels to the left at speed 4" ?

Don't use a repeat loop. You can use a frame loop, but that relies on frame
rate. A better way is to drive your animation with a timeout object.

Normally, I'd leave you with that advice and let you work through it, but I
happen to have written a behavior that will move a sprite in a straight line
to a destination point (not just horizontal or vertical - to get that
motion, you'd just give the behavior a destination point that has the same
locH or locV value as the sprite's starting point).

I'm sure there are ways to optimize... that's a hint, people - I'd love to
see ways to improve it.

HTH,
Kurt

Watch out for email-induced line breaks.

------------------
Property pDestination -- The point to which this sprite will travel
Property pSpeed -- The time it will take this sprite to get there
Property pChangePoint -- Increment to change the loc of the sprite at each
callback
Property pTimer -- Timeout object to drive things

Property pMySprite -- Duh
Property pFloatingLoc -- Stores the change in loc in floats, so as to avoid
a "round down" with changes that are less than .5
---------------------
on getPropertyDescriptionList
  pdl = [:]
  pdl.addProp(#pDestination,[#comment:"where do you want to go today?",
#format:#point, #default:point(0,0)])
  pdl.addProp(#pSpeed,[#comment:"How long to get there (in milliseconds)?",
#format:#integer, #default:5000])
  
  return pdl
end getPropertyDescriptionList
---------------------
on new me, destination, speed
  pDestination = destination
  pSpeed = speed
  
  return me
end 
---------------------
on beginSprite me
  pMySprite = sprite(me.spriteNum)
  
  -- Determine the overall change in x and y positions
  dx = pDestination.locH - pMySprite.locH
  dy = pDestination.locV - pMySprite.locV
  
  -- The larger distance will determine the frequency of the callbacks
  -- from the timeout object
  numberOfSteps = max(abs(dx), abs(dy))
  
  -- Keeping things simple. We're going to move one pixel on the larger axis
at each callback,
  -- and a fraction of a pixel on the smaller axis, determined by the slope
of
  -- line equation.
  deltaRatio = abs(float(dy)/dx)
  
  -- Need to know whether to travel left or right
  if dx < 0 then
    xDirection = -1
  else
    xDirection = 1
  end if
  
  -- Need to know whether to travel up or down
  if dy < 0 then
    yDirection = -1
  else
    yDirection = 1
  end if
  
  -- The larger axis gets the change in loc of 1 (or negative 1), and the
smaller
  -- gets a fractional change. The fraction gets multiplied by the
directional variable,
  -- otherwise the sprite would always move right or down.
  if numberOfSteps = abs(dx) then
    pChangePoint = point(xDirection, deltaRatio*yDirection)
  else
    pChangePoint = point(xDirection/deltaRatio, yDirection)
  end if
  
  -- Necessary to store a separate loc with floats, because simply setting
  -- pMySprite.loc = pMySprite.loc + pChangePoint rounds off fractions less
than .5,
  -- as "loc"s (when a property of a sprite) are integer-based.
  pFloatingLoc = point(float(pMySprite.loc).locH, float(pMySprite.loc).locV)
  
  --Unique name for the timeout object
  timername = string(me)
  pTimer = timername.char[timername.char.count - 8..timername.char.count]
  
  -- Create the timer. we are going numberOfSteps number of pixels in pSpeed
  -- time, so the callbacks need to happen every float(pSpeed)/numberOfSteps
  -- milliseconds.
  temp = timeOut(pTimer).new(float(pSpeed)/numberOfSteps, #mMove, me)
end beginSprite
---------------------
on mMove me
  -- If the sprite is not within a pixel of its destination, move it some
more.
  if abs(pDestination.locH - pMySprite.locH) > 1 then
    pFloatingLoc = pFloatingLoc + pChangePoint
    pMySprite.loc = pFloatingLoc
  else
    -- This is the last stop. Please take any personal belongings with
    -- you as you exit the train.
    death = timeOut(pTimer).forget()
    
    -- Tidy up (that locV value may be a bit off, so this will get us to the
end)
    pMySprite.loc = pDestination
  end if
end mMove
---------------------
on endSprite me
  -- Kill the engineer
  death = timeOut(pTimer).forget()
end endSprite
---------------------

[To remove yourself from this list, or to change to digest mode, go to 
http://www.penworks.com/lingo-l.cgi  To post messages to the list, email 
[EMAIL PROTECTED]  (Problems, email [EMAIL PROTECTED]). Lingo-L is for 
learning and helping with programming Lingo.  Thanks!]

Reply via email to