Jason's
Tip Sheets
click
here for Actionscript 2x reference code
ACTIONSCRIPT3
– Jason Challas Art 55 ©2011 WVC
/*ALL CODE SNIPPETS FROM ACTIONSCRIPT 3, CS5 VERSION!
for button instance named "ExampleButton"
/* ACTIONS CATEGORY:
------------
/* Click to Go to Web Page
Clicking on the specified symbol instance loads the URL in a new browser window.
Instructions:
1. Replace http://www.adobe.com with the desired URL address.
Keep the quotation marks ("").
*/
ExampleButton.addEventListener(MouseEvent.CLICK, fl_ClickToGoToWebPage);
function fl_ClickToGoToWebPage(event:MouseEvent):void
{
navigateToURL(new URLRequest("http://www.adobe.com"), "_blank");
}
--------------
/* Custom Mouse Cursor
Replaces the default mouse cursor with the specified symbol instance.
*/
stage.addChild(ExampleButton);
ExampleButton.mouseEnabled = false;
ExampleButton.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor);
function fl_CustomMouseCursor(event:Event)
{
ExampleButton.x = stage.mouseX;
ExampleButton.y = stage.mouseY;
}
Mouse.hide();
//To restore the default mouse pointer, uncomment the following lines:
//ExampleButton.removeEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor);
//stage.removeChild(ExampleButton);
//Mouse.show();
--------------
/* Drag and Drop
Makes the specified symbol instance moveable with drag and drop.
*/
ExampleButton.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
function fl_ClickToDrag(event:MouseEvent):void
{
ExampleButton.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void
{
ExampleButton.stopDrag();
}
--------------
/* Play a Movie Clip
Plays the specified movie clip on stage.
Instructions:
1. Use this code for movie clips that are currently stopped.
*/
ExampleButton.play();
--------------
/* Stop a Movie Clip
Stops the specified movie clip on stage.
Instructions:
1. Use this code for movie clips that are currently playing.
*/
ExampleButton.stop();
--------------
/* Click to Hide an Object
Clicking on the specified symbol instance hides it.
Instructions:
1. Use this code for objects that are currently visible.
*/
ExampleButton.addEventListener(MouseEvent.CLICK, fl_ClickToHide);
function fl_ClickToHide(event:MouseEvent):void
{
ExampleButton.visible = false;
}
--------------
/* Show an Object
Shows the specified symbol instance.
Instructions:
1. Use this code to show objects that are currently hidden.
*/
ExampleButton.visible = true;
--------------
/* Click to Position an Object
Moves the specified symbol instance to the x-coordinate and y-coordinate you specify.
Instructions:
1. Replace the value 200 with the x-coordinate
where you want to position the object.
2. Replace the value 100 witht he y-coordinate where you want to position the object.
*/
ExampleButton.addEventListener(MouseEvent.CLICK, fl_ClickToPosition);
function fl_ClickToPosition(event:MouseEvent):void
{
ExampleButton.x = 200;
ExampleButton.y = 100;
}
--------------
/* Click to Display a TextField
Clicking on the specified symbol instance creates and displays a TextField at the x-coordinate and y-coordinate you specify.
Instructions:
1. Replace the value 200 with the x-coordinate where you want to position the TextField.
2. Replace the value 100 with the y-coordinate where you want to position the TextField.
3. Replace the string value "Lorem ipsum dolor sit amet" with the text you want to display in the TextField that appears. Keep the quotation marks.
*/
ExampleButton.addEventListener(MouseEvent.CLICK, fl_ClickToPosition);
var fl_TF:TextField;
var fl_TextToDisplay:String = "Lorem ipsum dolor sit amet."
function fl_ClickToPosition(event:MouseEvent):void
{
fl_TF = new TextField();
fl_TF.autoSize = TextFieldAutoSize.LEFT;
fl_TF.background = true;
fl_TF.border = true;
fl_TF.x = 200;
fl_TF.y = 100;
fl_TF.text = fl_TextToDisplay;
addChild(fl_TF);
}
--------------
/* Generate a Random Number
Generates a random number between 0 and a limit number you specify.
Instructions:
1. To change the maximum random value, change the number 100 in the last line of this snippet to the number you want to use.
2. This code outputs the random number to the Output panel.
*/
function fl_GenerateRandomNumber(limit:Number):Number
{
var randomNumber:Number = Math.floor(Math.random()*(limit+1));
return randomNumber;
}
trace(fl_GenerateRandomNumber(100));
--------------
/* Bring Any Clicked Object to the Front
Clicking on any symbol on the Stage moves it in front of all other instances.
*/
// This code makes all symbol instances on stage clickable by making them listen for the CLICK event.
for (var fl_ChildIndex:int = 0;
fl_ChildIndex < this.numChildren;
fl_ChildIndex++)
{
this.getChildAt(fl_ChildIndex).addEventListener(MouseEvent.CLICK, fl_ClickToBringToFront);
}
// This is the function that moves the clicked object to the front of the display list
function fl_ClickToBringToFront(event:MouseEvent):void
{
this.addChild(event.currentTarget as DisplayObject);
}
--------------
/* Simple Timer
Displays a countdown timer in the Output panel until 30 seconds elapse.
This code is a good place to start for creating timers for your own purposes.
Instructions:
1. To change the number of seconds in the timer, change the value 30 in the first line below to the number of seconds you want.
*/
var fl_TimerInstance:Timer = new Timer(1000, 30);
fl_TimerInstance.addEventListener(TimerEvent.TIMER, fl_TimerHandler);
fl_TimerInstance.start();
var fl_SecondsElapsed:Number = 1;
function fl_TimerHandler(event:TimerEvent):void
{
trace("Seconds elapsed: " + fl_SecondsElapsed);
fl_SecondsElapsed++;
}
--------------
/* Countdown Timer
Counts down from a specified number of seconds.
Instructions:
1. To change the length of the countdown, change the value 10 in the first line below to the number of seconds you want.
*/
var fl_SecondsToCountDown:Number = 10;
var fl_CountDownTimerInstance:Timer = new Timer(1000, fl_SecondsToCountDown);
fl_CountDownTimerInstance.addEventListener(TimerEvent.TIMER, fl_CountDownTimerHandler);
fl_CountDownTimerInstance.start();
function fl_CountDownTimerHandler(event:TimerEvent):void
{
trace(fl_SecondsToCountDown + " seconds");
fl_SecondsToCountDown--;
}
--------------
/*FOR TIMELINE NAVIGATION CATEGORY: SEE OTHER HANDOUTS (or click here)
/*ANIMATION CATEGORY:
/* Move with Keyboard Arrows
Allows the specified symbol instance to be moved with the keyboard arrows.
Instructions:
1. To increase or decrease the amount of movement, replace the number 5 below with the number of pixels you want the symbol instance to move with each key press.
Note the number 5 appears four times in the code below.
*/
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_PressKeyToMove);
function fl_PressKeyToMove(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
ExampleButton.y -= 5;
break;
}
case Keyboard.DOWN:
{
ExampleButton.y += 5;
break;
}
case Keyboard.LEFT:
{
ExampleButton.x -= 5;
break;
}
case Keyboard.RIGHT:
{
ExampleButton.x += 5;
break;
}
}
}
--------------
/* Move Horizontally
Moves the specified symbol instance to the left or right by decreasing or increasing its x property by the specified number of pixels.
Instructions:
1. The default movement of the code as written is to the left.
2. To move the symbol instance to the right, change '-=' to '+=' below.
3. The '-=' operator is a shortcut to typing 'ExampleButton.x = ExampleButton.x - 100'.
4. To change the distance the symbol instance moves, change the number 100 below to the number of pixels you want the symbol instance to move.
*/
ExampleButton.x -= 100;
--------------
/* Move Vertically
Moves the symbol instance up or down by decreasing or increasing its y property by the specified number of pixels.
Instructions:
1. The default movement of the code as written is up.
2. To move the symbol instance down, change '-=' to '+=' below.
3. The '-=' operator is a shortcut to typing 'ExampleButton.y = ExampleButton.y - 100'.
4. To change the distance the symbol instance moves, change the number 100 below to the number of pixels you want the symbol instance to move.
*/
ExampleButton.y -= 100;
--------------
/* Rotate Once
Rotates the symbol instance by updating its rotation property by the specified number of degrees.
Instructions:
1. The default rotation of the code as written is clock-wise.
2. To rotate the symbol instance counter clock-wise, change '+=' to '-=' below.
3. The '+=' operator is a shortcut to typing 'ExampleButton.rotation = ExampleButton.rotation + 45'.
4. To change the amount the symbol instance rotates, change the value 45 below to the number of degrees of rotation you want.
*/
ExampleButton.rotation += 45;
--------------
/* Rotate Continuously
Rotates symbol instance continuously by updating its rotation property within an ENTER_FRAME event.
Instructions:
1. The default rotation of the code as written is clock-wise.
2. To change the direction of the rotation to counter clock-wise, change '+=' to '-=' below.
3. The '+=' operator is a shortcut to typing 'ExampleButton.rotation = ExampleButton.rotation + 10'.
4. To change the speed at which the symbol instance rotates, change the number 10 below to the number of degrees you want to rotate the symbol instance each frame. Higher numbers cause faster rotation.
5. Because the animation uses an ENTER_FRAME event, it progresses only when the playhead moves to a new frame, and the speed of the animation is also affected by the frame rate of the FLA file.
*/
ExampleButton.addEventListener(Event.ENTER_FRAME, fl_RotateContinuously);
function fl_RotateContinuously(event:Event)
{
ExampleButton.rotation += 10;
}
--------------
/* Animate Horizontally
Moves the symbol instance left or right across the stage by decreasing or increasing its x property within an ENTER_FRAME event.
Instructions:
1. The default direction of the animation is to the left.
2. To change the direction of the animation to the right, change '-=' to '+=' below.
3. The '-=' operator is a shortcut to typing 'ExampleButton.x = ExampleButton.x - 10'.
4. To change the speed at which the symbol instance moves, change the number 10 below to the number of pixels you want the symbol instance to move in each frame.
5. Because the animation uses an ENTER_FRAME event, it progresses only when the playhead moves to a new frame, and the speed of the animation is also affected by the frame rate of the FLA file.
*/
ExampleButton.addEventListener(Event.ENTER_FRAME, fl_AnimateHorizontally);
function fl_AnimateHorizontally(event:Event)
{
ExampleButton.x -= 10;
}
--------------
/* Animate Vertically
Moves the symbol instance vertically on the stage by decreasing or increasing its y property within an ENTER_FRAME event.
Instructions:
1. The default direction of the animation is up.
2. To change the direction of the animation to down, change '-=' to '+=' below.
3. The '-=' operator is a shortcut to typing 'ExampleButton.y = ExampleButton.y - 10'.
4. To change the speed at which the symbol instance moves, change the number 10 below to the number of pixels you want the symbol instance to move in each frame.
5. Because the animation uses an ENTER_FRAME event, it progresses only when the playhead moves to a new frame, and the speed of the animation is also affected by the frame rate of the FLA file.
*/
ExampleButton.addEventListener(Event.ENTER_FRAME, fl_AnimateVertically);
function fl_AnimateVertically(event:Event)
{
ExampleButton.y -= 10;
}
--------------
/* Fade In Movie Clip
Fades in the symbol instance by increasing its alpha property within an ENTER_FRAME event until it is fully visible.
Instructions:
1. To change the speed at which the symbol instance fades in, change the 0.01 value below (valid values are in the range 0.0 - 1.0). Higher values cause faster fade in.
2. The '+=' operator is a shortcut to typing 'ExampleButton.alpha = ExampleButton.alpha + 0.1'.
3. Because the animation uses an ENTER_FRAME event, it progresses only when the playhead moves to a new frame, and the speed of the animation is also affected by the frame rate of the FLA file.
*/
ExampleButton.addEventListener(Event.ENTER_FRAME, fl_FadeSymbolIn);
ExampleButton.alpha = 0;
function fl_FadeSymbolIn(event:Event)
{
ExampleButton.alpha += 0.01;
if(ExampleButton.alpha >= 1)
{
ExampleButton.removeEventListener(Event.ENTER_FRAME, fl_FadeSymbolIn);
}
}
--------------
/* Fade Out Movie Clip
Fades out the symbol instance by decreasing its alpha property within an ENTER_FRAME event until it is invisible.
Instructions:
1. To change the speed at which the symbol instance fades out, change the 0.01 value below (valid values are in the range 0.0 - 1.0). Higher values cause faster fade out.
2. The '-=' operator is a shortcut to typing 'ExampleButton.alpha = ExampleButton.alpha - 0.1'.
3. Because the animation uses an ENTER_FRAME event, it progresses only when the playhead moves to a new frame, and the speed of the animation is also affected by the frame rate of the FLA file.
*/
ExampleButton.addEventListener(Event.ENTER_FRAME, fl_FadeSymbolOut);
ExampleButton.alpha = 1;
function fl_FadeSymbolOut(event:Event)
{
ExampleButton.alpha -= 0.01;
if(ExampleButton.alpha <= 0)
{
ExampleButton.removeEventListener(Event.ENTER_FRAME, fl_FadeSymbolOut);
}
}
--------------
/*LOAD/UNLOAD CATEGORY:
/* Load External Text
Loads an external text file and displays it in the Output panel.
Instructions:
1. Replace "http://www.helpexamples.com/flash/text/loremipsum.txt" with the URL address of the text file you would like to load.
The address can be a relative link or an "http://" link.
The address must be placed inside quotation marks ("").
*/
var fl_TextLoader:URLLoader = new URLLoader();
var fl_TextURLRequest:URLRequest = new URLRequest("http://www.helpexamples.com/flash/text/loremipsum.txt");
fl_TextLoader.addEventListener(Event.COMPLETE, fl_CompleteHandler);
function fl_CompleteHandler(event:Event):void
{
var textData:String = new String(fl_TextLoader.data);
trace(textData);
}
fl_TextLoader.load(fl_TextURLRequest);
--------------
/*AUDIO AND VIDEO CATEGORY:
/* Click to Play/Stop Sound
Clicking on the symbol instance plays the specified sound.
Clicking on the symbol instance a second time stops the sound.
Instructions:
1. Replace "http://www.helpexamples.com/flash/sound/song1.mp3" below with the desired URL address of your sound file. Keep the quotation marks ("").
*/
ExampleButton.addEventListener(MouseEvent.CLICK, fl_ClickToPlayStopSound);
var fl_SC:SoundChannel;
//This variable keeps track of whether you want to play or stop the sound
var fl_ToPlay:Boolean = true;
function fl_ClickToPlayStopSound(evt:MouseEvent):void
{
if(fl_ToPlay)
{
var s:Sound = new Sound(new URLRequest("http://www.helpexamples.com/flash/sound/song1.mp3"));
fl_SC = s.play();
}
else
{
fl_SC.stop();
}
fl_ToPlay = !fl_ToPlay;
}
--------------
/* Click to Stop All Sounds
Clicking on the symbol instance stops all sounds currently playing.
*/
ExampleButton.addEventListener(MouseEvent.CLICK, fl_ClickToStopAllSounds);
function fl_ClickToStopAllSounds(event:MouseEvent):void
{
SoundMixer.stopAll();
}
--------------
/* On Cue Point Event Handler
Executes the fl_CuePointHandler function defined below each time a cue point is passed in the specified video instance.
Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when cue points are reached in a video that is playing.
*/
import fl.video.MetadataEvent;
ExampleButton.addEventListener(MetadataEvent.CUE_POINT, fl_CuePointHandler);
function fl_CuePointHandler(event:MetadataEvent):void
{
// Start your custom code
// This snippet code displays the cue point's name in the Output panel
trace(event.info.name);
// End your custom code
}
--------------
/* Click To Play Video (Requires FLVPlayback component)
Clicking on the symbol instance plays a video in the specified FLVPlayback component instance.
Instructions:
1. Replace video_instance_name below with the instance name of the FLVPlayback component that you want to play the video.
The specified instance of FLVPlayback video component on stage will play.
2. Make sure you have assigned a video source file in the properties of the FLVPlayback component instance.
*/
ExampleButton.addEventListener(MouseEvent.CLICK, fl_ClickToPlayVideo);
function fl_ClickToPlayVideo(event:MouseEvent):void
{
// Replace video_instance_name with the instance name of the video component
video_instance_name.play();
}
--------------
/* Click To Pause Video (Requires FLVPlayback component)
Clicking on the symbol instance pauses the video in the specified FLVPlayback component instance.
Instructions:
1. Replace video_instance_name below with the instance name of the FLVPlayback component that you want to pause.
*/
ExampleButton.addEventListener(MouseEvent.CLICK, fl_ClickToPauseVideo);
function fl_ClickToPauseVideo(event:MouseEvent):void
{
// Replace video_instance_name with the instance name of the video component
video_instance_name.pause();
}
--------------
/* Click To Rewind Video (Requires FLVPlayback component)
Clicking on the symbol instance rewinds the video in the specified FLVPlayback component instance.
Instructions:
1. Replace video_instance_name below with the instance name of the FLVPlayback component that you want to rewind.
*/
ExampleButton.addEventListener(MouseEvent.CLICK, fl_ClickToPauseVideo);
function fl_ClickToPauseVideo(event:MouseEvent):void
{
// Replace video_instance_name with the instance name of the video component
video_instance_name.seek(0);
}
--------------
/* Click To Set Video Source (Requires FLVPlayback)
Clicking on the specified symbol instance plays a new video file in the specified FLVPlayback component instance. The specified FLVPlayback component instance will pause.
Instructions:
1. Replace video_instance_name below with the instance name of the FLVPlayback component that you want to play the new video file.
2. Replace "http://www.helpexamples.com/flash/video/water.flv" below with the URL of the new video file you want to play. Keep the quotation marks ("").
*/
ExampleButton.addEventListener(MouseEvent.CLICK, fl_ClickToSetSource);
function fl_ClickToSetSource(event:MouseEvent):void
{
video_instance_name.source = "http://www.helpexamples.com/flash/video/water.flv";
}
--------------
/* Click to Seek to Cue Point (Requires FLVPlayback component)
Clicking on the specified symbol instance seeks to a cue point of the video in the specified FLVPlayback component instance.
Instructions:
1. Replace video_instance_name below with the instance name of the FLVPlayback component that you want to seek.
2. Replace "Cue Point 1" below with the name of the cue point to seek to. Keep the quotation marks ("").
*/
ExampleButton.addEventListener(MouseEvent.CLICK, fl_ClickToSeekToCuePoint);
function fl_ClickToSeekToCuePoint(event:MouseEvent):void
{
// Replace video_instance_name with the instance name of the video component.
// Replace "Cue Point 1" with the name of the cue point to seek to.
var cuePointInstance:Object = video_instance_name.findCuePoint("Cue Point 1");
video_instance_name.seek(cuePointInstance.time);
}
--------------
/* Create a NetStream Video
Displays a video on stage without using the FLVPlayback video component.
Instructions:
1. If you are connecting to a video file that is on a streaming server such as Adobe Flash Media Server 2, replace 'null' below with the URL address of the video file. Place quotation marks ("") around the URL address.
2. If you are connecting to a local video file or one that is not using a streaming server, leave 'null' in place below.
3. Replace "http://www.helpexamples.com/flash/video/water.flv" with the URL of the video you want to play. Keep the quotation marks ("").
*/
var fl_NC:NetConnection = new NetConnection();
fl_NC.connect(null); // starts a connection; null is used unless using Flash Media Server
var fl_NS:NetStream = new NetStream(fl_NC);
fl_NS.client = {};
var fl_Vid:Video = new Video();
fl_Vid.attachNetStream(fl_NS);
addChild(fl_Vid);
fl_NS.play("http://www.helpexamples.com/flash/video/water.flv");
-------------- |