Hi!
I’m trying to make a custom event that bubbles up from the Items object in this code to the Main object, and start
the changeInfoHandler there.
For some reason, it works when I put the dispatchEvent in the Items object inside a handler for a mouse click,
but when I try to bypass that and call the function directly, it seems to only work in the scope of the Items object.
Main.as:
package
{
import flash.display.*;
import flash.events.*;
import ChangeEvent;
[SWF(backgroundColor="0xefefef", frameRate='60', width='640', height='480')]
/**
* @mxmlc -use-network=false -library-path+=./ -source-path+=./ -locale=en_US
*/
public class Main extends Sprite
{
public function Main()
{
addEventListener(ChangeEvent.INFO, changeInfoHandler);
// this is for senocular's Output.as trace Class
stage.addChild(new Output(stage));
initItems();
}
private function initItems():void
{
var items: Items = new Items();
addChild(items);
}
public function changeInfoHandler(evt: ChangeEvent):void
{
Output.trace("we're in the changeInfoHandler of " + this + "(" + evt.target + evt.currentTarget + evt + ")");
}
}
}
Here’s the ChangeEvent.as
package
{
import flash.events.*;
public class ChangeEvent extends Event
{
public static const INFO:String = "info";
public function ChangeEvent(type: String, bubbles: Boolean, cancelable: Boolean)
{
Output.trace("we're in the ChangeEvent.");
super(type, bubbles, cancelable);
}
public override function clone():Event {
return new ChangeEvent(type, true, false);
}
}
}
The follow Items.as works:
package
{
import flash.display.*;
import flash.events.*;
import ChangeEvent;
import flash.text.TextField;
public class Items extends Sprite
{
public function Items()
{
addEventListener(ChangeEvent.INFO, changeInfoHandler);
addEventListener(MouseEvent.CLICK, mediate);
makeItems();
}
private function makeItems():void
{
var dummyField: TextField = new TextField();
dummyField.text = "here's something to click";
addChild(dummyField);
// mediate();
}
// private function mediate():void
private function mediate(evt: MouseEvent):void
{
var changeEvent:ChangeEvent = new ChangeEvent(ChangeEvent.INFO, true, false);
dispatchEvent(changeEvent);
}
public function changeInfoHandler(evt: ChangeEvent):void
{
Output.trace("we're in the changeInfoHandler of " + this + "(" + evt.target + evt.currentTarget + evt + ")");
}
}
}
traces:
we’re in the ChangeEvent.
we’re in the changeInfoHandler of [object Items]([object Items][object Items][Event type=“info” bubbles=true cancelable=false eventPhase=2])
we’re in the changeInfoHandler of [object Main]([object Items][object Main][Event type=“info” bubbles=true cancelable=false eventPhase=3])
The following Items.as doesn’t work:
package
{
import flash.display.*;
import flash.events.*;
import ChangeEvent;
import flash.text.TextField;
public class Items extends Sprite
{
public function Items()
{
addEventListener(ChangeEvent.INFO, changeInfoHandler);
makeItems();
}
private function makeItems():void
{
var dummyField: TextField = new TextField();
dummyField.text = "here's something to click";
addChild(dummyField);
mediate();
}
private function mediate():void
{
var changeEvent:ChangeEvent = new ChangeEvent(ChangeEvent.INFO, true, false);
dispatchEvent(changeEvent);
}
public function changeInfoHandler(evt: ChangeEvent):void
{
Output.trace("we're in the changeInfoHandler of " + this + "(" + evt.target + evt.currentTarget + evt + ")");
}
}
}
traces:
we’re in the ChangeEvent.
we’re in the changeInfoHandler of [object Items]([object Items][object Items][Event type=“info” bubbles=true cancelable=false eventPhase=2])
Any ideas why?