7 Useful AS3 Snippets
by Tahoe Designer on Nov.01, 2009, under Design, Personal, Technology, Website Design / Web Development
Below are some useful AS3 snippets that I use quite frequently. Thought they might come in handy for you too!
1. Get a url when the button is clicked.
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.*;
// ADD EVENT LISTENER AND FUNCTION TO THE BUTTON
button_name.addEventListener(MouseEvent.CLICK, go);
function go(event:MouseEvent):void {
var url:String = "http://www.yourdomain.com";
var request:URLRequest = new URLRequest(url);
navigateToURL(request, '_self');
}
2. Import Data from a JSON Feed.
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.*;
import com.adobe.serialization.json.*;
// CREATE THE OBJECTS
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest();
// CONNECT TO AND LOAD THE DATA
request.url = "http://tahoedesigner.com/index.php?feed=json";
loader.load(request);
// ATTACH LISTENR TO LOADER OBJECT
loader.addEventListener(Event.COMPLETE, decodeJSON) ;
// DECODE JSON
function decodeJSON(event:Event):void {
var loader:URLLoader = URLLoader(event.target) ;
var Blog:Array = JSON.decode(loader.data) ;
// TRACE JSON DATA
trace(Object[0].property);
// TRACE JSON DATA USING A LOOP
for (var key:Object in Blog) {
trace(Object[key].title) ;
trace(Object[key].excerpt) ;
trace(Object[key].date) ;
}
*/
3. Strip out characters or returns from a string.
var string:String = new String();
string = 'AS3 by Tahoe Designer \r\n AS3 is fun!';
string = string.split("\r").join("");
textBx.htmlText = string;
4. Load audio from an external mp3.
import flash.net.URLRequest ;
import flash.net.URLLoader ;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
var audio:Sound = new Sound(new URLRequest("http://yourdomain/audio.mp3"));
audio.addEventListener(Event.COMPLETE, doLoadComplete);
var audioChannel:SoundChannel = new SoundChannel();
audioChannel = audio.play();
audioChannel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete);
function doLoadComplete($evt:Event):void
{
//trace("Song loaded.");
}
function doSoundComplete($evt:Event):void
{
//trace("Song done.");
}
5. Load audio from a mp3 in Flash library.
import flash.net.URLRequest ;
import flash.net.URLLoader ;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
var audio:audio_track;
audio=new audio_track();
var audioChannel:SoundChannel;
audioChannel = audio.play();
audioChannel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete);
function doLoadComplete($evt:Event):void
{
trace("Song loaded.");
}
function doSoundComplete($evt:Event):void
{
trace("Song done.");
}
6. Set up a scrollbar for a textfield.
import flash.text.*;
import fl.controls.UIScrollBar;
import fl.controls.ScrollBarDirection;
// SET UP SCROLLBAR
var sb:UIScrollBar = new UIScrollBar();
sb.setSize(textBx.width, textBx.height);
sb.scrollTarget = textBx;
sb.x = textBx.x + textBx.width;
sb.y = textBx.y;
addChild(sb);
}
7. Set up CSS for a textfield.
import flash.text.*;
var style:StyleSheet = new StyleSheet();
textBx.styleSheet = style;
// CREATE THE OBJECT
var bold:Object = new Object();
bold.fontWeight = "bold";
bold.fontSize = 15;
bold.color = "#03aefe";
style.setStyle(".bold", bold);
Leave a Reply
You must be logged in to post a comment.
