A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: custom property with custom event- a quick way?

  1. #1
    will i ever get it?
    Join Date
    Feb 2004
    Posts
    707

    custom property with custom event- a quick way?

    i am a bit embarassed to ask this, but i do this so much i have to ask. Occasionally, i need to pass/handle custom events on the fly. Rather than creating a whole custom event class, i will just do something like

    Code:
    myObject.dispatchEvent(new Event('thisCustomEvent'));
    
    //and then handle it later 
    
    myObject.addEventListener('thisCustomEvent',doSomething);
    but what if i want to pass some more info with that custom event...do i have to make it a public variable of myObject? For example, if myObject is storing a number and i want to fire off with the event, and only pass that variable, is there a way to do something like:
    Code:
    myObject.dispatchEvent(new Event('thisCustomEvent',customNumberProperty));
    as you guess, i get an error

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Event does not have any payload property that you can pass along with it, but you could create a subclass which has an arbitrary content property. There would be no need to make that extra value a public property in any class other than the event.
    Code:
    public class DataEvent extends Event{
      public var content:*;
    
      public function DataEvent(type:String, data:*, bubbles:Boolean = false, cancelable:Boolean = false){
         super(type, bubbles, cancelable);
         content = data;
      }
    
      public function clone():Event{
         return new DataEvent(type, content, bubbles, cancelable);
      }
    }
    You can re-use that DataEvent in many projects. Your example might look like:
    Code:
    myObject.dispatchEvent(new DataEvent('thisCustomEvent',customNumberProperty));
    Code:
    function doSomething(de:DataEvent):void{
      trace("doSomething triggered.  data is: "+de.content);
    }
    Since content is typed as *, you can pass any single object there. You can even pass an Array, or Object, if you want to pass more than one value.

  3. #3
    Senior Member
    Join Date
    May 2004
    Posts
    226
    What I usually do when using generic events is store the value needed in a property of the dispatcher then cast event.target to the appropriate class to retrieve the value. Obviously this doens't cover all situations but generally gets the job done simply.

  4. #4
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    And if you're too lazy to make a new Event class but want to have events with custom properties, you can also use a SyncEvent
    http://livedocs.adobe.com/flex/3/lan...SyncEvent.html
    with its changeList array, you can throw assign anything you want to it (well, to the array, not the event object directly).

  5. #5
    will i ever get it?
    Join Date
    Feb 2004
    Posts
    707
    so, in the end , if i want to pass a payload i have no choice but to take a moment to write a subclass....ok....it's what i thought - time to break out the editor

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Quote Originally Posted by senocular View Post
    And if you're too lazy to make a new Event class but want to have events with custom properties, you can also use a SyncEvent
    http://livedocs.adobe.com/flex/3/lan...SyncEvent.html
    with its changeList array, you can throw assign anything you want to it (well, to the array, not the event object directly).
    Wow. Now that smells like a hack.

  7. #7
    will i ever get it?
    Join Date
    Feb 2004
    Posts
    707
    ouch... "lazy" - but, you are right.... i have to work on that

  8. #8
    Senior Member
    Join Date
    Jul 2004
    Posts
    264
    Hi - i know this is an old post - but could anyone please explain how to get the class posted by 5TonsOfFlax to work???

    I tried the following...

    DataEvent.as
    Actionscript Code:
    package classes{
       
        import flash.events.Event;
       
        public class DataEvent extends Event{
          public var content:*;
       
          public function DataEvent(type:String, data:*, bubbles:Boolean = false, cancelable:Boolean = false){
             super(type, bubbles, cancelable);
             content = data;
          }
       
          public function clone():Event{
             return new DataEvent(type, content, bubbles, cancelable);
          }
        }
       
    }

    on stage

    Actionscript Code:
    var MyClass:theClass = new theClass();
    MyClass.addEventListener("callBack",showMessage);
    function showMessage(de:DataEvent):void{
      trace("doSomething triggered.  data is: "+de.content);
    }

    In theClass

    Actionscript Code:
    package  {
       
        public class test {

            public function test() {
                dispatchEvent(new DataEvent('callBack','test message'));
            }

        }
       
    }


    But i cannot get it to work

    Thanks for any help

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your dispatching class is called test, but you've added the listener to an instance of theClass. That means that the dispatching instance is not the listening instance. Since you did not pass a value to the DataEvent constructor for bubbles, the DataEvent will not bubble up through the display list.

    What is the relationship between your instance of test and your instance of theClass?

  10. #10
    Senior Member
    Join Date
    Jul 2004
    Posts
    264
    Sorry - that was a mistake! The class is called "theClass" and not "test"

  11. #11
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Oh, well in that case the event is dispatched before you add the listener. Don't dispatch events in a constructor. I can't think of a good reason for it*. Nothing outside that instance could possibly hear it, and anything inside that instance, you could just call the function directly.

    *actually, I can think of one design in which that makes a bit of sense, but I do not want to encourage it to beginners.

  12. #12
    Senior Member
    Join Date
    Jul 2004
    Posts
    264
    AS3 has taken a lot of the fun out of making flash games. It used to be just about "hacking" something together -- and it "just worked".

    Now you have to know the "correct" way to do something - just to get the game to run.

    I've been working with AS2 for years - and have never heard of a constructor! LOL. Yes, i'm sure my code is terrible - but it used to be great just to make a game, without thinking if i was doing it right.

    Sorry for all the questions 5TonsOfFlax -- please would you mind looking at my code and letting me know if what i have done is ok.

    The reason i have done it like this is -- i can make lots of games just by changing the fla file -- and i never have to change the classes.

    I can add as many or change names of movieclips that the class can access -- without passing the whole stage to the class.

    What do you think? Is that ok? is it ok to call a dispatchEvent every 10 in a timer?

    Thanks again for the help.
    Attached Files Attached Files

  13. #13
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I looked at the .as files in your zip and they look okay, but I cannot open the fla because I don't have Flash. The code which actually creates an instance of theClass is in the fla, if anywhere, so I can't verify that that is correct.

    If it's the same as the code you posted above, then it should work. You should get about 100 traces of "doSomething triggered. data is: ". I'm not sure if the dataEvent content will be the same i variable in each of those, so you may get repeats. It depends on whether the flash player passes int primitives by reference or value, and the timing of execution.

    What results are you getting?

    I certainly wouldn't have done it this way, but I don't even really know what "it" you're trying to accomplish.

    As for AS2 "just working", that is the opposite of my experience. Hardly anything in AS2 worked the way I thought it should, and the necessity of using animation-oriented features (timelines!) to do programming tasks was extremely irritating.

    Your reasons for "doing it like this" don't make sense to me. I literally do not know what you are trying to say there. But if you mean that you can change visual appearances without changing the backing behavior defining classes, you could still do that with a thoughtfully designed architecture.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center