Thursday, June 21, 2007

URLRequest in AS3

ActionScript 3.0 is very different from its predecessors. Touted as a full-fledged programming language, it has some characteristics built into it that make it work more like other web programming languages and less like JavaScript, which is a good thing but will take some time getting used to.

For example, in AS 2.0, if I wanted to script a button to take me to a URL, I would simply select the button on the stage and write the following action in the Actions panel:

on (release) {
getURL("http://www.adobe.com", _blank);
}

That's no longer possible in AS 3.0. All scripts now must reside in the timeline or in an external file.

Also, that on (release) statement above makes some assumptions about what is going to be released and what object will be engaged when that event occurs. Event handlers are now more precise in AS 3.0 and it requires the aid of event listeners to trigger functions. The code is a lot longer than in the previous example, but the result is the same.

First, we start off by declaring a variable which will hold the URL we want to go to:

var link:URLRequest = new URLRequest("http://www.adobe.com");

You can still use buttons in Flash, but here I'm using a Movie Clip object as a button called 'button_mc.' I need to add an event listener to the object. The event listener has two arguments, the type of event and a function that will be triggered when that event occurs. In this case, I'm using a mouse event called click and creating a function called onClick.

button_mc.addEventListener(MouseEvent.CLICK, onClick);

The function is pretty simple, although the syntax is very different. Within the funtion we set an event parameter which corresponds to the event in the event listener. Then we use the navigateToURL function with a parameter called link, which looks back at our variable that holds the URL we want to navigate to.

function onClick(event:MouseEvent):void {
navigateToURL(link);
}

Since a Movie Clip object is not a button, it doesn't have mouse behaviors built into it. To enable mouse events on a Movie Clip, add the following:

button_mc.buttonMode = true;

You can see that the code is a lot longer than the AS 2 version, but it's more precise and consistent. Again, it will take a little getting use to, but it's worth it.

instructional design online training content development accessibility
section 508 interactivity learning activities
elearning podcasts usability CSS Flash XHTML
semantic markup

10 Comments:

Blogger R said...

you saved my life. thank you :D

11:45 PM  
Anonymous Anonymous said...

What do I need if I want the button to load "_top" instead of opening a brand new window (as in "_blank"?

12:29 PM  
Blogger alemieux3 said...

In the navigatetoURL(link) statement, put a comma after link and then your target

navigateToURL(link, "_parent");

12:42 PM  
Anonymous Vbullentinci said...

wonderful tip thanks alot

12:56 PM  
Blogger Jennifer said...

Eek!

Thank you for this! I am slowly getting the big picture about AS3, coming from a ASS1 background.

4:33 PM  
Anonymous Anonymous said...

hi my name is pau..
i just got some question here..
how can i pass a value from my php and load it up in flash actionscript 3? sample i have this 3 menubar at my flash and at first load ill pas a variable that will read from AS3 and make my menu1 as active or menu2 or menu3. depending on what value i have on my variable..

regards and thanks for the replay.. ^_^

12:31 PM  
Blogger alemieux3 said...

Pau,

Haven't done much with AS3/PHP communication, but I'd imagine that it would be similar to communication with JS. You'll need to use the ExternalInterface class. At least that will get you in the right direction.

6:40 AM  
Anonymous GrayWolfShaman said...

This was a fantastic, well written, and CORRECT posting - I needed help making my flash splash page take visitors onward to the rest of my site using AS3 in CS3 Flash and after hours of searching for help I could understand your post came to my rescue! My page is working now and I...can go to bed! :o) Thanks again!

9:00 PM  
Blogger anu said...

Hi, I am writing a class which will look for a file in two different places on local machine.

Donot know where I am going wrong.. Can anyone help me please:

***********************************
package{
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.text.*;
import flash.utils.*;

public class Testing extends MovieClip{
private var urlLoader:URLLoader = new URLLoader();
private var mediaLoader:Loader = new Loader();
private var urlRequest:URLRequest;
private var scoRequest:URLRequest;
private var courseRequest:URLRequest;
private var statusField:TextField = new TextField();

private var xmlFile:String = "";
private var swfFile:String = "";
var tempArray:Array = new Array();
var tempArray1:Array = new Array();

public function Testing():void{
// Create the status TextField
statusField.border = false;
statusField.background = true;
statusField.backgroundColor = 0x000000;
statusField.textColor = 0xffffff;
statusField.wordWrap = true;
statusField.width = statusField.height = 350;


//This line kicks things off and gets the code executing
this.loaderInfo.addEventListener(Event.INIT,initEventHandler);

}


private function initEventHandler(event:Event):void {
var tempStr:String = "../xml_010/swf/Test.swf?t010.xml";
var tempArray1:Array = new Array();*/

var tempInt:int = 0;
tempArray = tempStr.split("?");
tempArray1 = tempStr.split("/");


if(tempArray.length > 1){
tempArray = tempArray[1].split(",");

if(tempArray.length > 1){
//Get XML filename
xmlFile = tempArray[0];
swfFile = tempArray[1];

}
else{
xmlFile = tempArray[0];
swfFile = "";

}

if(tempArray1[0]="xml_010")
{

urlRequest = new URLRequest("../xml_010/xml/test_xml/" + tempArray[0]);
}
else
{

urlRequest = new URLRequest("../xml/test_xml/" + tempArray[0]);
}


}
else{

xmlFile = "Duplicate.xml";
swfFile = "";
urlRequest = new URLRequest(xmlFile);

}

//Setup the loader and its listener
urlLoader.addEventListener(Event.COMPLETE,xmlCompleteHandler);
urlLoader.load(urlRequest);

}


}

******************************

Above is the code. the tempArray1 is not looking for the array in if condition , always the else condition is getting true. Can anyone help me please

2:09 PM  
Blogger anu said...

for my previous comment,

I changed this "if(tempArray1[0]=="xml_010")" previously it was "if(tempArray1[0]="xml_010")"

2:18 PM  

Post a Comment

<< Home