Publishing Apps to iOS, Android and BlackBerry with AIR

Using Flash Builder Burrito and the Flash IDE you can export your ActionScript projects to three mobile platforms without any change in code! Let's take a look at how to make this possible. Before we get started, here is a video with one of my demo apps running on all three devices:

Multi-screen:
The variety of screen resolutions and DPIs can be the hardest part of building a mobile app. When building an AIR app for mobile devices you have two important properties Capabilities.screenResolutionX(Y) and Capabilities.screenDPI. Using stage.stageWidth on mobile devices does not work the way you would expect it to. If you set up your application in Flash to have a width of 480 and a height of 800, the stageWidth and stageHeight will return these numbers even if the screen is smaller. Another thing to note is that the iOS packager currently targets 320x480 even though many of the iOS devices run 640x960. We'll account for that by adding a screenResolution check.

In my application when a device has lower than 400 pixels in width I drop down the paint brush width. When the device has higher than 400 pixels I increase the size of the menu accordingly. A better approach would be to use the Capabilities.screenDPI for this calculation rather than just the screenResolution. All of your view components should be able to render at different DPIs and resolutions. When you know this from the start it is easy to build into the code. Here is an example:

Actionscript:
  1. if(Capabilities.screenResolutionX <400){
  2.     _canvas = new SimplePaint(Capabilities.screenResolutionX, Capabilities.screenResolutionY-85);
  3.     _canvas.ratio = 0.66;
  4. }else{
  5.     _canvas = new SimplePaint(Capabilities.screenResolutionX, Capabilities.screenResolutionY-125);
  6.     _menuBar.scaleX = _menuBar.scaleY = 1.5;
  7. }

Notice how we are changing the size ratio of the brushes for smaller screens with a lower DPI. For larger screens we are increasing the size of the menu. This makes the application appear the same to the end user even when the app is running at 480x800 on the Android and 320x480 on the iOS! This could be improved to use the actual DPI value but the basic idea is there.

Multi-touch
This is the easy part. Use the Multitouch.maxTouchPoints to determine if the device supports touch. If it does than add touch listeners, if the device has 0 touch points add mouse event listeners. Don't be that person that only codes for one device. With only a few extra lines of code you app will work on desktop and mobile. This also makes debugging much easier!

Actionscript:
  1. if(Multitouch.maxTouchPoints> 0){
  2.     Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
  3.     _canvas.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
  4.     _canvas.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
  5.     _canvas.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);
  6. }else{
  7.     _canvas.addEventListener(MouseEvent.MOUSE_DOWN, onMouseBegin);
  8.     _canvas.addEventListener(MouseEvent.MOUSE_MOVE, onMyMouseMove);
  9.     _canvas.addEventListener(MouseEvent.MOUSE_UP, onMouseEnd);
  10. }

Wasn't that easy? One thing to note is that TOUCH_END was not always dispatched, especially when you start using 3+ touch points. To work around this add a timer into your class that checks to see if a TOUCH_MOVE event has been called. The TOUCH_MOVE event is dispatched even when the user has stopped moving and only stops when the touch has ended. As a backup, check every half a second or so to make sure the specific touch point id is still moving.

Setup:
To setup your workspace to export to all three platforms you'll need to create a ActionScript only Mobile project in Flash Builder Burrito. Create an FLA in the source folder that uses the runnable AS file as its base. Create your assets in the FLA file, export them into a SWC to be used for FB and mark them as run time compiled so they work with the Flash IDE. From here you can export to all three devices without changing any files. Life is good :)

Conclusion:
In just 17 lines of code and a little setup we've got our multi-screen, multi-touch application ready to go! Make sure to add your event listeners for Android to close the app when the user hits back or home. This entire painting application is less than 400 lines of code (~4KB compiled), was built from scratch in less than 12 hours and is easily maintainable across multiple platforms. Performance is great and native APIs are easy to use. My only complaints are that the file size for the iOS is rather large and the iOS exporter doesn't support the Camera API yet. Outside of that, this is the best way to build cross platform apps.

Wet Paint on the Web:

Wet Paint Flash application.

Get Adobe Flash player

Porting to the web required changing 3 lines of code. To get these source files running on mobile devices change the _webMode value to false and remove the width / height from the SWF declaration. I also had to comment out the cacheAsBitmapMatrix to get it running on the web. Still very easy to convert!

Source files.

Enjoy!

Conflicts with Adobe Products and Windows 7

Is the Extension Manager failing to add extensions? Flash Builder unable to install add-ons? Acrobat 9 unable to auto update? Print to PDF freezing after 3 progress bars? The list continues... luckily there is a simple fix!

Problem:
By default, the Windows 7 notification system prevents software products from making changes to your computer. Normally applications would prompt you for access but in many cases Adobe products fail silently.

Solution:
In some cases you can right click on the application and select to run as administrator. This only fixes a handful of issues though. To completely resolve all of the issues you'll need to disable the pesky notifications within Windows 7 all together. To do this, open your control panel, click on User Accounts and Family Safety, select User Accounts, click Change User Account Control settings and finally drag the slider down to the bottom. You will need administrative access to do this.

Keep in mind this change will allow all applications to make changes to your computer without your consent. In my opinion, this is a small price to pay for working software.

Enjoy!

List of known conflicts:
>Dreamweaver unable to install HTML5 extensions during auto update.
>Extension manager unable to install any extensions.
>Flash Builder unable to install plug-ins.
>Acrobat 9 Pro upgrade from 9.0 to 9.2 fails to update.
>Distiller Print to PDF fails in all versions.
>Any others?

FITC Amsterdam 2011

I've always heard good things about FITC Amsterdam and after attending FITC San Francisco, I had to add it to my list for next year. The speaker line up looks phenomenal with over 50 presentations during the 2 day / 2 night conference. Get inspired in one of the coolest cities around the world. Check back for some live blogging during the event!

March 8-9, 2011 Amsterdam, NL

FITC Amsterdam 2011

2011 is nearly here, make sure to grab your early bird tickets before they sell out!

Canvas, Flash and SVG Mobile Comparison

This newer, more complex, animation validates that canvas does not perform well across platforms. Using SVG we can achieve similar performance to Flash when but SVG does not currently work on the Nexus One. Overall conclusion: when displaying animations or content with lots of motion, we need to have two implementations until either iOS supports Flash or until Android supports SVG.

Thanks again for all the submissions!

Flash and SVG Outperform Canvas:

Results:

Canvas Demo:
http://www.blackcj.com/blog/wp-content/jsdemos/BallBounce/eb.html

Flash Demo:
http://www.blackcj.com/blog/wp-content/swfs/BallBounce/box/index.html source code

SVG Demo:
http://www.blackcj.com/blog/wp-content/jsdemos/BallBounce/ebsvg.html

Conclusion:
The new web standards are not mature enough to display animations or interactive content across browsers and platforms for mobile devices. I anticipate this to change within the next couple years (maybe sooner?) but for now, we are stuck implementing interactive content using more than one language. Both Apple and Google should get their act together and support both Flash and SVG. This demo shows that BOTH are ready for the mobile world. The only cross platform solution, canvas, does not currently perform well enough to use for animations and interactive content on mobile devices.

Sample Flash Code:

Actionscript:
  1. _canvas.graphics.clear();
  2. _canvas.graphics.beginFill(_red<<16 | _green<<8 | _blue);
  3. _canvas.graphics.drawRect(0,0,_width,_height);

Sample Canvas Code:

JavaScript:
  1. ctx.clearRect(0, 50, _width, _height)
  2. ctx.fillStyle = 'rgb('+_red+','+_green+','+_blue+')';
  3. ctx.fillRect (0, 50, _width, _height);

Sample SVG Code:

JavaScript:
  1. _square.attr({width:_width, height:_height, fill:"#"+RGBtoHex(_red, _green, _blue)});

Notice:
Please keep comments constructive. I approve almost everything, however, really appreciate the people that take the time to understand what's going on before posting or trying to improve the code. 98% of the code optimizations from the last demo completely missed the point of purposefully redrawing the whole screen to compare performance. They were still good submissions, just not for the context of this comparison. This demo was engineered in response to those people and to include SVG.