PDA

View Full Version : AS 2.0 & AS 3.0: Same project - best practice


Flash Gordon
06-24-2007, 11:08 PM
Attached is the same project as an AS Flash 8 and as Flash 9 (AS 2.0 / AS 3.0).

Could I please get some feedback from you guys as to the very best OO practice for making this simple little application.

Thanks
:)


FLASH VERSION 8
fla:

stop();

import com.flash8.xml.*;
import com.flash8.loaders.*;

// load xml
var images:ImagesXML = new ImagesXML();
images.addEventListener(ImagesXML.LOAD, onLoadXML);
images.load("images.xml");

// init loading of images
var loader1:LoadImage = new LoadImage(pic1);
loader1.addEventListener(LoadImage.LOAD, onLoadImage);

var loader2:LoadImage = new LoadImage(pic2);
loader2.addEventListener(LoadImage.LOAD, onLoadImage);

var loader3:LoadImage = new LoadImage(pic3);
loader3.addEventListener(LoadImage.LOAD, onLoadImage);


function onLoadXML():Void
{
loader1.load(images._images[0][0]);
loader2.load(images._images[0][1]);
loader3.load(images._images[0][2]);
}

function onLoadImage(dispatch):Void
{
dispatch.target._xscale = dispatch.target._yscale = 30;
dispatch.target.onPress = startDrag;
dispatch.target.onRelease = stopDrag;
}


classes

import mx.utils.Delegate;
import mx.events.EventDispatcher;

class com.flash8.xml.ImagesXML
{
private var xml:XML;
private var images:Array;
private var urls:Array;
private var ids:Array;


public static var LOAD:String = "load";

// shut up compiler
public var addEventListener:Function;
public var dispatchEvent:Function;

public function ImagesXML()
{
xml = new XML();
xml.ignoreWhite = true;
xml.onLoad = Delegate.create(this, onLoad);

images = new Array();
urls = new Array();
ids = new Array();

EventDispatcher.initialize(this);
}

public function load(file:String):Void
{
xml.load(file);
}

private function onLoad(success:Boolean):Void
{
var nodes:Array = xml.firstChild.childNodes;

for (var i:Number=0; i<nodes.length; i++)
{
urls[i] = nodes[i].firstChild.nodeValue;
ids[i] = nodes[i].attributes.id
}

images.push(urls);
images.push(ids);

dispatchEvent( {type:LOAD, target:this} );
}


public function get _images():Array { return images; }
}


import mx.utils.Delegate;
import mx.events.EventDispatcher;

class com.flash8.loaders.LoadImage
{
private var target:MovieClip;
private var mcl:MovieClipLoader;

public static var LOAD:String = "load";

// shut up compiler
public var addEventListener:Function;
public var dispatchEvent:Function;


public function LoadImage(target:MovieClip)
{
this.target = target;
mcl = new MovieClipLoader();
mcl.addListener(this);

EventDispatcher.initialize(this);

}

public function load(url:String):Void
{
mcl.loadClip(url, target);
}

private function onLoadProgress(target:MovieClip, loaded:Number, total:Number):Void
{
//trace("loading");
}

private function onLoadInit(target:MovieClip):Void
{
dispatchEvent( {type:LOAD, target:target, scope:this} );
}
}


FLASH VERSION 9
fla:

stop();

import com.flash9.xml.*;
import com.flash9.loaders.*;

// load xml
var images:ImagesXML = new ImagesXML();
images.addEventListener(ImagesXML.LOAD, onLoadXML);
images.load("images.xml");

// init loading of images
var loader1:LoadImage = new LoadImage(pic1);
loader1.addEventListener(LoadImage.LOAD, onLoadImage);

var loader2:LoadImage = new LoadImage(pic2);
loader2.addEventListener(LoadImage.LOAD, onLoadImage);

var loader3:LoadImage = new LoadImage(pic3);
loader3.addEventListener(LoadImage.LOAD, onLoadImage);


function onLoadXML(event:Event):void
{
loader1.load( images.xml.url[0]);
loader2.load(images.xml.url[1]);
loader3.load(images.xml.url[2]);
}


classes:

package com.flash9.xml
{

import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.EventDispatcher;

public class ImagesXML extends EventDispatcher
{

protected var loader:URLLoader;

public static const LOAD:String = "load";

public function ImagesXML()
{
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, onComplete);
}

public function load(file:String):void
{
loader.load( new URLRequest(file) );
}

protected function onComplete(event:Event):void
{
dispatchEvent( new Event(LOAD) );
}


public function get xml():XML { return new XML(loader.data); }
}
}


package com.flash9.loaders
{
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.Sprite;
import flash.events.MouseEvent;

public class LoadImage extends Sprite
{
protected var loader:Loader;
protected var target:*; // what is the datatype for a movieclip on the stage?

public static const LOAD:String = "LOAD";


public function LoadImage(_target:*)
{
target = _target;
target.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
target.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);

loader = new Loader();
addChild(loader);
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, onLoadInit);

}

public function load(url:String):void
{
loader.load( new URLRequest(url) );
}


protected function onLoadInit(event:Event):void
{
this.scaleX = this.scaleY = .30;
target.addChild(this);
dispatchEvent( new Event(LOAD) );
}

protected function onMouseUp(event:MouseEvent):void
{
target.stopDrag();
}

protected function onMouseDown(event:MouseEvent):void
{
target.startDrag();
}

}
}


XML FILE TO LOAD

<?xml version="1.0" encoding="iso-8859-1"?>
<images>
<url id="1">http://images.askmen.com/galleries/actress/courtney-cox/pictures/courtney-cox-picture-1.jpg</url>
<url id="3">http://spoilerbuzz.com/wp-content/uploads/2006/12/courtneycox.jpg</url>
<url id="2">http://i26.photobucket.com/albums/c139/denee34/courteney-cox-1280x960-16546.jpg</url>
</images>

hangalot
06-25-2007, 02:00 PM
the type is sprite or movieclip depending on what it is.
never put code on a timeline, that is a bad practise. flash 9 you can assign a class to your stage, a much better approach.
why do you create your loaders immediatly. if your xml changes you are fooked. wait till your xml loads and then loop through it and create what you need.
personally i do not even think you need the loadimage class.

you realise with your images you will need a server(s) policy file(s)

Flash Gordon
07-02-2007, 06:36 AM
thanks hangalot!
:)

I made a new one, but I still have code on the timeline :(