The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 4 of 4

Thread: dispatching event on behalf of non display objects

  1. #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:

    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);
    }
    }
    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.

    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.
    email: jwopitz (at) gmail (dot) com
    blogs: http://jwopitz.wordpress.com
    check out the as3isolib

  2. #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).

  3. #3

    seamless 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);
      }
     }
    }
    Code:
    package lex{
     import flash.events.*;
     public class LexDisp extends EventDispatcher {
      public var obj:Object;
      public function LexDisp(_obj:Object) {
       super();
       obj = _obj;
      }
     }
    }
    first frame
    Code:
    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('');
    }
    Output
    Code:
    XM
    pricolo
    hello
    XM
    pricolo
    hello
    I feel good.

  4. #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.

    Code:
    public interface IEventDispatcherProxy
    
    function get eventDispatcherTarget ():IEventDispatcher;
    function set eventDispatcherTarget (value:IEventDispatcher):void;
    then an implementation would be something kinda like so:
    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.
    I dunno but I think that having a built in API rather than hack like mine would be great for certain unique situations.
    email: jwopitz (at) gmail (dot) com
    blogs: http://jwopitz.wordpress.com
    check out the as3isolib

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012