Slideshow with dynamic menu and loader in ActionScripting 3.0
This slideshow will dynamically pull from xml file with image location, link and description. Let's start out with the xml
<site>
<links>
<link><![CDATA[test1.html]]></link>
<title><![CDATA[HOME]]></title>
<image><![CDATA[tutorial4/01.jpg]]></image>
<des><![CDATA[CreativeReform.com - photo 1]]></des>
</links>
<links>
<link><![CDATA[test2.html]]></link>
<title><![CDATA[ABOUT]]></title>
<image><![CDATA[tutorial4/02.jpg]]></image>
<des><![CDATA[CreativeReform.com - photo 2]]></des>
</links>
<links>
<link><![CDATA[test3.html]]></link>
<title><![CDATA[PRODUCT]]></title>
<image><![CDATA[tutorial4/03.jpg]]></image>
<des><![CDATA[CreativeReform.com - photo 3]]></des>
</links>
<links>
<link><![CDATA[test4.html]]></link>
<title><![CDATA[TEST 4]]></title>
<image><![CDATA[tutorial4/04.jpg]]></image>
<des><![CDATA[CreativeReform.com - photo 4]]></des>
</links>
<links>
<link><![CDATA[test5.html]]></link>
<title><![CDATA[TEST 5]]></title>
<image><![CDATA[tutorial4/05.jpg]]></image>
<des><![CDATA[CreativeReform.com - photo 5]]></des>
</links>
<links>
<link><![CDATA[test6.html]]></link>
<title><![CDATA[TEST 6]]></title>
<image><![CDATA[tutorial4/06.jpg]]></image>
<des><![CDATA[CreativeReform.com - photo 6]]></des>
</links>
<links>
<link><![CDATA[test7.html]]></link>
<title><![CDATA[TEST 7]]></title>
<image><![CDATA[tutorial4/07.jpg]]></image>
<des><![CDATA[CreativeReform.com - photo 7]]></des>
</links>
<links>
<link><![CDATA[test8.html]]></link>
<title><![CDATA[TEST 8]]></title>
<image><![CDATA[tutorial4/08.jpg]]></image>
<des><![CDATA[CreativeReform.com - photo 8]]></des>
</links>
</site>
Create a square of 28px by 28px. Turn it to a MovieClip and give it an instance name of "myShape". Create a dynamic text on another layer of this and give it an instance name of "myNum". Turn both of these to a MovieClip and give it a class of MyMenu. Create another dynamic text and give it an instance name of "loaderTxt". Turn this into a MovieClip and give it a class name of "MyLoader". Create another dynamic textfield on stage and give it an instance name of myTxt. That's it for creating instances. Let's get on with the coding:
//Copyright by CreativeReform.com
//Developed by Don Le - contact: donle21@yahoo.com
//Feel free to use any portion of the code
//I will be greatly appreciated if CreativeReform.com is part of the credit or linked to.
//import for Tweening animation
import fl.transitions.Tween;
import fl.transitions.easing.*;
//variables
var myXML:XML;
var menuItem:MyMenu = new MyMenu();
var xmlPath = "flash_tutorials/tutorial4.xml";
var myArray:Array = new Array();
var loader:URLLoader = new URLLoader;
var myLoader:Loader = new Loader();
var myTimer:Timer = new Timer(4000);
var i = 0;
var timerCounter = 1;
var progressLoad:MyLoader = new MyLoader();
//load xml
loader.load(new URLRequest(xmlPath));
loader.addEventListener(Event.COMPLETE, runData);
//load xml and call function to create myMenu function
function runData(e:Event):void{
myXML = new XML(loader.data);
myXML.ignoreWhitespace = true;
myMenu();
}
function myMenu():void{
//creating menu items, start timer and add button stages.
for each ( var links:XML in myXML.links.link){
menuItem = new MyMenu();
//assign name, link, image path, description and number to menu
menuItem.name = "menuItem"+i;
menuItem.link = myXML.links.link[i];
menuItem.image = myXML.links.image[i];
menuItem.des = myXML.links.des[i];
menuItem.num = i;
//set positioning and tween menu on load up
menuItem.x = 30 + i*40;
menuItem.y = 180;
menuItem.myNum.text = i+1;
menuItem.mouseChildren = false;
menuItem.buttonMode = true;
//var myTween = new Tween(menuItem,"x", Elastic.easeOut, 0, 30 + i*40, 1, true);
//add mouse event for each menu item
menuItem.addEventListener(MouseEvent.MOUSE_OVER, mouseOverObject);
menuItem.addEventListener(MouseEvent.MOUSE_OUT, mouseOutObject);
menuItem.addEventListener(MouseEvent.MOUSE_DOWN, mouseClickObject);
addChild(menuItem);
i++;
//load 1st image dynamically
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadMyLoader);
myLoader.load(new URLRequest(myXML.links.image[0]));
myTxt.text = myXML.links.des[0];
//set timer
myTimer.addEventListener(TimerEvent.TIMER, timerEvent);
myTimer.start();
function timerEvent(e:TimerEvent):void{
//remove alpha on all menuItems
removeAlpha();
//reset timerCounter if it's equal to the number of items on xml
if
timerCounter = 0;
}
if
//load image and set menu item's alpha to 50%
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadMyLoader);
myLoader.load(new URLRequest(myXML.links.image[timerCounter]));
myTxt.text = myXML.links.des[timerCounter];
getChildByName("menuItem"+timerCounter).alphar = 0.5;
timerCounter++;
}
}
function removeAlpha():void{
//set alpha to 100% on all menuitem
for (var m = 0; m < i ; m++){
getChildByName("menuItem"+m).alphar = 1;
}
}
function mouseOverObject(e:Event):void{
//animate menu item on mouse over with moving up and increasing width
var myTween = new Tween(e.target.myShape,"scaleX", Elastic.easeOut, 1, 1.2, 1, true);
var myTween2 = new Tween(e.target,"y", Elastic.easeOut, e.target.y, 175, 1, true);
}
function loadMyLoader(e:Event):void{
//tween background image on load and add image to layer 1, which is
//behind the menuitems
var myTween = new Tween(myLoader,"alpha",Elastic.easeOut, 0, 1, 1, true);
var myTween2 = new Tween(myLoader,"y",Elastic.easeOut, -40, 0, 1, true);
addChildAt(myLoader,1);
//add click event on background and remove loader progress
myLoader.addEventListener(MouseEvent.CLICK, myLoaderClick);
removeChild(progressLoad);
}
function myLoaderClick(e:MouseEvent):void{
//display link
//this is where you can add navigateToURL function and pass in the link
//to go to desire page
var newCounter:uint = timerCounter-1;
myTxt.text = myXML.links.link[newCounter];
}
function mouseOutObject(e:Event):void{
//reset animation on menuitem on mouse out and start timer again
var myTween = new Tween(e.target.myShape,"scaleX", Elastic.easeOut, e.target.myShape.scaleX, 1, 1, true);
var myTween2 = new Tween(e.target,"y", Elastic.easeOut, e.target.y, 180, 1, true);
myTimer.start();
}
function mouseClickObject(e:Event):void{
//display description on background
//update background and set menuitem's alpha to 50%
//also set timerCounter to the current's number
myTxt.text = e.target.des;
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadMyLoader);
myLoader.load(new URLRequest(e.target.image));
removeAlpha();
getChildByName("menuItem"+e.target.num).alphar = 0.5;
timerCounter = e.target.num+1;
myTimer.stop();
}
function progressHandler(evt:ProgressEvent):void{
//showing loading percentage
var myPercentage = Math.ceil((evt.bytesLoaded/evt.bytesTotal*100));
trace(myPercentage+" %");
progressLoad.loaderTxt.text = myPercentage +" %";
//placing loader in middle of the page
progressLoad.x = stage.stageWidth/2;
progressLoad.y = stage.stageHeight/2;
addChild(progressLoad);
}
}
That's it. Enjoy!