Hi. I’m new to AS3, and I have a class file that displays a slideshow and preloads each image in an xml file. the code is below. What I want to know is, is there any way to improve this code? The code works, but I feel like I’m doing it wrong. Any help would be appreciated. Thanks.
package {
import flash.display.MovieClip;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.display.Loader;
import fl.transitions.*
import fl.transitions.easing.*
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
public class CarSS extends MovieClip {
protected var progressText:preloaderText = new preloaderText();
protected var progText:loadingText = new loadingText();
protected var ssc:slideControl = new slideControl();
protected var time:Timer;
protected var photo:Loader;
protected var xml:XML;
protected var currentImg:Number = 0;
protected var imgNum:Number = 1;
protected var xmlloader:URLLoader = new URLLoader();
protected const dur:Number = 5000
protected var myTween:Tween;
public function CarSS() {
//-----contructor-----//
progressText.height = 18;
progressText.width = 300;
progressText.x = 362;
progressText.y = 660;
progText.visible = true;
progText.x = (stage.stageWidth-progText.width)/2;
progText.y = 680;
progText.height = 18
ssc.y = 630;
ssc.x = (stage.stageWidth-ssc.width)/2;
ssc.playSS.addEventListener(MouseEvent.CLICK, startTimer);
ssc.stopSS.addEventListener(MouseEvent.CLICK, stopTimer);
ssc.playSS.visible = false;
xmlloader.load(new URLRequest("CarGallery.xml"));
xmlloader.addEventListener(Event.COMPLETE, processXML);
stage.addChild(progressText);
stage.addChild(progText);
stage.addChild(ssc);
function processXML(e:Event):void {
xml = new XML(e.target.data);
photo = new Loader();
photo.load(new URLRequest(xml.img[currentImg].@file));
photo.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, preload);
}
function preload(e:ProgressEvent):void {
var perc:int = (e.bytesLoaded*100/e.bytesTotal);
progressText.mtxt.text = String("LOADING " + perc + " %");
progText.ptxt.text = String("SHOWING "+imgNum + " of " + xml.img.length());
if (perc == 100) {
stage.addChild(photo);
photo.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);
progText.ptxt.text = String(xml.img[currentImg].@tite);
}
}
function imgLoaded(e:Event):void {
myTween = new Tween(photo,"alpha",Strong.easeOut,0,1,1.25,true)
myTween = new Tween(progText,"alpha",Strong.easeOut,0,1,1.25,true)
photo.x = (stage.stageWidth-photo.width)/2;
photo.y = 20;
}
time = new Timer(dur);
time.addEventListener(TimerEvent.TIMER, nextImg);
time.start();
function startTimer(e:MouseEvent):void {
time.start();
ssc.playSS.visible = false;
ssc.stopSS.visible = true;
}
function stopTimer(e:MouseEvent):void {
time.stop();
time.reset();
ssc.playSS.visible = true;
ssc.stopSS.visible = false;
}
function nextImg(e:Event):void {
myTween = new Tween(photo,"alpha",Strong.easeOut,1,0,1.25,true)
if (currentImg < xml.img.length() - 1) {
currentImg++;
imgNum++;
}
else {
currentImg = 0;
imgNum = 1;
}
if (imgNum == 1) {
myTween = new Tween(progressText,"alpha",Strong.easeOut,1,0,1.25,true)
myTween = new Tween(progText,"y",Strong.easeOut,680,660,1.25,true)
}
photo = new Loader();
photo.load(new URLRequest(xml.img[currentImg].@file));
photo.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, preload);
}
}
}
}
You can see this is action here
http://www.keithborders.com/CarSS.html