Jason's Tip Sheets

click here for Actionscript 2x reference cod

Click here for Actionscript 3x CODE SNIPPETS

ACTIONSCRIPT3 – Jason Challas Art 55 ©2008 WVC

All script is FRAME action script.
Create a new layer called ACTIONS. INSERT a NEW KEYFRAME at the frame where you want the actions to occur.
We define functions. You need to define each function ONLY ONCE per movie.
Then, call it from each frame by placing an “event listener” in that frame’s actionscript.
If you’re used to the functionality of ActionScript 2, you may need to IMPORT Actionscript “Classes.”
In order to make the movies smaller, ActionScript 3 doesn’t automatically “include” all of the functions.


To pause your movie at that (or the end) frame:
stop();

To make navigation buttons:
Each button MUST BE A NAMED INSTANCE on the stage.
First, create a button. Select the graphic/text and go to the MODIFY menu to CONVERT TO SYMBOL.
Give it a name “my button” just so you can find it later in the Symbols Library.
Select it on the stage and go to the properties palette and give it an INSTANCE NAME (ie. “back” or “next")

For a simple ENTER (start the movie) button:
stop();
function startMovie(event:MouseEvent):void
{
this.play();
}
playbutton.addEventListener(MouseEvent.CLICK, startMovie);


For inner-movie navigation:
Next Scene Method:
An enter button (named “enterbtn” in the properties palette)

stop();

function enter(event:MouseEvent):void
{
            this.nextScene();
}

enterbtn.addEventListener(MouseEvent.CLICK, enter);


For the button named “fineartbtn”
Note order of frame# BEFORE the scene name: gotoAndPlay(frame# or scene label, “scenename”)

stop();

function gofineart(event:MouseEvent):void
{
            this.gotoAndPlay(1,"fineart");
}

fineartbtn.addEventListener(MouseEvent.CLICK, gofineart);


nextFrame/prevFrame
and then to the nextScene/prevScene:
for the buttons named “back” and “nextbtn”

stop();

function backframe(event:MouseEvent):void
{
            this.prevFrame()
}

back.addEventListener(MouseEvent.CLICK, backframe);

function nextframe(event:MouseEvent):void
{
            this.nextFrame()
}

nextbtn.addEventListener(MouseEvent.CLICK, nextframe);

In the last frame of the scene:
stop();

back.addEventListener(MouseEvent.CLICK, backframe);

function nextscene(event:MouseEvent):void
{
            this.nextScene()
}

nextbtn.addEventListener(MouseEvent.CLICK, nextscene);

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

In each frame: You don’t need to duplicate the function definition, just the event listener:

import flash.display.MovieClip;
stop();

back.addEventListener(MouseEvent.CLICK, backframe);

next.addEventListener(MouseEvent.CLICK, nextframe);

homebutton.addEventListener (MouseEvent.CLICK, gotoAuthorPage);

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

Add this code to make a button to go to your website:
function gotoAuthorPage(event:MouseEvent):void
{
var targetURL:URLRequest = new URLRequest("http://jasonchallas.com");
navigateToURL(targetURL);
}
homebutton.addEventListener (MouseEvent.CLICK, gotoAuthorPage);

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

More Actionscript 3 reference:
(the following is written assuming you already know how to make a button, inclulde a class, add an event listener, etc.)

stop() method
public function stop():void
Stops the playhead in the movie clip.

play() method
public function play():void
Moves the playhead in the timeline of the movie clip.

gotoAndPlay () method
public function gotoAndPlay(frame:Object, scene:String = null):void
Starts playing the SWF file at the specified frame. This happens after all remaining actions in the frame have finished executing. To specify a scene as well as a frame, specify a value for the scene parameter.

Example
The following code uses the gotoAndPlay() method to direct the playhead of the mc1 movie clip to advance five frames ahead of its current location:

mc1.gotoAndPlay(mc1.currentFrame + 5);

The following code uses the gotoAndPlay() method to direct the playhead of the mc1 movie clip to the frame labeled "intro" in the scene named "Scene 12":
mc1.gotoAndPlay("intro", "Scene 12");


gotoAndStop() method
public function gotoAndStop(frame:Object, scene:String = null):void
Brings the playhead to the specified frame of the movie clip and stops it there. This happens after all remaining actions in the frame have finished executing. If you want to specify a scene in addition to a frame, specify a scene parameter.

Example
The following code uses the gotoAndStop() method and the currentFrame property to direct the playhead of the mc1 movie clip to advance five frames ahead of its current location and stop:
mc1.gotoAndStop(mc1.currentFrame + 5);

The following code uses the gotoAndStop() to direct the playhead of the mc1 movie clip to the frame labeled "finale" in the scene named "Scene 12" and stop the playhead:
mc1.gotoAndStop("finale", "Scene 12");


Controlling ActionScript 3.0 movie clips:

nextFrame() method
prevFrame()

Example Next Frame/Prev Frame
In the following example, two SimpleButton objects control the timeline. The prev button moves the playhead to the previous frame, and the nextBtn button moves the playhead to the next frame:

import flash.events.MouseEvent;
mc1.stop();
prevBtn.addEventListener(MouseEvent.CLICK, goBack);
nextBtn.addEventListener(MouseEvent.CLICK, goForward);
function goBack(event:MouseEvent):void {
mc1.prevFrame();
}
function goForward(event:MouseEvent):void {
mc1.nextFrame();
}nextScene() method

Example: Next Scene/Prev Scene
In the following example, two simple button objects control the timeline. The prevBtn button moves the playhead to the previous scene, and the nextBtn button moves the playhead to the next scene:

import flash.events.MouseEvent;
mc1.stop();
prevBtn.addEventListener(MouseEvent.CLICK, goBack);
nextBtn.addEventListener(MouseEvent.CLICK, goForward);
function goBack(event:MouseEvent):void {
mc1.prevScene();
}
function goForward(event:MouseEvent):void {
mc1.nextScene();
}

Example
In the following example, two SimpleButton objects control the timeline. The prevBtn button moves the playhead to the previous scene, and the nextBtn button moves the playhead to the next scene:

import flash.events.MouseEvent;
mc1.stop();
prevBtn.addEventListener(MouseEvent.CLICK, goBack);
nextBtn.addEventListener(MouseEvent.CLICK, goForward);
function goBack(event:MouseEvent):void {
mc1.prevScene();
}
function goForward(event:MouseEvent):void {
mc1.nextScene();
}

Loading a URL
The getURL command no longer exists in ActionScript 3.0. To call a URL, you will create a URLRequest instance and then call the navigateToUrl method. The try statement below illustrates the type of error handling capable with ActionScript 3.0 exception handling; it shows how to call a URL:

import flash.net.*;

var url = "";
var request:URLRequest = new URLRequest(url);
try {
navigateToURL(request);
}
catch (e:Error) {
// Handle error...
}

© 2008 by Jason C. Challas – www.jasonchallas.com