how to create custom class?
A:
main.as
package map{ import flash.display.; import flash.events.; import flash.net.; import flash.xml.; import fl.controls.ComboBox; import fl.data.DataProvider;
import map.xmlload.XMLDispatcher;
import map.imageload.imageLoad;
public class Main extends Sprite {
private var xmlDespatch:XMLDispatcher;
private var xmlPath:String = "xml/DynamicMap.xml";
private var CountryCombo:ComboBox;
private var RegionCombo:ComboBox;
private var RegionItams:DataProvider;
private var countryMap:imageLoad;
private var countryFlag:imageLoad;
public function Main() {
xmlDespatch = XMLDispatcher.getinstance();;
xmlDespatch.Dispatcher(xmlPath);
xmlDespatch.addEventListener("Finish0", XMLGUILoad);
stage.displayState = StageDisplayState.FULL_SCREEN;
stage.scaleMode = StageScaleMode.NO_SCALE;
}
public function XMLGUILoad(event:Event) {
setupCountryComboBox();
setupRegionComboBox();
}
public function setupCountryComboBox() {
CountryCombo=new ComboBox();
CountryCombo.x = 10;
CountryCombo.y = 10;
CountryCombo.setSize(150, 22);
CountryCombo.prompt = "Select Country";
for(var c:int=0; c<xmlDespatch.getCountryList().length; c++) {
CountryCombo.addItem( { label: xmlDespatch.getCountryList()[c][0], data:c } );
}
CountryCombo.addEventListener(Event.CHANGE, loadCountry)
addChild(CountryCombo);
}
public function loadCountry(e:Event):void {
var countryIndex:int = CountryCombo.selectedIndex;
if (countryMap != null) {
removeChild(countryMap);
}
var countryMapPath:String = xmlDespatch.getCountryList()[countryIndex][2];
countryMap = new imageLoad(countryMapPath, countryIndex, 600, 325, 0, 75);
addChild(countryMap);
if (countryFlag != null) {
removeChild(countryFlag);
}
var countryFlagPath:String = xmlDespatch.getCountryList()[countryIndex][1];
countryFlag = new imageLoad(countryFlagPath, countryIndex, 43, 29, 540, 10);
addChild(countryFlag);
var countryName:String = xmlDespatch.getCountryList()[countryIndex][0];
xmlDespatch.setRegionList(countryName);
RegionItams = new DataProvider();
for(var r:int=0; r<xmlDespatch.getRegionList().length; r++) {
RegionItams.addItem({label: xmlDespatch.getRegionList()[r][0]});
}
RegionCombo.dataProvider = RegionItams;
}
public function setupRegionComboBox() {
RegionCombo=new ComboBox();
RegionCombo.x = 175;
RegionCombo.y = 10;
RegionCombo.setSize(175, 22);
RegionCombo.prompt = "Select Region";
RegionCombo.addEventListener(Event.CHANGE, loadRegion)
addChild(RegionCombo);
}
public function loadRegion(e:Event):void {
var regionIndex:int = RegionCombo.selectedIndex;
if (countryMap != null) {
removeChild(countryMap);
}
var regionMapPath:String = xmlDespatch.getRegionList()[regionIndex][1];
countryMap = new imageLoad(regionMapPath, regionIndex, 600, 325, 0, 75);
addChild(countryMap);
}
}
}
CustomEvent.as
package map.xmlload{ import flash.events.Event;
public class CustomEvent extends Event
{
public var data:*;
public static const ONLOADED:String = "OnLoaded";
public function CustomEvent(type:String, data:*)
{
this.data= data;
super(type);
}
}
}
XMLDispatcher.as
package map.xmlload { import flash.display.; import flash.events.; import flash.xml.*; import map.xmlload.CustomEvent; import map.xmlload.XMLLoaderUtil;
public class XMLDispatcher extends Sprite {
private var mapXML:XMLLoaderUtil;
private var countryList:Array;
private var regionList:Array;
private var loadedXML:XML;
private static var instance:XMLDispatcher;
public static function getinstance() {
if (instance != null)
return instance;
else {
instance = new XMLDispatcher();
return instance;
}
}
public function XMLDispatcher() {
}
public function Dispatcher(xmlPath:String) {
mapXML = new XMLLoaderUtil();
mapXML.addEventListener(CustomEvent.ONLOADED, retXMLLoader);
mapXML.load(xmlPath);
}
public function retXMLLoader(event:CustomEvent) {
//var loadedXML:XML;
loadedXML = event.data as XML;
countryList = new Array();
var i:int = 0;
for each(var countryElement:XML in loadedXML.country){
countryList.push(new Array(3));
countryList[i][0] = countryElement.name;
countryList[i][1] = countryElement.flag;
countryList[i][2] = countryElement.map;
i++;
}
dispatchEvent(new Event("Finish0"));
}
public function setRegionList(countryName:String){
regionList = new Array();
var n:int = 0;
for each(var regionElement:XML in loadedXML.elements()){
if (regionElement.name == countryName) {
for each (var subelement:XML in regionElement.region) {
trace();
regionList.push(new Array(2));
regionList[n][0] = subelement.@name;
regionList[n][1] = subelement.@image;
n++;
}
}
}
}
public function getRegionList():Array{
return regionList;
}
public function getCountryList():Array {
return countryList;
}
}
}
XMLLoaderUtil.as
package map.xmlload{ import flash.events.Event; import flash.events.EventDispatcher; import flash.events.IEventDispatcher; import flash.net.URLLoader; import flash.net.URLRequest; import map.xmlload.CustomEvent;
public class XMLLoaderUtil extends EventDispatcher
{
private var strFileURL:URLRequest;
private var xmlObj:XML;
private var urlLoader:URLLoader;
public function XMLLoaderUtil(target:IEventDispatcher=null)
{
super(target);
}
public function load(strFileName:String):void{
strFileURL = new URLRequest(strFileName);
urlLoader = new URLLoader();
urlLoader.load(strFileURL);
urlLoader.addEventListener(Event.COMPLETE,onXMLLoaded);
}
private function onXMLLoaded(evt:Event):void {
urlLoader.removeEventListener(Event.COMPLETE, onXMLLoaded);
var dataLoader:URLLoader = evt.target as URLLoader;
xmlObj = new XML(dataLoader.data);
dispatchEvent(new CustomEvent(CustomEvent.ONLOADED,xmlObj));
xmlObj = null;
dataLoader = null;
urlLoader = null;
}
}
}
imageload.as
package map.imageload{
import caurina.transitions.*;
import flash.filters.DropShadowFilter;
import flash.net.URLRequest;
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.LoaderInfo;
import fl.controls.ProgressBar;
import fl.controls.ProgressBarMode;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
import flash.text.AntiAliasType;
import flash.text.Font;
import flash.events.ProgressEvent;
import flash.events.Event;
public class imageLoad extends Sprite {
private var img:String;
private var img_loader:Loader;
private var imgWidth:uint;
private var imgHeight:uint;
private var loadProgress_txt:TextField;
private var format:TextFormat;
private var font:Font;
private var pBar:ProgressBar;
private var X:int;
private var Y:int;
function imageLoad(img_image:String, img_id:uint, img_width:uint, img_height:uint, X:int, Y:int):void {
this.name = "image"+img_id;
this.img = img_image;
this.imgWidth = img_width;
this.imgHeight = img_height;
this.X = X;
this.Y = Y;
loadImage();
//text format
format = new TextFormat();
format.font = "Arial"
format.color = 0xFFFFFF;
format.size = 9;
//progress text
loadProgress_txt = new TextField();
loadProgress_txt.x = imgWidth/2 - loadProgress_txt.width/2;
loadProgress_txt.y = imgHeight/2 + 5;
loadProgress_txt.embedFonts = false;
loadProgress_txt.selectable = false;
loadProgress_txt.autoSize = TextFieldAutoSize.CENTER;
loadProgress_txt.defaultTextFormat = format;
}
//load image
private function loadImage():void {
// progress bar
pBar = new ProgressBar();
pBar.mode = ProgressBarMode.MANUAL;
pBar.y = imgHeight/2 - pBar.height/2;
pBar.x = imgWidth/2 - pBar.width/2;
//addChild(pBar);
img_loader = new Loader();
img_loader.load(new URLRequest(img));
img_loader.alpha = 0;
img_loader.contentLoaderInfo.addEventListener(Event.OPEN,showPreloader);
img_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,showProgress);
img_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,showLoadResult);
img_loader.mouseEnabled = false;
img_loader.x = X;
img_loader.y = Y;
addChild(img_loader);
}
//preloader events
private function showPreloader(evt:Event):void {
//addChild(loadProgress_txt);
}
private function showProgress(evt:ProgressEvent):void {
var percentLoaded:Number = evt.bytesLoaded/evt.bytesTotal;
loadProgress_txt.text = Math.round(percentLoaded * 100) + "%";
pBar.setProgress(evt.bytesLoaded, evt.bytesTotal);
}
//once image has loaded show
private function showLoadResult(evt:Event):void {
//removeChild(loadProgress_txt);
//removeChild(pBar);
Tweener.addTween(img_loader, {alpha:1, time:1, transition:"easeOut"});
img_loader.removeEventListener(ProgressEvent.PROGRESS, showProgress);
img_loader.removeEventListener(Event.COMPLETE, showLoadResult);
}
}
}
Mark
2009-12-12 03:04:49