Adobe Flash Basic Codes

Page 1

Knox Grammar School І TAS Department

Knox TAS Department

Flash CS4 – Basic ActionScript 3.0 Codes Contents of Button Functions  Go to a Web Page  Create a custom mouse cursor  Move an object with keyboard arrows  Go to a Frame and Stop  Go to a Frame and Play  Drag and Drop an object  Play and Stop a sound Introduction The codes below must only be inserted in the frames for which they are relevant. This can be done by creating a new layer and using keyframes at the beginning and end to indicate the frames between which the code needs to appear. In order to use the code, simply copy and paste the text within the boxes only. Note:  Where the term; button_1 appears, the text must be replaced with the instance name of the button to which the code is being applied. 

Where numbers appear in brackets, such as; (5), the number must be replaced with the desired frame number.

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 (""). button_1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToWebPage); function fl_ClickToGoToWebPage(event:MouseEvent):void { navigateToURL(new URLRequest("http://www.adobe.com"), "_blank"); }


Knox Grammar School І TAS Department Custom Mouse Cursor Replaces the default mouse cursor with the specified symbol instance. stage.addChild(button_1); button_1.mouseEnabled = false; button_1.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor); function fl_CustomMouseCursor(event:Event) { button_1.x = stage.mouseX; button_1.y = stage.mouseY; } Mouse.hide();

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: { button_1.y -= 5; break; } case Keyboard.DOWN: { button_1.y += 5; break; } case Keyboard.LEFT: { button_1.x -= 5; break; } case Keyboard.RIGHT: { button_1.x += 5; break; } } }


Knox Grammar School І TAS Department Click to Go to Frame and Stop Clicking on the specified symbol instance moves the playhead to the specified frame in the timeline and stops the movie. Can be used on the main timeline or on movie clip timelines. Instructions: 1. Replace the number 5 in the code below with the frame number you would like the playhead to move to when the symbol instance is clicked. button_1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame); function fl_ClickToGoToAndStopAtFrame(event:MouseEvent):void { gotoAndStop(5); }

Click to Go to Frame and Play Clicking on the specified symbol instance moves the playhead to the specified frame in the timeline and continues playback from that frame. Can be used on the main timeline or on movie clip timelines. Instructions: 1. Replace the number 5 in the code below with the frame number you would like the playhead to move to when the symbol instance is clicked. button_1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame); function fl_ClickToGoToAndPlayFromFrame(event:MouseEvent):void { gotoAndPlay(5); }

Drag and Drop Makes the specified symbol instance moveable with drag and drop. button_1.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag); function fl_ClickToDrag(event:MouseEvent):void { button_1.startDrag(); } stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop); function fl_ReleaseToDrop(event:MouseEvent):void { button_1.stopDrag(); } Click to Play/Stop Sound


Knox Grammar School І TAS Department 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 (""). button_1.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; }


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.