onEnterFrame

I'm excited for you! You're just now discovering all this magic computer programming stuff! We're going to work with functions now, which aren't as scary as the functions you come across in Calculus. These functions are powerful.

Let's start with a function that's built right into ActionScript - it's called onEnterFrame (it's case sensitive) and it basically allows you to loop actions just like you would loop a timeline. Let's say you wanted a variable called myNumber to add up and up and up! I'm sure you could figure out how to do that with multiple keyFrames and a gotoAndPlay action, but it's a hassle and could really interfere with other animations.

The better way is to use a simple onEnterFrame function that looks like this:

myNumber=0;
function onEnterFrame (){
myNumber=myNumber+1;
}

Why not try it? Start a new Flash document, paste the code above into the Actions window, then draw a dynamic text box on the stage that picks up the variable myNumber

The onEnterFrame function will loop all of the actions inside the braces "forever".

  1. Click Statements > User-Defined Functions > function
  2. Type onEnterFrame in the Name box. This is cAse sEnsitive

Now use set variable to repeatedly reset the bee's _x property (From now on, we'll try to use set variable to set properties, and var to declare variables) I want to move the bee five pixels to the right every time the frame "loops." So, we set bee._x (the bee's x position) equal to bee._x+5 (the bee's current x position, plus 5) Since my framerate is 24 frames/second, this should (hypothetically) move the bee 120px to the right every second:

  1. Click Statements > Variables > set variable
  2. Type bee._x in the Variable box
  3. Type bee._x+5 in the Value box
  4. Select Expression... we want Flash to evaluate that Value!

Ok, if you test your work the bee should move right across the stage and disappear into oblivion. Cool, right? At LIFT, we put the "fun" in function!