Kurt Griffin wrote:

> 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

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

Hey Kurt,

Here is another take on the animation behaviour. It is time based but doesn't use 
timeout objects, instead it calculates the change in time at each enterframe and uses 
that to position the sprite.

Also have a look at the use if  the vector in this approach (difference between 
destination and origin) this really eliminates the need for working out the number of 
steps the animation will take as well as calculating the direction (taken care of by 
the vector).

Finally this behaviour allows you to differentiate between speed as in total time to 
get to the destination or speed as in the rate (pixels per second) it travels to get 
to the destination. This behaviour is not un-like the slide in out behaviour in the 
libraries palette. The one
that comes with director is more detailed and has more error checking.

let me know what you think,

Rob

---linearMotionTimeBased behaviour---

property pOrigin
property pDestination
property pSpeed
property pSpeedType
property pMotionInit
property pMotionVector
property pStartTime
property pTravelTime
property pInMotion

on getPropertyDescriptionList me
  pdl=[:]

  if the currentSpriteNum>0 then
    pdl[#pOrigin]=[#comment:"Origin point", #format:#point, #default:point(sprite(the 
currentSpriteNum).locH, sprite(the currentSpriteNum).locV)]
    pdl[#pDestination]=[#comment:"Destination", #format:#point, #default:point(0,0)]
    pdl[#pSpeed]=[#comment:"Speed (msec or pixels/sec):", #format:#integer, 
#default:1000]
    pdl[#pSpeedType]=[#comment:"Speed type:", #format:#symbol, #default:#totalTime, 
#range:[#totalTime, #pixelsPerSec]]
    pdl[#pMotionInit]=[#comment:"AutoStart", #format:#boolean, #default:0]
  end if

  return pdl
end

on beginSprite me
  if pMotionInit then me.startLinearMotion()
end

on startLinearMotion me
  pMotionVector=pDestination-pOrigin
  if pSpeedType=#pixelsPerSec then
    distance=sqrt(power(pMotionVector.locH,2)+power(pMotionVector.locV,2))
    pTravelTime=distance/pSpeed*1000
  else
    pTravelTime=pSpeed
  end if
  pStartTime=the milliseconds
  pInMotion=1
end

on enterFrame me
  if pInMotion then
    dTime=the milliseconds-pStartTime*1.0
    if dTime>pTravelTime then
      newLoc=pDestination
      pInMotion=0
    else
      newLoc=pOrigin+dTime/pTravelTime*pMotionVector
    end if
    sprite(me.spriteNum).loc=newLoc
  end if
end


[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