By paddloPayday loans

Home > Mobile, Speed Tweaks > ActionScript 3.0 Single Class Event Handler

ActionScript 3.0 Single Class Event Handler

reachout

Reaching out to Single Class for Event Handling

One Object:Several Events

The first time I read Scott Petersen’s article on optimizing code for iOS devices, I got it wrong in terms of what he was trying to do in one of the optimizations. In re-reading it, I discovered an interesting structure Scott included, and while not a speed tweak in and of itself, I thought it deserved a short discussion and example.

The idea is to have one object with several methods handle all event requests. A single method handles a single request. As you know, event-requests expect one parameter–the event-object listening for the event. Once initiated, the event.target object can be used to determine an appropriate course of action. To see how this works, I put together a simple class for handling requests. It does nothing more than trace back the label of the UI that makes the initial request.

?View Code ACTIONSCRIPT
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
package 
{
	import flash.events.MouseEvent;
 
	public final class OneNav
	{
		public final function navNorth(e:MouseEvent):void
		{
			trace(e.target.label);
		}
 
		public final function navSouth(e:MouseEvent):void
		{
			trace(e.target.label);
		}
		public final function navEast(e:MouseEvent):void
		{
			trace(e.target.label);
		}
		public final function navWest(e:MouseEvent):void
		{
			trace(e.target.label);
		}
	}
}

With the OneNav class, the Client can then address all event handlers to the same class (object) and just append the specific method to the object. The general format is:

listeningObj.addEventListener(MouseEvent.CLICK,actionObj.actionMethod);

So now the developer can have everything to be handled in a single class with methods taking care of the details. If the details change, the developer can just go change the methods to reflect the changed details. To test it out, I’ve included the following Client class:

?View Code ACTIONSCRIPT
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
32
33
34
35
36
37
38
package 
{
	import flash.display.Sprite;
	import fl.controls.Button;
	import flash.events.MouseEvent;
 
	public final class Client extends Sprite
	{
		private var btnNorth:Button=new Button();
		private var btnSouth:Button=new Button();
		private var btnEast:Button=new Button();
		private var btnWest:Button=new Button();
		private var navNow:OneNav=new OneNav();
 
		public function Client()
		{
			btnNorth.addEventListener(MouseEvent.CLICK,navNow.navNorth);
			btnNorth.x=50;btnNorth.y=50;
			btnNorth.label="North";
			addChild(btnNorth);
 
			btnSouth.addEventListener(MouseEvent.CLICK,navNow.navSouth);
			btnSouth.x=50;btnSouth.y=80;
			btnSouth.label="South";
			addChild(btnSouth);
 
			btnEast.addEventListener(MouseEvent.CLICK,navNow.navEast);
			btnEast.x=50;btnEast.y=110;
			btnEast.label="East";
			addChild(btnEast);
 
			btnWest.addEventListener(MouseEvent.CLICK,navNow.navWest);
			btnWest.x=50;btnWest.y=140;
			btnWest.label="West";
			addChild(btnWest);
		}
	}
}

In some respects, this technique can add more work for the developer as he tries to figure out how to get the events to spark something on the viewing area even if he’s using a design pattern. This particular implementation reflects work I’ve been doing on the navigation portion of a game I’m developing, but the technique could apply to virtually any project. I would have to have the OneNav method call a State Pattern to get the appropriate image to show up on the stage. The would take a bit of doing. However, for some other kind of application, it might be just the thing you need.

A Final Word

One of the speed tweaks that Scott points out in this same article is using final in marking classes and methods. If you’re not subclassing a given class, you can mark it final as done in the Client class. Marking a class final allows compile-time determination of the exact function being called. When some methods of a class can be overridden, you cannot mark the class final but you can mark the individual method as final, as was done in the OneNav class.

As a matter of fact, I cannot think of a single use case where I would subclass a Client class, and so, it’s probably a safe bet to mark all Client classes as final. When developing with ActionScript 3.0 for mobile devices, every single speed tweak matters.

Share
Categories: Mobile, Speed Tweaks
  1. January 13, 2012 at 7:57 pm | #1

    I personally found Signals and Slots, as well as other Observable-Observer patterns, the fastest. Event pattern being used in Flash is being misused, and it’s one of the main reasons everything runs so slow. Think about it: each moment thousands of event objects are being instantiated with “new” operator: it’s expensive.

    https://github.com/robertpenner/as3-signals

    • January 14, 2012 at 2:21 am | #2

      Hi Danko,
      The example instantiates a single OneNav object only once:

      private var navNow:OneNav=new OneNav();

      Each event call does not involve a new navNow instantiation; so there’s only a single new involved in the Client and it is only used once. The rest of the event handling is done with methods in the OneNav class instantiated through navNow.

      However, I have a great deal of respect for Robert Penner, and I will definitely take a look at the solution you sent. Thank you for taking the time.

      Kindest regards,
      Bill

  2. January 14, 2012 at 2:34 am | #3

    Yes, what you did is you “patched” your “cords” into a single object. However, you still got 4 listeners, and the system still has to instantiate a mouse event.

    Better solution in your case would be event bubbling, where you add only 1 event listener, on the parent, i.e. Client class. And the you could switch..case on target.

    Why don’t you try Penner’s DeluxeSignal (which converts mouse events into signals) and pathc those 4 signals into your 4 slots? Just for fun.. :)

    • William B. Sanders
      January 14, 2012 at 10:52 am | #4

      Hi Danko,

      Okay, I’ll give it a whirl. I am sure you are right about Penner’s DeluxeSingnal since a similar point was made by Scott Andersen and it makes sense. The solution is not so much in the external coding as it is in using the event handler process as few times as possible. It is certainly worth a try if not a post.

      Kindest regards,
      Bill

  3. boris
    January 17, 2012 at 10:35 am | #5

    Hi
    your OneNav object is never added to the display list so why do you extend Sprite?
    Why not simply designing it as a simple object since it’s responsability is to provides event handler functions?

    • William B. Sanders
      January 17, 2012 at 2:43 pm | #6

      Boris,

      You’re right! D’oh! I was treating it like a Client class expecting to use addChild(). Okay, check it out now; it’s all fixed.

      Thanks for your keen eye and good knowledge,
      Bill

  4. xacto
    January 17, 2012 at 3:45 pm | #7

    Really cool idea! seems useful for helping organize complex interactions like drag/drop too!

    • William B. Sanders
      January 18, 2012 at 12:11 am | #8

      Hi Xacto,

      I was thinking along the same lines. What I’m hoping to eventually do is to set up a design pattern that handles events. I’m not sure which pattern, but the State and Strategy patterns first come to mind.

      Thanks,
      Bill

  5. hayesmaker
    January 29, 2012 at 11:08 am | #9

    marking methods/classes final doesn’t speed up runtime code execution.. so this isn’t really a good speed tweak… and neither is the OneNav class.

  6. William B. Sanders
    January 29, 2012 at 7:21 pm | #10

    Hi Hayesmaker,

    Yeah, the OneNav class doesn’t do much for speeding things up. (I think I said as much.) The final designation does come into play at compile time, but like static objects, the CPU does not have to spend resources at run time determining the exact functions to call. So, indirectly, it saves CPU resources that would otherwise have to be expended at run time.

    Kindest regards,
    Bill

  1. January 17, 2012 at 9:04 am | #1

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>