Advertisement |
ActionScript 3.0 Codes
//
Creates
a comment. This line has no effect. A good way troubleshoot code is to make a
line of code a comment, and see if that was what was causing the problem.
Comments can also be used to organize code.
Commands
stop();
§
Stop playing when this frame is reached.
nextFrame();
§
Go to the next frame.
prevFrame();
§
Go to the previous frame.
gotoAndStop("frameLabel");
§
Go to indicated frame label in the timeline and stop.
gotoAndPlay("frameLabel");
§
Go to indicated frame label and play from there (until a stop
command is reached).
navigateToURL(new
URLRequest("http://www.google.com/"));
§
Opens a link to the specified website.
navigateToURL(new
URLRequest("http://www.google.com/"), "_blank");
§
Opens a link to the specified website in a new window.
trace("hello
world");
§
Adds the message "hello world" to the output window in
Flash. This does not affect your final piece, but is useful for debugging.
Mouse Events
MouseEvent.MOUSE_UP
§
Triggers something when the mouse button is released.
MouseEvent.MOUSE_DOWN
§
Triggers something when the mouse button is first pressed (used
especially for drag functions).
Traversing Timelines
(parent
as MovieClip).gotoAndStop("frameLabel");
§
References a frame label "frameLabel" in the timeline
above/outside of the current movie clip.
movieClipName.gotoAndStop("frameLabel");
§
References a frame label "frameLabel" inside of the
movie clip "movieClipName" (which is currently on the stage).
Button Codes
instanceName.addEventListener(MouseEvent.MOUSE_UP,
functionName);
function
functionName(event:MouseEvent):void { ----- }
§
Basic button code, where ----- is the action to be performed
(gotoAndPlay, gotoAndStop, etc.) when the mouse button is released.
instanceName.removeEventListener(MouseEvent.MOUSE_UP,
functionName);
§
Remove eventListener from a button, so that clicking the button
no longer does anything.
Drag and Drop Code
instanceName.buttonMode
= true;
instanceName.addEventListener(MouseEvent.MOUSE_DOWN,
startDragging);
function
startDragging(event:MouseEvent):void {
instanceName.startDrag();
}
instanceName.addEventListener(MouseEvent.MOUSE_UP,
stopDragging);
function
stopDragging(event:MouseEvent):void {
instanceName.stopDrag();
}
§
"instanceName" is an instance of a movie clip symbol.
Drag and drop requires linking a command to both pressing the mouse button and
releasing it.
Loading and Unloading a .swf File
instanceName.addEventListener(MouseEvent.MOUSE_UP,
loadSwf);
function
loadSwf(event:MouseEvent):void {
loader.source =
"swf/loadMovie.swf";
}
§
This loads the external .swf file "loadMovie.swf" to
the stage, as long as the file is stored in the "swf" folder in the
main project folder.
instanceName.addEventListener(MouseEvent.MOUSE_UP,
unloadSwf);
function
unloadSwf(event:MouseEvent):void {
loader.unload();
}
§
This unloads an external .swf file from the stage.
ActionScript 3.0 Codes: Advanced
§ Loading and Controlling
.flv Videos
§ Detecting an Internet
Connection
§ Accelerator
§ Quiz
Things
You Can Do with Frame Labels, gotoAndPlay, gotoAndStop, and Movie Clips
The
humble button is a powerful tool. By nesting content inside a movie clip symbol
and adding frame labels to the timeline, you can do a lot of things.
Control Sound
Inside
a movie clip, add a sound to the second frame (or later). Later in the
timeline, stop that sound by adding a keyframe changing the sync setting to
stop. Add a new "actions" layer. Add a stop command on the first
frame, and add frame labels "startSound" and "stopSound"
where appropriate. Create your startSoundBtn and stopSoundBtn buttons, and code
them to "gotoAndStop("startSound");" and
"gotoAndStop("stopSound");" respectively.
startSoundBtn.addEventListener(MouseEvent.MOUSE_UP,
playSound);
function
playSound(event:MouseEvent):void {
gotoAndStop("startSound");
}
stopSoundBtn.addEventListener(MouseEvent.MOUSE_UP,endSound);
function
endSound(event:MouseEvent):void {
gotoAndStop("stopSound");
}
Create a Basic Quiz
A
simple multiple choice quiz can be created by coding the correct answer to go
to a "Correct!" page or the next question, while coding the incorrect
answers to go to a "Try Again!" page with a button that takes you
back to the question. This type of quiz won't keep track of the users score,
but is good for review quizzes.
Control a Movie Clip
This
really what a basic buttons does: plays (or stops at) a different part of the
timeline. Remember that you can control movie clips from outside of the clip by
adding movieClipInstanceName. or control the timeline of the parent by adding
(parent as MovieClip).
0 comments: