LogoLogo
  • Home
  • Projects
  • About
  • Contact

Passing Arguments to Event Listening Methods

Devon O. · March 28, 2008 · Actionscript · 1 comments
2

I’ve seen this question asked in several forums. Enough, anyway, that I would remark on it. The question takes many a form, but it boils down to this: How does one pass arguments to a method acting as an event listener in Actionscript 3?

In general there are three broad routes to take:

  • Using a custom object and accessing its properties with the currentTarget property of whatever Event the method is listening for.
  • Dispatching a custom event which can contain its own properties.
  • Using a Dictionary object instance to hold references associated with event dispatching objects.

 Let’s take a little look at each of these.

With the custom object approach, arguments you wish to access in an event listener are added to the event dispatching object as properties. This is most useful when you want the event handling to be performed in a class which contains the dispatching object.

A quick example:

The custom object:

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package {
 
    import flash.display.Sprite;
 
    public class  CustomObject extends Sprite {
 
        private var _foo:Boolean;
        private var _bar:int;
 
        public function CustomObject(foo:Boolean, bar:int):void {
            _foo = foo;
            _bar = bar;
 
            graphics.beginFill(0x000077);
            graphics.drawRect(0, 0, 100, 50);
            graphics.endFill();
        }
 
        public function get bar():int { return _bar; }
 
        public function set bar(value:int):void {
            _bar = value;
        }
 
        public function get foo():Boolean { return _foo; }
 
        public function set foo(value:Boolean):void {
            _foo = value;
        }
    }
}

And document class:

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package {
 
    import flash.display.Sprite;
    import flash.events.MouseEvent;
 
    public class CustomObjectTest extends Sprite {
 
        public function CustomObjectTest():void {
            var co:CustomObject = new CustomObject(true, 23);
            co.addEventListener(MouseEvent.CLICK, onClick);
            addChild(co);
        }
 
        private function onClick(me:MouseEvent):void {
            trace (me.currentTarget.foo, me.currentTarget.bar);
        }
    }
}

As you can see, the foo and bar properties are “passed” to the event listening method as actual properties of the object clicked.

The custom event approach is most useful when you wish event handling to be done within the dispatching object. Here the “passed” arguments are contained inside properties of the dispatched event.

Example:

Custom event class:

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package {
 
    import flash.events.Event;
 
    public class CustomEvent extends Event {
 
        public static const CUSTOM:String = "customEvent";
 
        private var _foo:Boolean;
        private var _bar:int;
 
        public function CustomEvent(type:String, foo:Boolean, bar:int):void {
            super(type);
 
            _foo = foo;
            _bar = bar;
        }
 
        public function get foo():Boolean { return _foo; }
 
        public function get bar():int { return _bar; }
    }
}

A custom object which dispatches the event:

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package {
 
    import flash.display.Sprite;
    import flash.events.MouseEvent;
 
    public class CustomEventObject extends Sprite {
 
        public function CustomEventObject():void {
            graphics.beginFill(0x000077);
            graphics.drawRect(0, 0, 100, 50);
            graphics.endFill();
 
            addEventListener(MouseEvent.CLICK, sendCustomEvent);
        }
 
        private function sendCustomEvent(me:MouseEvent):void {
            dispatchEvent(new CustomEvent(CustomEvent.CUSTOM, true, 23));
        }
    }
}

And finally a document class which ties it all together:

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package {
 
    import flash.display.Sprite;
 
    public class CustomEventTest extends Sprite {
 
        public function CustomEventTest():void {
            var ceo:CustomEventObject = new CustomEventObject();
            ceo.addEventListener(CustomEvent.CUSTOM, onCustomEvent);
            addChild(ceo);
        }
 
        private function onCustomEvent(ce:CustomEvent):void {
            trace(ce.foo, ce.bar);
        }
    }
}

Here you can see the “arguments” are accessed in the event listener as properties of the dispatched event.

Finally, the Dictionary approach is probably the least object oriented of the bunch but sometimes it’s just what the doctor ordered. Here the arguments we wish to pass to the event listener are stored in a dictionary and associated with the event dispatching object. Using this approach it’s easy to maintain everything in a single class such as this:

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package {
 
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.utils.Dictionary;
 
    public class DictionaryTest extends Sprite {
 
        private var _dict:Dictionary
 
        public function DictionaryTest():void {
            _dict = new Dictionary();
 
            var clickableSprite:Sprite = new Sprite();
            clickableSprite.graphics.beginFill(0x000077);
            clickableSprite.graphics.drawRect(0, 0, 100, 50);
            clickableSprite.graphics.endFill();
            clickableSprite.addEventListener(MouseEvent.CLICK, onClick);
 
            _dict[clickableSprite] = [true, 23];
 
            addChild(clickableSprite);
        }
 
        private function onClick(me:MouseEvent):void {
            trace (_dict[me.currentTarget][0], _dict[me.currentTarget][1]);
        }
    }
}

Of course there are other more or less complicated methods of getting the job done, but they follow the same general guidelines (such as adding properties willy nilly to dynamic classes like MovieClips, which is basically the same as using a custom object but not generally recommended, or using objects as associative arrays rather than a Dictionary instance).

  Facebook   Pinterest   Twitter   Google+
  • More With the JiglibFlash Terrain
    March 17, 2010 · 0 comments
    2901
    6
    Read more
  • Adobe Gone Wild
    February 25, 2008 · 0 comments
    1649
    3
    Read more
  • Lorenz Attractor – HTML5 Stylee
    February 06, 2010 · 1 comments
    2524
    4
    Read more
1 Comments:

Sorry, the comment form is closed at this time.

Devon O. Wolfgang

AIR | Unity3D | AR/VR

Unity Certified Developer

Technical Reviewer of “The Essential Guide to Flash CS4 AIR Development” and “Starling Game Development Essentials”

Reviewer of “The Starling Handbook”

Unity Engineer at Touch Press.

Categories
  • Actionscript (95)
  • AIR (16)
  • Flash (99)
  • Games (7)
  • Liberty (13)
  • Life (53)
  • Shaders (20)
  • Unity3D (21)
Recent Comments
  • Devon O. on Unity Ripple or Shock Wave Effect
  • Feral_Pug on Unity Ripple or Shock Wave Effect
  • bavvireal on Unity3D Endless Runner Part I – Curved Worlds
  • Danielius Vargonas on Custom Post Processing with the LWRP
  • Luca G on Unity Ripple or Shock Wave Effect
Archives
  • December 2020 (1)
  • December 2019 (1)
  • September 2019 (1)
  • February 2019 (2)
  • December 2018 (1)
  • July 2018 (1)
  • June 2018 (1)
  • May 2018 (2)
  • January 2018 (1)
  • December 2017 (2)
  • October 2017 (1)
  • September 2017 (2)
  • January 2017 (1)
  • July 2016 (1)
  • December 2015 (2)
  • March 2015 (1)
  • September 2014 (1)
  • January 2014 (1)
  • August 2013 (1)
  • July 2013 (1)
  • May 2013 (1)
  • March 2013 (2)
  • December 2012 (1)
  • November 2012 (1)
  • September 2012 (3)
  • June 2012 (2)
  • May 2012 (1)
  • April 2012 (1)
  • December 2011 (2)
  • October 2011 (3)
  • September 2011 (1)
  • August 2011 (1)
  • July 2011 (1)
  • May 2011 (2)
  • April 2011 (2)
  • March 2011 (1)
  • February 2011 (1)
  • January 2011 (2)
  • December 2010 (3)
  • October 2010 (5)
  • September 2010 (1)
  • July 2010 (2)
  • May 2010 (5)
  • April 2010 (2)
  • March 2010 (7)
  • February 2010 (5)
  • January 2010 (5)
  • December 2009 (3)
  • November 2009 (1)
  • October 2009 (5)
  • September 2009 (5)
  • August 2009 (1)
  • July 2009 (1)
  • June 2009 (2)
  • May 2009 (6)
  • April 2009 (4)
  • March 2009 (2)
  • February 2009 (4)
  • January 2009 (1)
  • December 2008 (5)
  • November 2008 (2)
  • September 2008 (1)
  • August 2008 (6)
  • July 2008 (6)
  • June 2008 (9)
  • May 2008 (4)
  • April 2008 (3)
  • March 2008 (4)
  • February 2008 (9)
  • January 2008 (7)
  • December 2007 (6)
Copyright © 2017 Devon O. Wolfgang