Monday, June 29, 2009

Making a weblink with Actionscript 3.0 in Adobe Flash

The previous code tutorial explains how to make a flash movie
automatically jump to a web page when it reaches a specific frame in
your timeline. Here I will show you have to make a button that will
allow the user to jump to a webpage when they choose to. This is the
kind of interaction that you would build into a banner add, or any
other time you would like to create a user initiated action.

The complete code block looks like this:



stop();

var myWebSite01:URLRequest = new URLRequest("insert URL here");

myWebLink01_btn.addEventListener(MouseEvent.CLICK, gotoWebLink01);

function gotoWebLink01(myEvent:MouseEvent) {

navigateToURL(myWebSite01);

}



To make this code work all you would have to do is insert your URL into the "insert URL here" space and change the myWebLink01_btn text to reflect your button's instance name.

The specifics of each bit of code are explained below:



/*The following stop function is used to stop the playhead from advancing and therefore repeating the movie.

Depending on the type of project you are creating you may want it to repeat and can therefore just skip this line of code.
*/

stop();


/*
1: As with any external link you have to create a URLRequest
variable to store the URL that you will want to navigate to later. As
with all variable declarations the left side of the equation states the
name of the variable (here the name myWebSite01 is used) and the type
of data (or data type) the variable will hold. The right side of the
equation instanciates the new variable and sets its value (the URL it
holds). You would have to create a separate URLRequest variable for
each web address you want to use.
*/

var myWebSite01:URLRequest = new URLRequest("http://www.agitraining.com/eval");

/*
2:
In order for an object on the stage to respond to the user you have to
attach an event Listener to it. An event listener will wait for a
specific event to occur and then it triggers a function.
Note: A
LIstener is similar in concept to the reciever on a cell phone, it is
completely passive until someone calls you, then it triggers the ringer.
3:
The addEventListener method is added to the instance of a movieClip or
Button on your stage. Every object on yoru stage has methods and
properties, a method is something that an object can do while a
property is something that it possesses.
Note: If an object on the
stage can be compared to a human being then out ability to jump up and
down would be a method while our height and weight would be properties.
4:
The addEventListener method takes two parameters the irst is the type
of event that it listenes for (in this case the click MouseEvent) and
secondly the name of the function that it is going to fire off when the
Event is found to be true. The Function name in this case is
gotoWebLink01, this is a completely arbitrary name and this function
will be declared in teh next step.
*/


myWebLink01_btn.addEventListener(MouseEvent.CLICK, gotoWebLink01);


/*
5: Functions are declared using the function keyword followed
by the name of the custom function that you want to create. A function
that is going to be triggered by an eventListener must be prepared to
receive information about the event that triggered it from this
listener. This is the information that is placed inside of the
parenthesis after the function name. In this case myEvent is an
internal variable that is used ot hold this information once it is
passed from the listener to the function.
6: Once the function is
triggered whatever commands are located between the braces are
initiated. In this case that is the navigateToURL function.
*/
function gotoWebLink01(myEvent:MouseEvent) {
navigateToURL(myWebSite01);
}


Technorati Tags: Adobe Flash, Actionscript 3.0, Actionscript, Flash


_______________________

No comments:

Post a Comment