Developing Android Applications with Adobe AIR [25]
// registration point is at upper-left corner as defined earlier
box.x = stage.stageWidth * 0.5 - box.width*0.5;
box.y = stage.stageHeight * 0.5 - box.height*0.5;
For an object whose registration point is in the middle, the x and y positions do not need an offset (see Figure 5-2):
box.x = stage.stageWidth;
box.y = stage.stageHeight;
In this example, a sprite with a registration point at the upper left is positioned at the bottom of the screen (see Figure 5-3):
box.y = stage.stageHeight - box.height;
Figure 5-1. Offsetting the sprite in relation to the registration point
Figure 5-2. Sprite whose registration point is in the middle, requiring no offset
Figure 5-3. Sprite whose registration point is at the upper left, positioned at the bottom of the screen
In this example, the sprite is positioned one-quarter of the way down from the upper-left corner (see Figure 5-4):
box.x = stage.stageWidth*0.25;
box.y = stage.stageHeight*0.25;
Figure 5-4. Sprite positioned one-quarter of the way down the upper-left corner
If your application is displayed full screen, use stage.FullScreenWidth and stage.FullScreenHeight instead.
Sometimes the simplest solution is to group your art and center it, and then fill the borders with a colored background as for a picture frame.
Using a grid layout as a starting point is a good approach for multiple screen deployment. The website at http://960.gs/ provides some ideas.
Vector Graphics or Bitmaps?
Vector graphics scale well, are reusable, and reduce production time, but they can render slowly. Bitmaps render quickly but do not scale well. Enhancement can be done in both areas.
Using vector graphics
Create your vector art at medium size and resize it on the fly based on the device’s resolution and PPI. Then use cacheAsBitmap or cacheAsBitmapMatrix based on the transformation applied to improve its rendering performance. If you want to apply a filter, draw it into a bitmap.
Using bitmaps
You can choose from several techniques when using bitmaps. For instance, you can create art at different resolutions and choose the right one for the device’s resolution. This guarantees fidelity, but at the cost of packaging extra assets. Alternatively, you can create the bitmap at the highest possible resolution and then scale it down if needed. Keep your art light so that you don’t waste too much bandwidth.
Other techniques, commonly used in game production, are sprite sheet production and blitting. We will cover all these topics in depth in Chapter 16.
Developing a Deployment Strategy
If you create an application to deploy on Android, iOS, and the desktop, use different compilation processes. In Flash Professional, create different Flash files and document classes.
In Flash Builder, create separate projects. Define the project dimension in the initialWindow tag of the descriptor file. If you are only developing for the Android platform, your Main class should handle the device’s capabilities and instructions on layout, as described earlier.
Considering Connectivity
Unlike with the desktop experience, you cannot assume your user is always connected when using a mobile device. She might be moving in and out of networks. Be prepared to display a message if your application is not able to function because of this.
At startup and during the life of your application, you can check the state of connectivity. AIR on the desktop has this functionality, but it is not part of the package for AIR for Android. Rest assured: the classes you need are available. Go to the AIR SDK and navigate to Frameworks→Projects→AIR→Core→src→AIR→Net. Copy all four air.net classes into the src directory on Flash Builder and next to your document class in Flash Professional. Change the package name from package air.net to just package.
You can check the status on URLMonitor for an HTTP request or on SocketMonitor for a socket connection request. Both classes extend from the ServiceMonitor class.
Here is an example of an HTTP request:
import URLMonitor;
import