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);
}
}
}
Full disclosure: I am an anal and pedantic reviewer so don't take it personally. In general your code is good.
ENTER_FRAME
delay before init
? Perhaps you have a good reason but I don't know of one. You should be able to call init
directly from your constructor.TweenLite
) for animations. +1for (var i:Number
<-- should use an int
for a integer counter.xml.leiligheter.leilighet.length()
<-- consider caching this in var len:int = ...
var flatName =
<-- don't be lazy and forget your types.xml.leiligheter.leilighet[i].navn
<-- digging pretty deep there. You might rather do:var children:XMLList = xml.leiligheter.leilighet; for each(var node:XML in children) { var flatName:String = String(node.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
<-- empty function? Code rot, get rid of it.trace('flatName: '+flatName);
<-- I make a habit of removing traces. There are several in your code. Keep them in there only as long as you absolutely need them then get rid of them.showFlat
more traces and poor XML handling.TweenLite.to(imageLoader, 0.5, {alpha:0, onComplete:function(){
<-- I know it is easy to write an anonymous function here but it is better practise IMHO to make a member function. I tend only to use anonymous functions when I want a closure.building[e.target.name].buttonMode = true;
you should set this only once when you are constructing the buildings (during handleXML
) and not everytime it is moused overloadImage
you have two named functions defined inside named loaderProgressStatus
and loaderComplete
. These should be defined at the class level and since loaderProgressStatus
is empty -- don't define it and don't assign it as a listener (code-cruft)*
) in your import statements. It may be a little more work to explicitly declare each import but it minimizes the chances of a possible name-clash.Like I said - I am very picky. Your code style is consistent which shows good potential. There are only a few major types of error:
trace
s should only exist during debugging and should disappear ASAP.int
over Number
)