Skip to content

Instantly share code, notes, and snippets.

@darylducharme
Created March 11, 2012 17:52
Show Gist options
  • Save darylducharme/2017407 to your computer and use it in GitHub Desktop.
Save darylducharme/2017407 to your computer and use it in GitHub Desktop.
Testing why, when, and how of overriding AS3's event.clone() method
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
private var _child1:Sprite;
private var _child2:Sprite;
private var _child3:Sprite;
public function Main()
{
_child1 = new Sprite();
_child1.name = "child1";
_child1.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage, false, 0, false);
_child1.addEventListener("Added: child3", onChild3Added, false, 0, true);
_child2 = new Sprite();
_child2.name = "child2";
_child2.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage, false, 0, false);
_child2.addEventListener("Added: child3", onChild3Added, false, 0, true);
_child3 = new Sprite();
_child3.name = "child3";
_child3.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage, false, 0, false);
_child3.addEventListener("Added: child3", onChild3Added, false, 0, true);
_child2.addChild(_child3);
_child1.addChild(_child2);
addChild(_child1);
}
protected function onChild3Added(event:Event):void
{
trace("child 3 added - target = " + event.target.name);
trace("\tcurrent target = " + event.currentTarget.name);
switch(event.currentTarget){
case _child3:
_child2.dispatchEvent(event);
break;
case _child2:
_child1.dispatchEvent(event);
break;
}
}
protected function onAddedToStage(event:Event):void
{
event.target.dispatchEvent(new TestEvent("Added: " + event.target.name, false));
event.target.dispatchEvent(new TestEvent("Added: " + event.target.name, false, false));
}
}
}
package
{
import flash.events.Event;
public class TestEvent extends Event
{
private var _cloneWithThis:Boolean;
public function TestEvent(type:String, bubbles:Boolean=false, cloneWithThis:Boolean = true)
{
super(type, bubbles, cancelable);
_cloneWithThis = cloneWithThis;
}
override public function clone():Event {
trace("cloning: " + type);
trace("\twith this? " + _cloneWithThis);
if(_cloneWithThis)
return this;
else
return new TestEvent(type, bubbles, false);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment