Creating A PLAY Button with ActionScript 3.0

Create a Function

Now we need to get the movie playing again. Predictably, ActionScript has a play method. The problem is, we need to find a way to tell ActionScript exactly when we want it to play.

We'll build a function. A function is a group of one or more Actions that are (usually) related. Functions are a cornerstone of Object-Oriented Programming, and you'll soon know all about them.

The template for a function looks like this:

function functionName(parameters):return {
    actions to run;
}

First, we declare the function and assign a name so we can refer to it later, kind of like an instance name. The name of the function should indicate what the function is supposed to do. I chose the name playTimeline:

function playTimeline

Notice we do not have any spaces in the function name. That's part of the rules. Function names should not:

  1. Contain spaces
  2. Contain any punctuation other than dashes and underscores - and _
  3. Begin with a number
  4. Should not use "reserved" names (more on that later)

Next, we specify any parameters inside a set of ellipses. Functions won't always contain parameters, but in our example, the function will be connected with a MouseEvent:

function playTimeline(e:MouseEvent)

Next, we declare whether the function should return any values. This function will not, so we use :void

function playTimeline(e:MouseEvent):void

Finally, all of the Actions we want the function to run must appear between a set of curly braces:

function playTimeline(e:MouseEvent):void {
     play();
}

Watch the video below as the function is built. We'll leave one blank line between the stop(); method and the function. This is not required, but it will help us see the code in discreet chunks later:

Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player

Pay very close attention to the elements the ActionScript window turns blue. Those are built-in elements. If mine is blue, but your's isn't, you're doing something wrong. ActionScript is case sensitive, so only capitalize the letters you see in my example: