Hi,
I making custom Event class, I did it, it works but have some problems.
I make to show some photos in the swf
I import my class album, and this class, call another class loadImages. loadImages, put photos in Scene, I want to make when an image load, to dispatchEvent, and album class to get the event. I make listeners to get the event, but they didn’t.
Here is the all. http://theflashblog-bg.com/resources/Event.zip
here is code in the main timeline
[AS]import classes.album;
var MyPhotoAlbum:album = new album();
MyPhotoAlbum.loadXML(“photoflow.xml”);
MyPhotoAlbum.name =“myALb”;
addChild(MyPhotoAlbum);[/AS]
my event class
[AS]package classes.events_v3 {
import flash.events.*;
public class flowEvent extends Event {
public static const PLC:String = "photo load complited";
public function flowEvent() {
super(type, true);
}
}
}[/AS]
my album class
[AS]package classes{
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.MovieClip;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.xml.XMLDocument;
import classes.loadImages;
//import classes.events_v3.flowEventDispatcher;
import classes.events_v3.flowEvent;
////////
public class album extends MovieClip {
//Property; specifies the number ot photos
public var allPhotos:uint = 0;
//Property; boolean value indicatin whether all of the images have loaded or not
public var allLoaded:Boolean = false;
//Property; the path to the folder containing external images.
public var folderPath:String = "images/";
//Property; the max height of the photos.
public var photoHeight:Number = 200;
//Property; an array containing each photo's object data.
public var photosData:Array = new Array();
//Property; the max width of the photos.
public var photoWidth:Number = 200;
//The path and name of the XML file to load. The XML file contains the list of images to load.
public var xmlPath:String = "";
//Property; the type of scaling used. Options are "showAll", "scaleToFit" and "noScale".
public var scaleMode:String = "noScale";
//Property: margin top, right, bottom, left
private var margin:Array = new Array(10,10,10,10);
public var holderColor:uint = 0xffffff;
//The color of the border that appears around each image while it is loading.
public var holderBorderColor:uint = 0xe4e4e4;
public function album() {
}
//Method; loads the specified XML file.
public function loadXML(XMLurl:String) {
xmlPath = XMLurl;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onLoadXML);
loader.load(new URLRequest(XMLurl));
}
public function showForm():void {
var i:uint;
for (i=0; i<allPhotos; i++) {
var temp:loadImages = new loadImages(photosData*[".name"],photoWidth ,photoHeight, photosData*[".url"], scaleMode, holderColor, holderBorderColor,margin);
temp.x = i*240;
temp.name = "photo"+String(i)+"_mc";
//addEventListener(flowEvent.PLC, photoHaveBeenLoad);
addChild(temp);
}
}
public function photoHaveBeenLoad(info:Event) {
trace(info.target);
}
///from loadXML(), when XMLLoaded;
private function onLoadXML(info:Event) {
var photoDataXML:XMLDocument = new XMLDocument();
photoDataXML.ignoreWhite = true;
photoDataXML.parseXML(info.target.data);
//Вземане на allPhotos, folderPath, photosData
allPhotos = photoDataXML.firstChild.childNodes.length;
folderPath = photoDataXML.firstChild.attributes["path"];
var i:uint;
for (i=0; i<allPhotos; i++) {
photosData.push({".url":String(folderPath+photoDataXML.firstChild.childNodes*.attributes["url"]), ".name":String(photoDataXML.firstChild.childNodes*.attributes["name"]),".desc":String(photoDataXML.firstChild.childNodes*.firstChild)});
}
showForm();
}
}
}[/AS]
my loadImageClass
[AS]package classes{
import flash.display.Shape;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
import flash.text.TextField;
import classes.events_v3.flowEvent;
import classes.album;
public class loadImages extends MovieClip {
//prop:Name of the picture
private var Pcaption:String = "";
//prop:Photo Width to become
private var PW:Number;
//prop:Photo Height to become
private var PH:Number;
//prop:Photo Width
private var PWreal:Number;
//prop:Photo Height
private var PHreal:Number;
//prop:url to the photo
private var Purl:String = "";
//prop: scaleMod
private var PScaleMode:String = "";
//prop: Photo background color
private var PBGColor:uint = 0xffffff;
//prop: Photo border color
private var PBColor:uint = 0xffffff;
//prop: margin top, right, bottom, left
private var margin:Array = new Array(0,0,0,0);
private var _loaderImage:Bitmap = new Bitmap();
public var loader:Loader = new Loader();
public var resizeStatus = true;
public var keepRatio = true;
//percent from photo loading
private var onLoadingPrecent:uint;
///the Ruler
public function loadImages(_caption:String, W:uint, H:uint, url:String, scaleMode:String, bgColor:uint, bgBorderColor:uint, photoMargin:Array):void {
///init Vars
Pcaption = _caption;
PW = W;
PH = H;
Purl = url;
PScaleMode = scaleMode;
PBGColor = bgColor;
PBColor = bgBorderColor;
margin = photoMargin;
this.bg_mc.addChild(drowShape(PW, PH, PBGColor, PBColor));
loadImage(Purl);
}
public function drowShape(W, H, bgColor, borderColor):Shape {
var shape:Shape = new Shape();
shape.graphics.beginFill(bgColor);
shape.graphics.lineStyle(1, borderColor);
shape.graphics.drawRect(0, 0, W, H);
shape.graphics.endFill();
return shape;
}
///Lets all begin
private function loadImage(url:String) {
setPreloader();
loader.load(new URLRequest(url));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoading);
this.photo_mc.addChildAt(loader ,0);
}
///Pictures is Loaded
private function onLoadComplete(info:Event):void {
hidePreloader();
var mm:Loader = Loader(info.target.loader);
var image:Bitmap = Bitmap(mm.content);
//set postion
setPostion(image);
//optimizations
image = optimiseImage(image);
this.photo_mc.addChild(image);
///dispatch
var ff:album = new album();
addEventListener(flowEvent.PLC, ff.photoHaveBeenLoad);
dispatchEvent(new Event(flowEvent.PLC));
}
///return loading percent in MCs TextField
private function onLoading(info:ProgressEvent):void {
onLoadingPrecent = int(100*info.bytesLoaded/info.bytesTotal);
this.preloader_mc.persent_txt.text = String(onLoadingPrecent)+" %";
}
///hide preloader after image load
private function hidePreloader():void {
this.preloader_mc.visible = false;
}
///set loaded image postions
private function setPostion(image:Bitmap) {
PWreal = int(image.width);
PHreal = int(image.height);
switch (PScaleMode) {
case "showAll" :
var NewW:Number = 0;
var NewH:Number = 0;
var propToWidth:Number = (PW -margin[1]-margin[3])/PWreal;
var propToHeight:Number = (PH -margin[0]-margin[2])/PHreal;
if (propToWidth>=propToHeight) {
NewW = PWreal*propToHeight;
NewH = PHreal*propToHeight;
} else {
NewW = PWreal*propToWidth;
NewH = PHreal*propToWidth;
}
image.width = NewW;
image.height = NewH;
image.x = margin[3];
trace(PH+"x"+NewH);
image.y = PH - NewH -margin[0];
PWreal = NewW;
PHreal = NewH;
break;
case "scaleToFit" :
image.width = PW-margin[1]-margin[3];
image.height = PH-margin[0]-margin[2];
image.x = margin[3];
image.y = margin[0];
break;
case "noScale" :
image.width = PWreal - margin[1] - margin[3];
image.height = PHreal - margin[0] - margin[2];
image.x = (PW - image.width)/2;
image.y = (PH - image.height)/2;
break;
default :
break;
}
}
///set loaded image optimization
private function optimiseImage(image:Bitmap):Bitmap {
image.smoothing = true;
return image;
}
///set preloader
private function setPreloader():void {
this.preloader_mc.x = (PW-this.preloader_mc.width)/2;
this.preloader_mc.y = (PH-this.preloader_mc.height)/2;
this.caption_txt.x = 10;
this.caption_txt.y = this.preloader_mc.y+this.preloader_mc.height+10;
this.caption_txt.width = (PW -20);
this.caption_txt.text = Pcaption;
}
////////////get and set functions
public function get loaderImage():Bitmap {
return _loaderImage;
}
}
}[/AS]
so u can see for now I’m getting the event with this lines in loadImages.as
[AS]var ff:album = new album();
addEventListener(flowEvent.PLC, ff.photoHaveBeenLoad);
dispatchEvent(new Event(flowEvent.PLC));[/AS]
but I want to add dispatchEvent in albun like so
[AS]
temp.name = “photo”+String(i)+"_mc";
//addEventListener(flowEvent.PLC, photoHaveBeenLoad);
addChild(temp);[/AS]
but if I write addEventListener, didn’t get the event. What to do?