Table of Contents
ActionScript 3.0 is a robust yet easy to use object-oriented programming language that works behind the web apps we use. It gets executed by the ActionScript Virtual Machine (AVM) and ActionScript 3 (AS3) is executed with the new Virtual Machine called AVM2. ActionScript 3 also uses an improved application programming interface (API) and E4X language extension to add XML as a native data type of the language. A developer, familiar with OOP can easily mingle with ActionScript 3. Here we have elaborated some useful ActionScript 3 Code Snippets (AS3 Code Snippets) to use in their regular flash developments.
ActionScript 3 code snippets are helpful for flash developers which saves valuable time and energy. Below are some flash code snippets which are ready for use, just copy and paste any ActionScript 3 code snippets in your as3 coding.
Useful ActionScript 3 Code Snippets
- Make window full screen using AS3
- Play an external Mp3 using AS3
- Custom right click menu using AS3
- Go to URL (getURL) using AS3
- Disable tab (yellow rectangle) using AS3
- Quit File using AS3
Elaborated AS3 Code Snippets
To make a fullscreen window
This AS3 code snippet can be used to make the executed flash windows full screen. In AS2 it was a single line code but in actionscript 3 we need to import flash display class.
import flash.display.StageDisplayState; stage.displayState = StageDisplayState.FULL_SCREEN;
Play an external Mp3 file in flash
The following AS3 code can be used to play an external Mp3 file in flash. Embedding MP3 files into flash makes the executed file bigger in size and of course increases the loading time. So the code below is really helpful when it comes to mingle with sound in flash.
var mySound:Sound = new Sound(); var myURL:URLRequest = new URLRequest("song.mp3"); //Change file name here var mySoundChannel:SoundChannel = new SoundChannel(); mySound.load(myURL); play_btn.addEventListener(MouseEvent.CLICK, playSound); stop_btn.addEventListener(MouseEvent.CLICK, stopSound); mySoundChannel = mySound.play(); mySoundChannel.addEventListener(Event.SOUND_COMPLETE, endOfSound); //Functions written can be used with buttons to play or stop the sound function playSound(e:MouseEvent):void { mySoundChannel = mySound.play(); } function stopSound(e:MouseEvent):void { mySoundChannel.stop(); } function endOfSound(e:Event):void { trace("Sound playback is complete.") mySoundChannel = mySound.play(); }
Custom right click menu in AS3
The AS3 code written below customizes right click menu in flash executed file.
var my_menu:ContextMenu = new ContextMenu(); my_menu.hideBuiltInItems(); var my_notice = new ContextMenuItem("Your Company Name"); var my_email = new ContextMenuItem("your_mail@my_company.com"); var my_copyright = new ContextMenuItem("Copyright - 2012"); my_copyright.enabled = false; my_copyright.separatorBefore = true; function openLink(e:ContextMenuEvent):void{ navigateToURL(new URLRequest("http://yourcompany.com ")); } my_notice.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, openLink); my_menu.customItems.push(my_notice,my_email,my_copyright); contextMenu = my_menu;
Go to URL (getURL in AS3)
Actionscript 3 code that opens specified URL or local file.
mybtn.addEventListener(MouseEvent.CLICK, mybtnClickHandler); function mybtnClickHandler(event:MouseEvent):void { var request:URLRequest = new URLRequest("http://www.somewebsite.com"); navigateToURL(request); }
Disable tab in AS3 (yellow rectangle)
Below is an AS3 code to disable tab key in flash
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown); private function myKeyDown(event:KeyboardEvent):void { if (event.keyCode == Keyboard.TAB) { event.currentTarget.tabChildren = false; } }
Quit File AS3 code
Actionscript 3 code used to quit flash
quitme.addEventListener(MouseEvent.CLICK, quitmeClickHandler); function quitmeClickHandler(event:MouseEvent):void { fscommand("quit"); }