Flash AS 3.0 building a message console approach
I am having trouble figuring out how to build a console for my application so i can send msgs to it from any class in the package.
i tried to send a gene开发者_高级运维ric Event, but I cannot find a way send a msg together with the event, or at least a reference to the object which is dispatching the event.
what approach would you suggest?
A custom event with the dynamic keyword will work as advertised in Danjp's post, but I'd advise against it on any sort of larger project. You lose the ability to strictly type what your informational payload is - if someone else has to edit your code, they may have no idea that you've got a dynamically generated "myData" property on your event, for instance. It will cause no end of headaches for someone trying to edit your code later.
The "correct" way to do this is to create custom events with strongly typed members that hold your data. So for instance:
package com.yourdomain.events {
import flash.events.Event;
import com.yourdomain.model.vo.MyValueObject;
public class MyEvent extends Event {
public var myVO:MyValueObject;
public function MyEvent(type:String):void {
super(type);
}
}
}
So, yeah - that's kind of it in a nutshell. You'll want to google the exact format, mine is simplified - your super() call should have other parameters, and you'll want to override the clone() method. But the advantage here is that anyone inspecting your code knows EXACTLY what kind of datatype to look for as a payload. In this case, it's an instance of MyValueObject.
Let me know if you have any specific questions about why, but that's the general idea. Whenever you do a large AS3 project you'll want to use custom events like this to not only dispatch various notifications but also to carry clearly defined and strictly-typed data payloads.
The easiest way is to create a DynamicEvent class that you can add any message you want to it.
package {
import flash.events.Event;
dynamic public class DynamicEvent extends Event {
public function DynamicEvent(t:String, b:Boolean = false, c:Boolean = false){
super(t, b, c);
}
}
}
By setting this class to be dynamic (using dynamic keyword) you are able to add values to your event object:
var _dynEvent:DynamicEvent = new DynamicEvent(LOAD_PROGRESS);
_dynEvent.id = _id;
_dynEvent.bytesLoaded = bytesLoaded;
dispatchEvent(_dynEvent);
Then when you listen for the event:
function eventHandler(e:DynamicEvent):void{
trace(e.bytesLoaded);
trace(e.id);
}
I tried what you guys suggested.. this is my event code
public class ConsoleEvent extends Event
{
public var variable: String;
public function ConsoleEvent( variable1: String, type:String='message',bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
this.variable = variable1;
}
override public function clone():Event
{
return new ConsoleEvent ( variable,type, bubbles, cancelable);
}
}
}
but this is the trace
[Event type="message" bubbles=false cancelable=false eventPhase=2]
i have caught the event with
reader.addEventListener('message', consoleadd);
精彩评论