Home Contents Forum Links Tip Jar
Animation Math in Lingo       

First Script: One Axis

Movement on one axis - source movie - To restart, right-click and select "restart"

property sp

on beginsprite(me)
  sp = sprite(me.spritenum)
end

on exitframe()
  sp.loch = sp.loch + 1
end


A script shown with a movie is the behavior script attached to the sprite. If you copy the script from the web page to Director, the invisible characters from the page may cause script errors. To fix, either delete them or get the script from the source movie.

If you aren't familiar with what is happening in the beginsprite handler, see The Standard Behavior Script.

This is what is happening in the exitframe handler. The computer first evaluates the right side of the "=" statement and calculates 1 plus the current value of sp.loch. The result of this calculation is then put in the variable on the left side of the "=" statement, which in this case is also sp.loch. The handler is then finished.

The value of sp.loch is now one greater than before the handler was executed. Each time the handler executes, sp.loch will increase by one. On the screen, the sprite moves to the right.

Since the sprite is moving one pixel each frame, you could say that the sprite has a velocity of 1 px/frame to the right.

•How would you move the sprite 2 px/frame to the right? How would you move the sprite to the left?

Velocity
Using a variable for the velocity makes obvious what is happening:

property sp
property velocity

on beginsprite me
  sp = sprite(me.spritenum)
  velocity = 1
end

on exitframe me
  sp.loch = sp.loch + velocity
end

This script does the same thing as the first, but it makes the expression easier to understand. Also, the velocity value can be used or changed in other places in the code. The new variable velocity was declared as a property so that it's value is not forgotten between handlers.

In the beginsprite handler, velocity is being set to an initial value of 1, or initialized to 1. What would happen if velocity was set to a larger value? As you probably guessed, the sprite will move to the right faster. If it is set to a negative number, the sprite will move to the left. (Try it!)

•How would you move the sprite up or down?

 
 


Copyright © 2003 JM Harward 
 jmckell~at~jmckell~dot~com
All lingo provided on this site may be used freely for educational purposes. Not for redistribution as uncompiled code. Instructional materials may not be reproduced without permission.