Actionscript 3.0 Tips

Making an animation load a Web Page
//--add the following code to the frame you want to call the URL
//--where it says, "http://site" add the url of the site or page: "http://www.google.ca"
//--you can load the url into a specific frame or window by changing, "_blank"
//-- the code and flash app must be hosted on a web server for the below to work

var url:String = "http://site";
var request:URLRequest = new URLRequest(url);
try {
  navigateToURL(request, '_blank'); // second argument is target
} catch (e:Error) {
  trace("Error occurred!");
}


Making an animation stop or loop
//--add the following code to the frame you want to call the loop
//--change the number to the frame you want to jump to for the loop
//-- change the line to say: "stop();" if you want to just make the animation stop.

gotoAndPlay(1);


Add a "Mouse Up" Event to a Movie Clip
//-- the following actionscript attaches a bit of
//-- logic to a movieclip called "movObj"

movObj.addEventListener
(
	MouseEvent.MOUSE_UP,  
	function(evt:MouseEvent):void 
	{
    gotoAndStop(10);
  	}
);

Add a "Mouse Up" Event to a Button and load a new Web page.
//-- the following actionscript attaches a bit of
//-- logic to a button called "button1"

button1.addEventListener
(
MouseEvent.MOUSE_UP,
  function(evt:MouseEvent):void
  {
   var req:URLRequest = new URLRequest("http://www.your_url.com");
    navigateToURL(req, "_blank");
   }
);

Drag and Drop.
//-- the following actionscript attaches two functions
//-- to a button called "button1". MouseDown makes the movieclip draggable
//-- MouseUp stops the drag and does a hit-test on a movieclip named "target"
//-- If the hit-test is a success, then it makes "button1" invisible.

button1.addEventListener
(
MouseEvent.MOUSE_DOWN,
  function(evt:MouseEvent):void
  {
   button1.startDrag();
   }
);

button1.addEventListener
(
MouseEvent.MOUSE_UP,
  function(evt:MouseEvent):void
  {
   button1.stopDrag();
   if(button1.hitTestObject(target))
   	{
		button1.visible=false;
	}
   }
);

Deecting Key Input for Game or Interaction Control.
//-- the following actionscript listens for a keypress.
//-- if the key pressed is the right-arrow key then the script does something.

function hearKey(yourEvent:KeyboardEvent):void
	{
	if (yourEvent.keyCode==Keyboard.RIGHT)
		{
		trace("the right arrow key was pressed");
		};
	};
stage.addEventListener(KeyboardEvent.KEY_DOWN, hearKey);

Loading an external movie or image into another movie.
//-- the following actionscript loads an image into another movieclip.
//-- when button1 is clicked upon it loads an image into the movieclip named "placeholder".

button1.addEventListener
(
MouseEvent.MOUSE_UP,
  function(evt:MouseEvent):void
  {
   var loadit = new Loader();
	if(placeholder.loadit){placeholder.removeChild(loadit);}
	placeholder.addChild(loadit);
	loadit.load(new URLRequest("images/t2.jpg"));
   }
);

Sending Flash Form Variables to a PHP mailer Script.
//-- the following actionscript collects form variables from text fields and submits them.
//-- to a php script that then creates and sends an email.

var variables:URLVariables = new URLVariables();
var varSend:URLRequest = new URLRequest("flashmailprocessor.php");
var varLoader:URLLoader = new URLLoader;

varSend.method = URLRequestMethod.POST;
varSend.data = variables;

function sendActions(event:MouseEvent):void{
 variables.yourname = yourname.text; //-------the viewers name
 variables.youremail = youremail.text; //-------the viewers email
 variables.yoursubject = yoursubject.text; //-------the viewers subject
 variables.yourcomment = yourcomment.text; //-------the viewers comments
 variables.formaction = ""; //--------enter the secret code here
 variables.to = "randy@mediaboot.com"; //------enter your email address here
	
 //--------------------------------------send the variables to the web address
 varLoader.load(varSend);
 gotoAndStop(2);//--------goto a frame that displays a thank you message
}
sendbutton.addEventListener(MouseEvent.CLICK, sendActions);
stop();