Saving Data: Flash vs AIR

Posted in: Articles, Problems & Solutions by preitz on

The Task:

When Adobe first released AIR, I was asked to prepare a very large base code library for a possible migration. This was to have features available for any games that were created for AIR while not breaking any existing games. The idea was to have a game behave a certain way depending on whether it was built for Flash Player or for AIR. This has come up a few times since then. To give an idea of the task (and the problem) I’ll use a simple example.

For this example I’ll set a simple task: Create an app that saves user data to some storage some where, depending on the following situations:

  • If the app is running in a browser then send the data to a server to be stored.
  • If the app is running on a desktop, save the data to the harddrive.
  • If the app is running on a mobile device, save the data to a sqlite database.

The Problem

The idea, when I was handed the original task, was for the app to determine what platform it was running on and react accordingly. The problem was when you compile an app or game for Flash Player, the AIR specific classes and functions are not available and you’ll get a compiler error.

So the idea of adding all the functions you need into the base code and simply calling the ones you need doesn’t work. The desktop and mobile apps will compile just fine, but the Flash app will not.

A Solution

The solution I came up with is to use an interface. For the purposes of the example I would create an interface called ISavable, and define all the functions necessary to save the data. Then create three classes that implement this interface and saves the data according to one of the three conditions.

Next I would create a class called DocumentBase. I would define a protected variable called _savableClass or something similar and the type would be ISavable. Then I would write the following constructor:

public function DocumentBase(savableClass:ISavable)
 {
    _savableClass = savableClass as ISavable;
 }

This class will contain all of the logic and functionality of the document class. Then I would create three classes that extend DocumentBase and use super(); to pass in the correct interface to the base class. These three classes would be used as the document class for the three different platforms.

Of course this isn’t exactly what was called for in the task. There isn’t any automatic decision making based on the platform. But all of the logic is contained in a single class, with the document classes acting as wrappers and passing in the correct functionality.

  • No Comments Yet