PDA

View Full Version : Fighting loader and custom event dispatch


petzi-baer
07-09-2009, 03:08 AM
Hello, I am fighting with the following code. The problem starts with not being able to dispatch the "UnloadMe". I got this code from another forum thread, but it doesn't work for me. How can I debug this type of problem?

thanks Peter

package code{

import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.ProgressEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;

public class Main extends MovieClip {

public var canvas:Sprite;
public var swfLoader:Loader;
public var swfContainer:MovieClip;
private var isLoaded:Boolean=false;
private var isImgLoaded:Boolean=false;
private static const YELLOW:Number = 0xFFFF00;
private static const BLUE:Number = 0x0000FF;
private static const GREEN:Number = 0x00FF00;
private static const RED:Number = 0xFF0000;
private static const BLACK:Number = 0x000000;

public function Main() {


canvas=new Sprite();
swfLoader=new Loader();
swfContainer=new MovieClip();
drawCanvas();
addChild(canvas);
controllingSwfs("Load Movie");
cl_Btn.addEventListener(MouseEvent.CLICK, drawCanvasClick);

}
function controllingSwfs(WhatToLoad:String):void {

// declare the load complete event handler
// load the external SWF
trace(WhatToLoad);

loadSwf('videoDemo_final.swf');
isLoaded=true;


}
function loaderCompleteHandler(evt:Event):void {
// cast swfContainer as a MovieClip
swfContainer=swfLoader.content as MovieClip;
// add the external SWF to the display list after it has completed loading
swfContainer.x=10;
swfContainer.y=10;
var rect:Shape = new Shape();
rect.graphics.beginFill(0);
rect.graphics.drawRect( 30, 30, 349, 299);
rect.graphics.endFill();
canvas.addChild(rect);

swfContainer.mask=rect;
canvas.addChild(swfContainer);
trace("in complete");
swfContainer.clear_Btn = this.cl_Btn;
trace(swfContainer.clear_Btn, cl_Btn);
swfLoader.contentLoaderInfo.removeEventListener(Ev ent.COMPLETE,loaderCompleteHandler,false);

}
function drawCanvas():void {
canvas.graphics.clear();
canvas.graphics.beginFill(0xFFFFF0);
canvas.graphics.lineStyle(2, BLACK);
canvas.graphics.drawRect( 30, 30, 350, 300);
canvas.graphics.endFill();
}
function drawCanvasClick(event:MouseEvent):void {
if (isLoaded==true) {
trace("blink blink");
dispatchEvent(new Event("UnloadMe"));

// swfLoader.unload();
isLoaded=false;
}
drawCanvas();

}
function loadSwf(Url:String):void {
trace("in load swf");

swfLoader.contentLoaderInfo.addEventListener(Event .COMPLETE,loaderCompleteHandler);
swfLoader.addEventListener("UnloadMe", unloadFunction);
swfLoader.load(new URLRequest(Url));

}
function unloadFunction(event:Event):void {
// event.target is the loader reference.. so cast it as Loader
trace("blink blink blink");
canvas.removeChild(swfContainer);
Loader(event.currentTarget).unload();
swfLoader.removeEventListener("UnloadMe", unloadFunction, false);

}
}
}