Actionscript Code:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
public class DocClass extends MovieClip {
private var car:Car;
public function DocClass() {
// constructor code
car = new Car();
car.addEventListener(Event.COMPLETE,completed);
addChild(car);
stage.addEventListener(MouseEvent.CLICK,clicked);// this is for test
}
private function clicked(evt:MouseEvent):void {
removeChild(car);
}
private function completed(evt:Event):void {
if (evt.target.types=="addedToStage") {
trace(evt.target.types);
evt.target.removeEventListener(Event.ADDED_TO_STAGE,evt.target.dispatchEvents);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
} else if (evt.target.types=="removedFromStage") {
trace(evt.target.types);
evt.target.removeEventListener(Event.REMOVED_FROM_STAGE,evt.target.dispatchEvents);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
}
}
public function keyPressed(event:KeyboardEvent):void {
if (event.keyCode == 65) {
car.x -= 5;
}
if (event.keyCode == 83) {
car.x += 5;
}
}
}
}
Actionscript Code:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.EventDispatcher;
public class Car extends MovieClip {
public var types:String;
public function Car() {
// constructor code
x = 200;
y = 300;
addEventListener(Event.ADDED_TO_STAGE,dispatchEvents);
addEventListener(Event.REMOVED_FROM_STAGE,dispatchEvents);
}
public function dispatchEvents(evt:Event):void {
types=evt.type;
dispatchEvent(new Event(Event.COMPLETE));
}
}
}