I have just started coding in AS3 and it would be really great to get some feedback from the experts; on my coding style, things I'm doing wrong, thing I can improve on, best practises, and so on... Also if you have some extra tips or tricks, that would be great.
Here's my first bit of AS3 code, took me 5 hours, puh:
package {
import flash.display.Sprite;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.*;
import flash.errors.*;
import flash.display.MovieClip;
import gs.*;
import flash.display.Loader;
import net.stevensacks.preloaders.CircleSlicePreloader;
public class FlatSelector extends MovieClip {
var preloader:CircleSlicePreloader = new CircleSlicePreloader();
var imageLoader:Loader = new Loader();
var globalXML:XML;
public function FlatSelector() {
stage.addEventListener(Event.ENTER_FRAME, init);
building.alpha = 0;
}
public function init(event:Event):void {
stage.removeEventListener(Event.ENTER_FRAME, init);
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, handleXML);
loader.load(new URLRequest('http://localhost/boligvelger/flats.xml'));
TweenLite.to(building, 2, {alpha:1});
TweenLite.to(building.flat, 2, {alpha:0.5, tint:0x00FF23});
//var myTween:TweenLite = TweenLite.to(mc, 1, {x:200});
//var myTween:TweenLite = new TweenLite(mc, 1, {x:200});
}
public function handleXML(e:Event):void {
var xml:XML = new XML(e.target.data);
globalXML = xml;
for (var i:Number = 0; i < xml.leiligheter.leilighet.length(); i++) {
var flatName = xml.leiligheter.leilighet[i].navn;
if(movieClipExists(building[flatName])) {
building[flatName].addEventListener(MouseEvent.MOUSE_UP, flatMouseClick);
building[flatName].addEventListener(MouseEvent.MOUSE_OVER, flatMouseOver);
building[flatName].addEventListener(MouseEvent.MOUSE_OUT, flatMouseOut);
building[flatName].alpha = 0;
TweenLite.to(building[flatName], 2, {alpha:0.5, tint:0x00FF23});
}
}
}
public function showInfoBox():void {
}
public function showFlat(flatName:String):void {
trace('flatName: '+flatName);
trace('flat shown');
var imageURL;
for (var i:Number = 0; i < globalXML.leiligheter.leilighet.length(); i++) {
if(globalXML.leiligheter.leilighet[i].navn == flatName) {
imageURL = globalXML.leiligheter.leilighet[i].plantegning;
}
}
trace(imageURL);
loadImage(imageURL);
}
public function showBuilding():void {
TweenLite.to(imageLoader, 0.5, {alpha:0, onComplete:function(){
removeChild(imageLoader);
}});
}
public function flatMouseClick(e:MouseEvent):void {
trace('clicked');
TweenLite.to(building, 0.7, {alpha:0, onComplete:showFlat(e.target.name)});
TweenLite.to(building, 2, {y:stage.stageHeight, overwrite:0});
}
public function flatMouseOver(e:MouseEvent):void {
TweenLite.to(building[e.target.name], 0.5, {tint:0x62ABFF});
building[e.target.name].buttonMode = true;
}
public function flatMouseOut(e:MouseEvent):void {
TweenLite.to(building[e.target.name], 0.5, {tint:0x00FF23});
}
public function showPreloader():void {
preloader.x = (stage.stageWidth-preloader.width)/2;
preloader.y = (stage.stageHeight-preloader.height)/2;
preloader.alpha = 0;
addChild(preloader);
TweenLite.to(preloader, 0.5, {alpha:1});
}
public function hidePreloader():void {
TweenLite.to(preloader, 0.5, {alpha:0, onComplete:function(){
removeChild(preloader);
}});
}
public function loadImage(url):void {
imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loaderProgressStatus);
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
var imageURL:URLRequest = new URLRequest(url);
imageLoader.load(imageURL);
showPreloader();
function loaderProgressStatus(e:ProgressEvent) {
//trace(e.bytesLoaded, e.bytesTotal);
}
function loaderComplete(e:Event) {
hidePreloader();
imageLoader.alpha = 0;
imageLoader.y = (stage.stageHeight-imageLoader.height)/2;
addChild(imageLoader);
TweenLite.to(imageLoader, 2, {alpha:1});
}
}
public function movieClipExists(mc:MovieClip):Boolean {
return mc != null && contains(mc);
}
}
}