Sunday 17 April 2011

Actionscript 3.0 : The Foundations For Your Application

This example below has the initial structure to start coding your application. It does two things, the first is that is sets the stage properties and creates a resize handler in case  you require it and the second is that the application waits for the stage to initiate and then you can begin creating your elements, this ensures that the stage is ready before you start calling stage or stage methods. Calling the stage before the stage is ready will through errors.

package 
{
import flash.events.Event;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.display.Sprite;
        [SWF(backgroundColor="#2d3845", frameRate="31", width="780", height="570")]
public class Main extends Sprite
{
public function Main()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.addEventListener(Event.RESIZE, resizeHandler);
if (stage)
{
init ();
} else 
{
addEventListener (Event.ADDED_TO_STAGE, init);
}
}
private function init (evt:Event = null):void
{
removeEventListener (Event.ADDED_TO_STAGE, init);
}
private function resizeHandler(event : Event) : void {
}
}
}

Checking Flash Vars


In all my application a create a function to check for flash vars because you can use them to configure your application. Below in the init function I created a call to a 'checkFlashVars'. 


private function init (evt:Event = null):void
{
//stop the listener
removeEventListener (Event.ADDED_TO_STAGE, init);
//check flash vars
checkFlashVars();
//load data xml
loadDataXML();
}
In This function I created checks for certain flash vars that can help configure the application. The example below checks for 'dataUrl' and 'dataFileName' flashvars, I check them through a Class that I created (I will explain later on) which has help methods to access flashvars. 


private function checkFlashVars() : void 
{
if(flashVars.getFlashVar("dataUrl"))
{
appData.addData("dataUrl", flashVars.getFlashVar("dataUrl"));
}else{
appData.addData("dataUrl", appData.DEFAULT_DATA_URL);
}
if(flashVars.getFlashVar("dataFileName"))
{
appData.addData("dataFileName", flashVars.getFlashVar("dataFileName"));
}else{
appData.addData("dataFileName", appData.DEFAULT_DATA_FILENAME);
}
}
FlashVars Class
This Class stores the flashvars in an object and then using a helper method it allows easy access to the flashvars.


1.create a instance in the Main Class: 
private var flashVars : FlashVars;


2. Initiate it and store the flashvars:

flashVars = new FlashVars();
flashVars.data= LoaderInfo(this.root.loaderInfo).parameters;



3.The FlashVars Class



package com.data {
/**
* @author Fahim
*/
public class FlashVars {
private var _data : Object;


public function get data() : Object {
return _data;
}


public function set data(value : Object) : void {
_data = value;
}
public function getFlashVar(value:String):String{
return _data[value];
}
}
}





No comments:

Post a Comment