Results 1 to 4 of 4
-
September 12th, 2009, 03:11 AM #1
dispatching event on behalf of non display objects
This happens to be a really difficult question to ask. And maybe the answer is terribly simple and I have simply overlooked it. Happens all the time. Anyway.....
Say you have a non-visual class (one that does not reside on the display list). This custom class, thru composition, will have a property that is a visual class that DOES reside on the display list. This custom class will also implement IEventDispatcher. So how do I do this:
Basically I want to be able to dispatch the event from the custom class and have the event.target still reference the class rather than the IEventDispatcher property contained within.Code:public class MyNonVizClass implements IEventDispatcher { private var vizProperty:IEventDispatcher; public function MyNonVizClass () { //instead of this vizProperty = new EventDispatcher(this); //I want to do this vizProperty = new Sprite(this); } }
I know this is possible somehow because I think Papervision3D implements something like this. Sadly I havne't been able to find the code in their codebase. I have a work around called an EventDispatcherProxy but it lacks finesse.
-
September 12th, 2009, 04:00 AM #2
if Sprite dispatches an event, its target will always be that sprite. The only way to change that is to re-dispatch the event through your own object, or an EventDispatcher targeting your object (new EventDispatcher(this)). If you need the sprite events to be your class instance's, then you'll have to capture them from the sprite, and re-dispatch them through your class. Your EventDispatcherProxy can probably handle this pretty well, though a more straightforward way would be to addEventListener with the listener being vizProperty.dispatchEvent (where vizProperty is the EventDispatcher).
-
September 12th, 2009, 04:07 AM #3202Registered User
postsseamless my Event non display objects (or any object)
Code:package lex{ import flash.events.Event; public class LexEvent extends Event { //public static const OBJ_SELECT:String = "objSelect"; public static const SIM_BTN_CLICK:String = "simBtnClick"; public var params:Object; public function LexEvent(_type:String, _params:Object, _bubbles:Boolean = false, _cancelable:Boolean = false ) { super( _type, _bubbles, _cancelable ); this.params = _params; } public override function toString():String { return formatToString("CustomEvent","params","type","bubbles","cancelable"); } public override function clone():Event{ return new LexEvent(type, params, bubbles, cancelable); } } }first frameCode:package lex{ import flash.events.*; public class LexDisp extends EventDispatcher { public var obj:Object; public function LexDisp(_obj:Object) { super(); obj = _obj; } } }
OutputCode:import lex.*; var o:Object = {xm:"XM"}; var disp:LexDisp = new LexDisp(o); var e:LexEvent = new LexEvent("hello",{str:"pricolo"}); disp.addEventListener("hello",helloEventHandler); disp.dispatchEvent(e); disp.dispatchEvent(e); function helloEventHandler(e:LexEvent):void{ trace(e.target.obj.xm); trace(e.params.str); trace(e.type); trace(''); }
I feel good.Code:XM pricolo hello XM pricolo hello
-
September 12th, 2009, 12:53 PM #4
@alex lexcuk
sorry but I didn't quite get this.
@senocular
Thanks Senocular. I was afraid that was the answer. But since I hadn't ever played with the Object.prototype or Object.constructor properties before I wasn't sure if there was a trick to this. I guess yeah for now the EventDispatcherProxy class I have written will suffice.
Alot of 3D SDKs/APIs out there utilize this concept of having a pure data object make reference to the visual object on screen. When needing to tie into the display list, the data object utilizes the visual object, and that visual object dispatches events on behalf of the "owner", in this case the data object. So maybe Adobe will listen if I mention that we need a tie-in property so that we can dispatch events on behalf of other objects, where the dispatching object is more complex than a simple EventDispatcher.
then an implementation would be something kinda like so:Code:public interface IEventDispatcherProxy function get eventDispatcherTarget ():IEventDispatcher; function set eventDispatcherTarget (value:IEventDispatcher):void;
I dunno but I think that having a built in API rather than hack like mine would be great for certain unique situations.Code://assuming all DisplayObject classes implemented EventDispatcherProxy var proxy:IEventDispatcherProxy = new Sprite(); var proxyTarget:IEventDispatcher = new NonVisualClass(proxy); //constructor would hook up IEventDispatcher mechanisms proxy.eventDispatcherTarget = proxyTarget; proxyTarget.dispatchEvent(new Event("customEvent", true)); trace(evt.target); //would trace proxyTarget even tho the Sprite was the actual dispatcher of the event.

Reply With Quote



Bookmarks