Hi Blake,

> Is it possible to move a bitmapped sprite across the stage 
> using lingo. 
> I know I can use locH and it will put the sprite in a 
> specific place on 
> the stage but can I use lingo to move that sprite from one point to 
> another and make it look like its animated?


You animate the sprite by changing it's locH, locV or both at the same time,
by an n amount of pixels, at regular intervals (for even movement - or
irregular intervals if you want to ease it in or out).
The intervals can be provided by either the time out object, or frame events
(such as on prepareFrame or exitFrame). My favourite is the time out object,
because it can be turned on/off at will.
You also need to work out what speed you want to move the sprite. One way to
do it is to set the chunkiness (how many pixels you want it to move at a
time) and the interval, and then figure out what the horizontal increment
and vertical increment are going to be, depending on the relevant distances
(for example, your sprite may need to cover more ground horizontally, so the
horizontal increment would be larger then the vertical one)

Here's a behavior that does it for you. You need to call the MoveSprite()
method, with the appropriate coordinates. 
Underneath, you'll find another small behavior which, if you attach to the
same sprite, will continually adjust the target coordinates to those of the
mouse, so the sprite will try to play tag with the mouse.


Regards,
Karina Steffens
Creative and Technical Designer
www.neo-archaic.net


----------------------------------------------------------------------------
--------------

-- MOVE SPRITE BEHAVIOR --
-- Animate the sprite across the stage from the starting to 
-- the target coordinates.
-- The animation is triggered from outside the sprite.
-- Example call: sprite(1).MoveSprite(#x, 100, #y, 200)

-- HISTORY

-- (c) Nov 2003, Karina Steffens, http://www.neo-archaic.net

-- PROPERTIES

property spriteNum     
property pTargetLocH   --  Target horizontal loc for the sprite
property pTargetLocV   --  Target vertical loc for the sprite
property pIncH         --  Horizontal increment of the animation 
property pIncV         --  Vertical increment of the animation 
property pChunk        --  Chunkiness of the animation in pixels
property pInterval     --  Interval in milliseconds
property pTimeOut      --  The timeout object



-- PUBLIC METHODS

on MoveSprite(me, x1, x2, y1, y2)-------------------------------------------
  -- CALLED BY: From anywhere outside this behavior, 
  --            Eg, a movie script, other sprites or other behaviors 
  --            that are attached to this sprite
  -- ACTION:    Initiate the sprite's animation
  -- INPUT:     <x1> The starting locH. If the symbol #x is passed,
  --                then the starting location is the current sprite's locH
  --            <x2> The target locH
  --            <y1> The starting locV. If the symbol #y is passed,
  --                then the starting location is the current sprite's locV
  --            <y2> The target locV 
  --------------------------------------------------------------------------
  
  pTargetLocH = x2
  pTargetLocV = y2
    
  if x1 = #x then
    -- If #x is passed then use the current locH
    x1 = sprite(spriteNum).locH
  else 
    -- Move the sprite to it's starting location
    sprite(spriteNum).locH = x1
  end if
  
  if y1 = #y then
    -- If #y is passed then use the current locH
    y1 = sprite(spriteNum).locV
  else
    -- Move the sprite to it's starting location
    sprite(spriteNum).locV = y1
  end if
    
  -- Calculate the distances
  tDeltaH = x2 - x1
  tDeltaV = y2 - y1
  
  -- Convert the chunk to a floating number
  pChunk = pChunk.float
  
  -- Calculate the number of steps necessary for the movement
  aNumSteps = max (abs (tDeltaH) , abs(tDeltaV)) / pChunk
  
  if not aNumSteps then
    -- Error check
    exit
  end if
  
  -- Calculate the horisontal and vertical increments
  pIncH = tDeltaH/aNumSteps 
  pIncV = tDeltaV/aNumSteps 
  
  -- Generate a new time out object, that will call the mAnimage method
  -- of the sprite. If a time out object already exists (animation in 
  -- progress), continue using that one.
  if pTimeOut.voidP then
    pTimeOut = timeOut ("animate").new(pInterval, #mAnimate, me)
  end if
    
  updateStage
end MoveSprite


-- PRIVATE METHODS:

on mAnimate(me)-------------------------------------------------------------
  -- CALLED BY: the time out object
  -- ACTION: Animate the sprite until it reaches it's destination
  --------------------------------------------------------------------------
  
  -- Adjust the sprite's location horizontally
  if pIncH >= 0 then
    x = min (sprite(spriteNum).locH + pIncH, pTargetLocH)
  else
    x = max (sprite(spriteNum).locH + pIncH, pTargetLocH)
  end if
  
  -- Adjust the sprite's location vertically
  if pIncV >= 0 then
    y = min (sprite(spriteNum).locV + pIncV, pTargetLocV)
  else 
    y = max (sprite(spriteNum).locV + pIncV, pTargetLocV)
  end if
  
  -- Check if the sprite reached it's destination
  if x = pTargetLocH and y = pTargetLocV then
    -- Clear the time out object from memory
    -- This will stop the animation as well
    pTimeOut.target = void
    pTimeOut.forget()
    pTimeOut = void
  end if
  
  -- Move the sprite
  sprite (spriteNum).loc = point (x, y)
  updateStage
end mAnimate

-- EVENTS

on endSprite(me)------------------------------------------------------------
  -- ACTION: Make sure the time out is cleared
  --------------------------------------------------------------------------
  
  if pTimeOut.objectP then
    pTimeOut.target = void
    pTimeOut.forget()
    pTimeOut = void
  end if
end endSprite

on getPropertyDescriptionList(me)-------------------------------------------
  -- ACTION: Define the sprite's properties
  --------------------------------------------------------------------------
  
  -- Chunkiness
  tPropList = [:]
  tPropList[#pChunk] = [\
#comment: "Animation chunk in pixels",\
  #format: #integer,\
  #default: 10,\
  #range: [#min: 1, #max: 100]\
]
  
  -- Interval
  tPropList[#pInterval] = [\
#comment: "Animation interval in miliseconds",\
  #format: #integer,\
  #default: 100,\
  #range: [#min: 10, #max: 200]\
]
  
  return tPropList
end getPropertyDescriptionList

----------------------------------------------------------------------------
-------------


--- FOLLOW MOUSE BEHAVIOR (Triggers the Mouse Move Behavior) --

property pMouseLoc

on exitFrame (me)
 -- Trigger the Mouse Move behavior
  if pMouseLoc <> the mouseLoc then
    sprite(me.spriteNum).MoveSprite(#x, the mouseH, #y, the mouseV)
    pMouseLoc = the mouseLoc
  end if  
end exitFrame

----------------------------------------------------------------------------
-------------

[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