What is an identifier?

Every instruction that defines an object in a movie, such as a shape, button or sound must be assigned an identifier, in the range 1..65535. This identifier is used to identify the object when, for example, adding it to the display list.

int imageId = 1;
int layer = 1;
int x = 2000;
int y = 1600;

FSDefineImage image = constructor.defineImage(imageId);

movie.add(new FSPlaceObject2(imageId, layer, x, y));
movie.add(new FSShowFrame());

When the Flash Player loads a movie it stores all the object definitions in an internal dictionary using the unique identifier as a key. This means that you must define an object before you make reference to it, otherwise the behaviour of the Flash Player is undefined - if you are lucky the object will not be displayed but you might also crash the Flash Player.

In Transform, all classes that define an object such as a shape, sound, etc. inherit from the FSDefineObject class which contains the methods getIdentifier() and setIdentifier() to get and set the unique identifier.

The FSMovie class can be used to keep track of identifiers both when creating a new movie and when loading an existing one. To generate a new identifier use the newIdentifier() method:

FSDefineImage image = constructor.defineImage(movie.newIdentifier());

movie.add(new FSPlaceObject2(image.getIdentifier(), layer, x, y));
movie.add(new FSShowFrame());

FSMovie uses a simple counter to track identifiers and this counter is incremented each time newIdentifier() is called. The counter is also used to track identifiers when decoding a movie. Every time an object definition is decoded the counter is pre-set with the value of the identifier, if it is higher than the current value. That way, after the movie is decoded, the counter contains the value of the highest identifier value decoded. The means that you can add a new object to an existing movie simply by calling newIdentifier(), avoiding any conflicts caused by duplicate identifiers.

Notes

  1. Identifiers with a value of zero are treated as a special null identifier. These are rarely used in day-to-day Flash files.
  2. Transform stores identifiers using the int data type for convenience. Methods used to set identifiers do perform range checking and an exception will be thrown if the value is outside the range 0..65535.
  3. Since the identifier is used as a dictionary key, you can reuse them if you re-define an object, though obviously this is a potential source of bugs that would be hard to track down.