Director MX introduced the ability to obtain an object reference to objects and/or movie clips with a Macromedia Flash sprite. This was a huge step forward in terms of programmatic access and control of Flash assets within Director. The process involved two steps:
getVariable()This was a major advancement, but it did have its difficulties since
the getVariable method was really an overloaded version
of the original getVariable() method introduced with the
Flash 4 Asset Xtra. Flash 4 and 5 assets used getVariable()to
get the string representation of an object or variable. Director
MX added an optional parameter to indicate whether to retrieve the
string representation "true" or the object reference "false." It was
easy to forget the optional parameter or to use the incorrect one
and get a string representation as opposed to the object reference.
You can still use the getVariable metho as shown in this getVariable() example:
strValue = sprite(flashSpriteNum).getVariable("myObject")
strValue = sprite(flashSpriteNum).getVariable("myObject", true)
foObject = sprite(flashSpriteNum).getVariable("myObject", false)
var strValue = sprite(flashSpriteNum).getVariable("myObject");
var strValue = sprite(flashSpriteNum).getVariable("myObject", true);
var foObject = sprite(flashSpriteNum).getVariable("myObject", false);
The first two examples return a string representation of a variable in Macromedia Flash, while the final example returns an object reference, which you can use to call methods and properties on the object.
Director MX 2004 simplifies this process by eliminating the getVariable()
middle man, letting you directly access and manipulate Macromedia Flash
objects and movie clips. In essence, access is almost identical to the
way you would access and manipulate the same object types in ActionScript.
The following examples demonstrate direct access to objects within a
Flash sprite.
foObject = sprite(flashSpriteNum).myObject foObject.someMethod() foObject.someProperty = 23 sprite(flashSpriteNum).myObject.someMethod() sprite(flashSpriteNum).myObject.someProperty = 23
var foObject = sprite(flashSpriteNum).myObject; foObject.someMethod(); foObject.someProperty = 23; sprite(flashSpriteNum).myObject.someMethod(); sprite(flashSpriteNum).myObject.someProperty = 23;
While this may seem like a minor change, it provides a great deal of
flexibility. You can manipulate a Macromedia Flash sprite going through
the step-by-step process of first obtaining a reference using getVariable.
In addition to manipulating pre-existing objects in Macromedia Flash
SWFs, you can create new instances of objects for native Flash data
types and classes, as well as those that you have defined using the
Director newObject method. There are two ways to use the
newObject method; the first is to call newObject
without a sprite reference. This creates a new Flash object reference
that is associated with a special code-only instance of the Flash asset.
Object types are restricted to only the built-in object types from Flash.
The other method is to use the newObject method with a
sprite reference. This method lets you instantiate both native Flash
object types as well as user-defined classes on the _global object of
Flash. The following are a few examples of using the newObject
method:
xmlObject = newObject("XML", someXMLString)
xmlObject = sprite(flashSpriteNum).newObject("XML", someXMLString)
var xmlObject = newObject("XML", someXMLString);
var xmlObject = sprite(flashSpriteNum).newObject("XML", someXMLString);
Note that if you intend to pass a Macromedia Flash object reference
back into a Flash sprite as an object reference, you should create
that object reference using the spriteRef.newObject() format.
If you intend to only use the Flash object outside of a Flash sprite,
then using the newObject() syntax without the Flash sprite
reference is the way to go. However, if you have a Flash sprite on
the Stage that will remain in scope for the duration of your need
for the Flash object, then you should use the spriteRef.newObject()
format. This is because the global code-only instance of the Flash
asset is created when the first newObject method is called
upon it. This has the same memory requirements as a separate instance
of the Flash asset. Therefore, it is cheaper performance-wise to use
the sprite reference method.