Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

Go Back   ActionScript.org Forums > General > Best Practices

Reply
 
Thread Tools Rate Thread Display Modes
Old 07-02-2007, 06:44 AM   #1
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default Flash AS 3.0 OO gallery - best practices

Hi,

Attached are the files for a simple slideshow/gallery presentation. I'm looking for comments for best practice in OO or convention in AS 3.0. I understand I'm not checking for loading of images in such, as that isn't my focus right now.

Also, is it better to have callbacks like I'm doing from the classes and then make new classes as needed? Or is there a better way OO way of organizing the application?

Thanks




scripts:

FLA
ActionScript Code:
stop(); import com.flash9.xml.*; import com.flash9.load.*; import com.flash9.engines.*; // references to classes var slideshow:SlideShow; //load xml var info:ImagesXML = new ImagesXML(); info.addEventListener(Event.COMPLETE, onXML); info.load("banner.xml"); function onXML(event:Event):void {     // add text     company_txt.text = info.company;     industry_txt.text = info.industry;     url_txt.text = info.url;         // load images     for (var i:uint=0; i<info.images.length(); i++)     {         this["image"+i] = new LoadImage(container);         this["image"+i].load( info.images[i] );     }     container.setChildIndex(this["image0"], container.numChildren-1);         // start slide show     slideshow = new SlideShow(container);     slideshow.start(); }

ImagesXML
ActionScript Code:
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;         protected var xml:XML;                         public function ImagesXML()         {             loader = new URLLoader();             loader.dataFormat = URLLoaderDataFormat.TEXT;             loader.addEventListener(Event.COMPLETE, onLoad);         }                 public function load(file:String):void         {             loader.load( new URLRequest(file) );         }                 protected function onLoad(event:Event):void         {             xml = XML(loader.data);             dispatchEvent( new Event(Event.COMPLETE) );         }                 public function get time():Number { return parseFloat(xml.@time) }         public function get company():String { return xml.set.client; }         public function get industry():String { return xml.set.industry; }         public function get url():String { return xml.set.url; }         public function get link():String { return xml.set.url.@link; }         public function get images():XMLList { return xml.pictures.image; }     } }

LoadImage
ActionScript Code:
package com.flash9.load {     import flash.events.Event;     import flash.events.EventDispatcher;     import flash.net.URLRequest;     import flash.display.Sprite;     import flash.display.Loader;     import flash.display.LoaderInfo;     import flash.display.DisplayObjectContainer;     import flash.events.MouseEvent;         public class LoadImage extends Sprite     {         protected var _loader:Loader;         protected var _info:LoaderInfo;                 public function LoadImage(target:DisplayObjectContainer=null)         {             _loader = new Loader();             addChild(_loader);                         _info = LoaderInfo(_loader.contentLoaderInfo);             _info.addEventListener(Event.COMPLETE, onLoadInit);                         if (target != null) target.addChild(this);         }                 public function load(file:String):void         {             _loader.load( new URLRequest(file) );         }                 protected function onLoadInit(event:Event):void         {             dispatchEvent( new Event(Event.COMPLETE) );         }     } }

SlideShow
ActionScript Code:
package com.flash9.engines {         import fl.transitions.Tween;     import fl.transitions.easing.*;     import flash.display.DisplayObjectContainer;     import flash.utils.Timer;     import flash.events.TimerEvent;         public class SlideShow     {         protected var container:DisplayObjectContainer;         protected var delay:Number;         protected var clips:Array;         protected var nowShowing:uint = 0;                 protected var timer:Timer;         protected var tween:Tween;                 public function SlideShow(container:DisplayObjectContainer, delay:Number=3000)         {             this.container  = container;             this.delay   = delay;                         clips = findChildren(container);                         timer = new Timer(delay);             timer.addEventListener(TimerEvent.TIMER, toLoop);         }                 private function findChildren(container:DisplayObjectContainer):Array         {             var temp:Array = new Array();             for (var i:uint=0; i<container.numChildren; i++)                 temp.push( container.getChildAt(i) );                         return temp;         }                 public function start():void         {             timer.start();         }                 protected function toLoop(event:TimerEvent):void         {             container.setChildIndex(clips[nowShowing], container.numChildren-1);             tween = new Tween(clips[nowShowing], "alpha", None.easeNone, 0, 1, 1.0, true);                         nowShowing = ++nowShowing % clips.length;         }     } }

XML
HTML Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<banner time="5.5">

	<set>
		<client>Client Name: Google</client>
		<industry>Expertise: Searching</industry>
		<url link="http://www.google.com">www.google.com</url>
	</set>
	
	<pictures>
		<image>image1.jpg</image>
		<image>image2.jpg</image>
		<image>image3.jpg</image>
		<image>image4.jpg</image>
	</pictures>

</banner>
Attached Files
File Type: zip banner - flash9.zip (118.2 KB, 1505 views)
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman

Last edited by Flash Gordon; 07-02-2007 at 07:38 AM..
Flash Gordon is offline   Reply With Quote
Old 07-03-2007, 11:34 AM   #2
panel
AS3
 
panel's Avatar
 
Join Date: Mar 2007
Location: Warsaw
Posts: 1,761
Send a message via Skype™ to panel
Default

I am not saying that my way is better or something. Just few minor advices.

1. I am naming class variables starting with underscore becouse when you are passing variable to method you don;t have to use this keyword.
ActionScript Code:
this.container  = container; //your way _container  = container; //my way

2. When parsing xml I am using try, catch. It's easy way to deal with incorrect xml especially when you edit it xml manually there is chance to make mistake.
ActionScript Code:
function onXML(evt:Event) {     try     {         _xml= new XML(evt.target.data);         processXML();     }     catch ( evt:TypeError )     {         trace("Could not parse text into XML");         trace(evt.message);     } }

I would also split xml. Create one for images and one for 'banner info'.
Becouse in future you may add some parameters to image (title, navigateUrl) xml won't be so easy to read (in my opinion using one xml file for 2 things isn't good practice )

<Image>
<Title>Eye</Title>
<Url>Image3.png</Url>
<NavigateUrl>http://www.actionscript.org</NavigateUrl>
</Image>

3. In my photo gallery I am using two classes PhotoList and Photo.
This way I can create new photo object in PhotoList and manage add/remove to display list them when needed or Imagine situation when you have separate link for eqach photo. You could easly get current photo link.
Remember that processXML function runt from try block onXML, so if something is wrong with xml you will know.

ActionScript Code:
private function processXML():void     {         for each (var property:XML in _xml.Photo)         {             var photo:Photo = new Photo(property.Title, property.ImageUrl, property.NavigateUrl);             photo.addEventListener("imageLoadComplete", onImageLoad);             _photos.push(photo); //now all photos are in array if you will remember currentPhotoIndex you could easly get to this photo properties         }        }

4. Consider using document class
panel is offline   Reply With Quote
Old 07-10-2007, 12:22 AM   #3
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default

cool thanks bro!

It appear private/protected vars in a class begin with underscore so yea that is a good idea. and I used a doc class in my most recenty project.

Cheers
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman
Flash Gordon is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Developing a flash site, Part1: Some thoughts Bloom22 Other Flash General Questions 6 09-29-2006 03:10 AM
RESUME: Senior Flash Designer / Developer - San Francisco OSS Projects and Positions 0 08-16-2006 08:29 PM
Flash Encrypt 1.2 Released. SIntrix General Chat 14 04-21-2005 10:02 PM
scrollBar on Flash Exchange cdeg Components 5 05-03-2004 08:13 PM


All times are GMT. The time now is 07:36 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.