ActionScript 3.0 TouchEvent: Guaranteed Mobile Speedup
I suppose old habits die hard, especially when those habits still work. (Some programmers are fond of saying, If it ain’t broke, don’t fix it! and the similarly dull-witted proverb, Don’t re-invent the wheel!) Well, with mobile devices, you’d better think about breaking old habits, and I put myself at the top of the dullard list for taking so long to make some adoptions. One of the first changes to make when creating mobile apps is to use the TouchEvent instead of the MouseEvent. In working on a game, I merrily kept using MouseEvent until I noticed that my apps kept getting stuck in the mud when I put them on a mobile device. So, I decided to give TouchEvent a try. Naturally, I couldn’t test in Flash Pro CS5.5; so I had to wait until I launched it in my iPhone. When I did, the difference was night and day. The sluggishness disappeared.
The good news is that MouseEvent and TouchEvent classes work pretty much the same as far as programming them into an app. I have no idea what’s going on with either under the hood, and I shouldn’t have to concern myself with those issues other than to know that one works better with mobile devices than the other. To get started, we’ll take a look at the sequence for setting up for TouchEvents, and while almost identical to MouseEvent, there are programming differences. First, though, click the Play button to see the little testing app on your computer. You can also download the Flash 5.5 Fla file and Client class by clicking on the Download button.
![]()
![]()
As you can see it’s a simple little app to move a “Samurai” character left and right; up and down. The buttons are in Japanese, and if you read Japanese, you’ll realize that the characters look like a first-grader’s attempts to learn how to write in Japanese. (Feel free to make some better simple buttons using Japanese characters and send them along with any comments you may have.)
Possible Controversy: After I had finished this post, I ran across the following:
http://help.adobe.com/en_US/as3/dev/WSb2ba3b1aad8a27b0-6ffb37601221e58cc29-8000.html
In it is a note of caution:
Note: Listening for touch and gesture events can consume a significant amount of processing resources (equivalent to rendering several frames per second), depending on the computing device and operating system. It is often better to use mouse events when you do not actually need the extra functionality provided by touch or gestures. When you do use touch or gesture events, consider reducing the amount of graphical changes that can occur, especially when such events can be dispatched rapidly, as during a pan, rotate, or zoom operation. For example, you could stop animation within a component while the user resizes it using a zoom gesture.
That certainly makes sense, but I found that even using TOUCH_TAP, it was a quicker and far more responsive than listening for a CLICK event. There’s only one way to resolve this issue; try it yourself with a mobile app on a mobile device–any one you want. Use the Comment section to report what you’ve found. The sample app is as good as any for a trial, but feel free to create one of your own choosing.
As Chico Marx remarked in Duck Soup, Well, who you gonna believe, me or your own eyes? Believe your eyes and give it a whirl!
Just the Right Touch
Setting up the TouchEvent includes three imports:
- import flash.events.TouchEvent;
- import flash.ui.Multitouch;
- import flash.ui.MultitouchInputMode;
That contrasts with the single MouseEvent import required when using the mouse event. However, prior to using the TouchEvent class you have to set up your mobile context with the following line:
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
That statement focuses the input mode to touch instead of a mouse event. I was unable to get the TouchEvent to work without the statement; so, don’t forget to include it in your program.
Finally, you need an event listener, and it has the same format as a mouse event except you use TouchEvent. Also, instead of using CLICK, use TOUCH_TAP, as the following illustrates:
upBtn.addEventListener(TouchEvent.TOUCH_TAP,moveNow);
With that knowledge, you’re all set to set up an app using TouchEvent.
Many Ears; One Handler
One of the big memory hogs in programming, especially noticeable in mobile devices, is event listening. Each event listener sets up a series of operations that have to keep listening for an event. Having a single event won’t help, but it’s a step in the direction of having a single listener (which will help). The following Client sets up an example of using the TouchEvent. All of the buttons are SimpleButton objects and the background is a big Sprite. Likewise, Sam3 is a Sprite that you can move around using tap on your mobile device.
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | package { import flash.display.Sprite; import flash.display.SimpleButton; import flash.events.TouchEvent; import flash.ui.Multitouch; import flash.ui.MultitouchInputMode; public final class Client extends Sprite { private static var leftBtn:SimpleButton=new Left(); private static var rightBtn:SimpleButton=new Right(); private static var upBtn:SimpleButton=new Up(); private static var downBtn:SimpleButton=new Down(); private static var bkground:Sprite=new Background(); private var sam3:Sprite=new Sam3(); public function Client() { addChildAt(bkground,0); setControls(); setSamurai(); } private final function setControls():void { Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT; upBtn.x = 825,upBtn.y = 19; upBtn.addEventListener(TouchEvent.TOUCH_TAP,moveNow); addChild(upBtn); leftBtn.x = 740,leftBtn.y = 106; leftBtn.addEventListener(TouchEvent.TOUCH_TAP,moveNow); addChild(leftBtn); rightBtn.x = 873,rightBtn.y = 105; rightBtn.addEventListener(TouchEvent.TOUCH_TAP,moveNow); addChild(rightBtn); downBtn.x = 825,downBtn.y = 202; downBtn.addEventListener(TouchEvent.TOUCH_TAP,moveNow); addChild(downBtn); } private final function setSamurai():void { sam3.x=(960/2); sam3.y=(640/2)+100; addChild(sam3); } private final function moveNow(e:TouchEvent):void { switch(e.target.name) { case "instance11": sam3.y -=5; break; case "instance1": sam3.x -=5; break; case "instance6": sam3.x +=5; break; case "instance16": sam3.y +=5; break; } } } } |
I set it up to be viewed from a landscape perspective because of a larger project I’m developing. Also, as you can see in the moveNow method, there are no “brakes” on the movement, and so the character can move anywhere, including right off the stage. Currently, I have a nicely working global movement system set up using a State Pattern, and I’d like to integrate both local and global movement into a single movement. Not only would this cut down on the number of listeners, but it would also provide a more interesting game. Figure 1 shows how it looks on an iPhone, and I’d like any feedback if tried on Android or other non-iOS devices.

The 960 x 640 size is compressed to fit in an iPhone environment
You also might want to swap out the TouchEvent for a MouseEvent and try it again in a mobile device. You’ll find that it gets “stuck” after a few clicks. As always, we’re interested in your comments and feedback.






Bill Sanders
Recent Comments