A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Hmmm.. Events?

  1. #1
    Ronze Ronze's Avatar
    Join Date
    Oct 2002
    Location
    Copenhagen, Denmark
    Posts
    185

    Hmmm.. Events?

    Hey everybody

    I'm trying to pass an argument with a dispatchEvent, but I can't seem to understand it. I know that you are able to make custom events, but I'm really confused??

    What is an custom event? What is exactly the difference between the Event object and the constant that you give the new event object?

    I have read all the actionscript tips of the day at Kirupa, but that just makes me more confused. Does anyone have a complete exmaple of how to use a custom event, that passes a parameter to the listener?

    I can't figure out what is what with all these events :-)
    --- Ronze ---

  2. #2
    Senior Member
    Join Date
    Sep 2005
    Location
    Detroit
    Posts
    193
    It's pretty simple really. In order to dispatch an event you just need to create one. A custom event is essentially an object with information about the event.

    to dispatch an event just do dispatchEvent(new Event('MY_EVENT'));
    While the music played you worked by candlelight
    Those san francisco nights
    You were the best in town
    Just by chance you crossed the diamond with the pearl
    You turned it on the world
    That’s when you turned the world around

  3. #3
    Ronze Ronze's Avatar
    Join Date
    Oct 2002
    Location
    Copenhagen, Denmark
    Posts
    185
    But... the event "MY_EVENT" in your example is just a public constant string?
    That's not an event object? I don't get it?
    --- Ronze ---

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    And here's an improvised complete but trivial example
    Code:
    public class StupidEvent extends Event{
      public static var STUPID:String = "STUPID";
    
      public var anarg:String;  //argument
    
      public function StupidEvent(type:String, a:String=''){
        super(type);
        anarg = a;
      }
    }
    Code:
      public class EventTester extends Sprite{
        public var listenerSprite:Sprite;
        public function EventTester(){
          listenerSprite = new Sprite();
          addChild(listenerSprite);
          listenerSprite.graphics.beginFill(0xff0000);
          listenerSprite.graphics.drawCircle(0, 0, 10);
          listenerSprite.graphics.endFill();
          listenerSprite.addEventListener(MouseEvent.CLICK, send);
          addEventListener(StupidEvent.STUPID, hithere);
        }
    
        public function send(evt:MouseEvent):void{
          var blah:String = 'blah';
          var stupid:StupidEvent = new StupidEvent(StupidEvent.STUPID, blah);
          dispatchEvent(stupid);
        }
    
        public function hithere(sevt:StupidEvent):void{
          trace('hi there, the arg in the stupid event is set to: "+sevt.anarg);
        }
      }
    Edit: d'oh. forgot "extends Event"

  5. #5
    Ronze Ronze's Avatar
    Join Date
    Oct 2002
    Location
    Copenhagen, Denmark
    Posts
    185
    I GET IT!!!!!!!!!!!!!!! wuhu... :-) Thanks alot
    --- Ronze ---

  6. #6
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    5Tons, to properly extend an Event, you must also override the clone() and toString() methods or it won't work right. And you also shouldn't cripple the event by stripping out the bubbles and cancelable parameters.
    Code:
    package
    {
    	import flash.events.Event;
    	
    	public class StupidEvent extends Event
    	{
    		public static const STUPID:String  = "stupid";
    
    		public var var1:String;
    		
    		public function StupidEvent($type:String, $var1:String, $bubbles:Boolean = false, $cancelable:Boolean = false)
    		{
    			super($type, $bubbles, $cancelable);
    
    			var1 = $var1;
    		}
    		
    		public override function clone():StupidEvent
    		{
    			return new StupidEvent(type, var1, bubbles, cancelable);
    		}
    		
    		public override function toString():String
    		{
    			return formatToString("StupidEvent", "type", "var1", "bubbles", "cancelable", "eventPhase");
    		}
    	}
    }

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    True enough. But I did call it a StupidEvent.

  8. #8
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    and i rolled with it

  9. #9
    will i ever get it?
    Join Date
    Feb 2004
    Posts
    707
    you guys are funny- i have a quick question...when i have done custom events, i always forget to override the toString() function...the events have still worked (by luck i suppose) but can anyone explain what the toString() function does, and why it should be overran?

  10. #10
    Ronze Ronze's Avatar
    Join Date
    Oct 2002
    Location
    Copenhagen, Denmark
    Posts
    185
    It's just a method that takes the objects properties and print them out as a string. If you don't override that method, you won't be able to trace out the right properties of the custom event. I know tha much :-)
    --- Ronze ---

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