Posts in Category "AIR"

New Flash Runtimes gaming blog: for neophytes (and willing mentors)

Are you new to game development? Struggling to find your footing in the ActionScript game development world? So am I. Although ActionScript and programming are not new to me, game development and gaming in general are. In my blog I’ll be exploring how to get a game going, what kinds of existing games are out there, cutting edge technologies, and how various technologies work together towards a great gaming experience.

Come along for the ride, and please share your experiences and insights! http://actionscriptandotherthings.wordpress.com/

SWC and SWF files in native extensions

I’d like to highlight the role of your ActionScript library — a SWC file — in native extensions. And while I’m at it, I’d like to discuss the role of  the SWF files that the SWC file contains.

You’ve read it before: A native extension is made of two parts: The ActionScript side and the native code side. Here we are just talking about the ActionScript side.

You build the ActionScript side of your extension into an ActionScript library that is a SWC file. The SWC file is an archive file (think ZIP file) that contains your ActionScript classes and other resources, such as its images and strings.

Suppose your extension targets different platforms — for example, both iOS and Android. If  your extension requires a different ActionScript implementation for each platform, create a different SWC file for each platform. A best practice  is that all the ActionScript implementations have the same public interfaces. However, even if all the ActionScript implementations have the same public interfaces, what happens inside the implementation can vary by platform. If so,  you’ll have to create multiple SWC files.

The SWC file comes into play when you package your extension into an ANE file. To do the packaging, you use the ADT command-line tool. You specify a SWC file in the -swc option of the ADT packaging command. For example:

adt -package <signing options> -target ane MyExtension.ane MyExt.xml
    -swc MyExtension.swc
    -platform Android-ARM -C platform/Android .
    -platform iPhone-ARM -C platform/ios .
    -platform default -C platform/default library.swf

The SWC file contains a file called library.swf.  It is the main SWF file of your ActionScript library. ADT automatically puts the library.swf from the SWC file into the ANE file. When an AIR
application uses a native extension, it includes the extension’s ANE file in its library path so that the application can compile. In fact, the application compiles against the public interfaces in library.swf.

Let’s say, as discussed above, you have different SWC files for each platform, but they all share the same public interfaces. In this case, it doesn’t matter which of the platform-specific SWC files you use in the -swc option of the ADT command. It doesn’t matter because the library.swf file in the SWC file is used only for application compilation.

Application execution is another matter. Take a look again at the above ADT command.  Each platform directory (platform/android, platform/ios, and platform/default in this example) specifies the files to include in the ANE package. The -C option tells ADT to make the specified directory the current directory. ADT puts the files and directories that follow into the ANE package. Each platform directory has a library.swf file in it.  For the iOS and Android platform directories, the ‘.’ indicates that all the files in the current directory are to be packaged into the ANE. The files include the library.swf as well as native libraries.

Therefore, you must put the appropriate library.swf into each platform directory. When you have a different SWC file for each platform, get the library.swf from the appropriate SWC file. You can extract the library.swf from a SWC file with extraction tools such as WinZip.  When the application executes and calls your extension class, this is the library.swf file that runs.

Note the following about the library.swf file:

  • ADT requires that you provide a main SWF file named library.swf for each platform. When you create a SWC file, library.swf is the name of the main SWF file.
  • The library.swf file for each platform is different if your ActionScript side has platform dependencies.
  • The library.swf file for each platform is the same if the ActionScript side has no platform dependencies.
  • The library.swf for each platform can load other SWF files that you include in the platform-specific directory. These other SWF files can have any name.

Detailed documentation about creating extensions is at Developing native extensions for Adobe AIR.

For more information about using a native extension in your AIR application, see Using native extensions in AIR applications.

Jackie Levy, Content and Community Lead

Putting the Community in community help

Check out all the great new links we’ve added to community help: http://tmblr.co/Z8MtrwBUsOmL

Optimizing your application with object pooling

As applications grow in size and complexity, the number of objects you use increases rapidly. Instantiation can be expensive. Even with the new GC advice API in Flash Player 11 and AIR 3, garbage collection can slow down or pause your application.  One technique you can use to improve performance by decreasing the number of objects in your application and reducing how often garbage collection runs is object pooling.  With object pooling, you create objects during the initialization of your application, store them in a pool, and keep reusing them.

Look at an example of object pooling in Optimizing Performance for the Flash Platform. You can also read about garbage collection and the new GC advice API in Garbage collection internals for Flash Player and Adobe AIR.

New to AIR Native Extensions?

Looking to get started using Native extension? Here’s a quick primer with links to more resources. What the heck are AIR Native Extensions?

Optimizing Flash and AIR applications for mobile

Looking for ways to build incredibly speedy Flash or AIR applications for mobile devices? If so, look no further than Optimizing Performance for the Flash Platform. Released last year, this guide is brimming with valuable tidbits, provided in part by Adobe’s own Thibault Imbert. It offers tips for optimizing performance on mobile devices, desktops and TVs, ranging from basic ActionScript best practices to sophisticated graphics rendering techniques.

Here are a couple of simple ActionScript optimizations from the Miscellaneous optimizations section of the ActionScript 3.0 performance chapter.

Avoid evaluating statements in loops
Another optimization can be achieved by not evaluating a statement inside a loop. The following code iterates over an array, but is not optimized because the array length is evaluated for each iteration:

for (var i:int = 0; i< myArray.length; i++)
{
}

It is better to store the value and reuse it:

var lng:int = myArray.length;
for (var i:int = 0; i< lng; i++)
{
}

Use reverse order for while loops
A while loop in reverse order is faster than a forward loop:

var i:int = myArray.length;
while (--i > -1)
{
}

These tips provide a few ways to optimize ActionScript, showing how a single line of code can affect performance and memory. Many other ActionScript optimizations are possible. For more information,
see http://www.rozengain.com/blog/2007/05/01/some-actionscript-30-optimizations.

Configure Flash Builder 4.5 to use Flash Player 11 and AIR 3

I’ve seen several people ask how to set up Flash Builder 4.5 to develop applications that use Flash Player 11 and AIR 3 features. Here are the steps:

  1. Download playerglobal.swc from http://www.adobe.com/support/flashplayer/downloads.html.
  2. Copy it to [Flash Builder 4.5]/sdks/4.5.1/frameworks/libs/player/11.0.
  3. In Flash Builder > Project > Properties > Flex Compiler, set the minimum Flash Player version to 11.
  4. In the Flex Compiler properties, specify an additional compiler argument:
    -swf-version=13

Graeme Bull of FMSGuru.com has created an (always) excellent video tutorial that walks you through the steps:

Setting Up Flash Builder 4.5 for Flash 11 and AIR 3 Apps

 

Using Flash Builder 4.5.1 to compile an AIR App that uses a native extension

To use a native extension in an AIR application, you have these three tasks to do:

  1. Declare the extension in your application descriptor file.
  2. Include the ANE file in your application’s library path.
  3. Package the application.

This post is about Step 2 with regard to Flash Builder 4.5.1 projects.

Native extensions are packaged in ANE files. To compile an AIR application that uses a native extension, include the ANE file in the Flash Builder project’s build path.

In Flash Builder 4.5.1, do the following steps:

  1. Change the filename extension of the ANE file from .ane to .swc.
  2. Select Project > Properties on your Flash Builder project.
  3. Select the Flex Build Path in the Properties dialog box.
  4. In the Library Path tab, select Add SWC….
  5. Browse to the SWC file and select Open.
  6. Select OK in the Add SWC… dialog box. The ANE file now appears in the Library Path tab in the Properties dialog box.
  7. Expand the SWC file entry. Double-click Link Type to open the Library Path Item Options dialog box.
  8. In the Library Path Item Options dialog box, change the Link Type to External.

Now you can compile your application using, for example, Project > Build Project.

The part about changing the ANE file’s file extension from .ane to .swc is not strictly necessary. In step 4 above, when you browse to find the file, in the “Choose a SWC file” dialog box, you can change the setting of the Files Of Type drop-down field. It is set by default to *.swc.  Change it to *.*.

Now you can choose the ANE file. Then, continue with step 5 above.

However, when you build your Flash Builder 4.5.1 project with an ANE file rather than a SWC file, you get compiler warnings. The first warning is on the import statement for the extension class, saying that the extension could not be found. Successive warnings occur when you use the extension’s classes.

But interestingly, the application still builds and runs successfully.

Note: Adobe recommends using the next version of Flash Builder for developing native extensions for Adobe AIR. Sign up now to get access to the prerelease of Flash Builder 4.6.

For more information about using a native extension in your AIR application, see Using native extensions in AIR applications.

Detailed documentation about creating extensions is at Developing native extensions for Adobe AIR.

Native extensions: When to call FREContext instance methods

Here’s a little tidbit about developing native extensions for Adobe AIR for Android devices using the native extension Java API.

The main point: The call that your extension’s ActionScript side makes to ExtensionContext.createExtensionContext()  must return before the native side of the extension can call methods of the instance of your class that extends the  FREContext class.

Let’s break that down.

The AIR runtime instantiates your FREExtension instance the first time the  ActionScript side of your extension calls ExtensionContext.createExtensionContext().

The sequence of calls to your Java implementation of the FREExtension class is:

  • FREExtension implementation class constructor
  • initialize()
  • createContext()

In createContext(), you return an instance of your class that extends the FREContext class.

But be careful. The FREContext object can only be considered fully constructed once the ExtensionContext.createExtensionContext() ActionScript method has returned.

Therefore, do not call any methods of your FREContext instance until after the ExtensionContext.createExtensionContext() ActionScript method has returned.

For example, you might be tempted to call your FREContext instance’s getActivity() method in its constructor. Don’t do it. You’ll get an IllegalStateException if you do.

Instead:

  • Provide an initialization method that the ActionScript side can call after ExtensionContext.createExtensionContext() returns.
  • In the initialization method, call your FREContext instance’s getActivity() method.

The Gyroscope and Vibration examples do exactly that. See them at http://www.adobe.com/devnet/air/native-extensions-for-air.html.

Detailed documentation about creating extensions is at Developing native extensions for Adobe AIR.

To use a native extension in your AIR application, see Using native extensions in AIR applications.

Pen tablet native extension example

Sean Fujiwara recently posted a native extension for Adobe AIR at AIR 3 Native Extension Example – PenTablet.

This extension lets an AIR application get the pressure sensitivity data from a Wacom Intuos 4 pen tablet on Windows 7.  It’s a good example of a Windows native extension that handles Windows messages and uses a native library — in this case, the pen tablet’s library.

Sean also provided another native extension for Windows that encodes a bitmap into BMP, JPEG, or PNG format using Microsoft .NET Framework 4.  See  AIR 3 Native Extension Example – ImageProcessor.

You can see more examples and tutorials of native extensions at the Adobe Developer Connection page http://www.adobe.com/devnet/air/native-extensions-for-air.html

Detailed documentation about creating extensions is at Developing native extensions for Adobe AIR.

To use a native extension in your AIR application, see Using native extensions in AIR applications.