Code for various actions when clicking a button
July 4th, 2009Here is the code for moving to a different frame when clicking a button, open a pdf file when clicking a button, open a url when a button on an image is clicked, and play the flash again when an image is clicked.
/* This code is placed in frame 1 on the action layer . When the user clicks somewhere this code runs. */
stop();
import flash.events.MouseEvent;
/* Listen for mouse clicks. */
addEventListener(MouseEvent.CLICK, clickHandler);
/* When the user clicks somewhere with the mouse, this function is running. */
function clickHandler(event:MouseEvent):void
{
/* if what was clicked was not a button then do nothing */
if(!(event.target is SimpleButton))
{
return;
}
/*
These are the possible button names:
btnframe(label of frame) - example: btnframepage43 - action: goto frame 43
btnpdf(file name of pdf without the .pdf extension) -
example: btnpdfmanual -
action: open the file manual.pdf, the files is in the folder pdf/
btnurl(name of web site) - example: btnurlcnn -
example: open the website cnn, the url is given in the code below
btnplayagain - example: btnplayagain - action: play the flash file again
*/
/* remove the btn part of the button name*/
var name:String = event.target.name.toLowerCase().replace("btn", "");
if (name.indexOf("frame") != -1)
{
trace("Going to frame " + name);
gotoAndStop(name);
}
if(name.indexOf("pdf") != -1)
{
var pdfName:String = "pdf/" + name.replace("pdf", "") + ".pdf";
trace("Open PDF file " + pdfName);
navigateToURL(new URLRequest(pdfName));
return;
}
/* if an image is clicked and a url should open */
if (name.indexOf("url") != -1)
{
name = name.replace("url", "");
var urlName:String = "";
switch(name)
{
case "cnn":
urlName = "http://www.cnn.com/";
break;
case "andres":
urlName = "http://www.andresnordgreen.com/";
break;
}
trace("Open URL: " + urlName);
navigateToURL(new URLRequest(urlName));
return;
}
if (name.indexOf("playagain") != -1)
{
trace("Play again");
goto(1);
}
}









