User Guide
Object-oriented programming with Lingo 59
Creating a child object
Child objects exist entirely in memory; they are not saved with a movie. Only parent and ancestor
scripts exist on disk.
To create a new child object, you use the
new() method and assign the child object a variable
name or position in a list so you can identify and work with it later.
To create a child object and assign it to a variable, use the following syntax.
-- Lingo syntax
variableName = new(script "scriptName", parameter1, parameter2, ...)
The scriptName parameter is the name of the parent script, and parameter1, parameter2, ...
are any parameters you are passing to the child object’s
on new handler. The new() method
creates a child object whose ancestor is
scriptName. It then calls the on new handler in the child
object with the specified parameters.
You can issue a
new() statement from anywhere in a movie. You can customize the child object’s
initial settings by changing the values of the parameters passed with the
new() statement.
Each child object requires only enough memory to record the current values of its properties and
variables and a reference to the parent script. Because of this, in most cases, you can create and
maintain as many child objects as you require.
You can produce additional child objects from the same parent script by issuing additional
new() statements.
You can create child objects without immediately initializing their property variables by using the
rawNew() method. The rawNew() method does this by creating the child object without calling
the parent script’s
on new handler. In situations where large numbers of child objects are needed,
rawNew() allows you to create the objects ahead of time and defer the assignment of property
values until each object is needed.
The following statement creates a child object from the parent script Car without initializing its
property variables and assigns it to the variable
car1:
-- Lingo syntax
car1 = script("Car").rawNew()
To initialize the properties of one of these child objects, call its on new handler:
car1.new
Checking child object properties
You can check the values of specific property variables in individual child objects by using a
simple
objectName.propertyName syntax. For example, the following statement assigns the
variable
x the value of the carSpeed property of the child object in the variable car1:
-- Lingo syntax
x = car1.carSpeed
Querying object properties from outside the objects themselves can be useful for getting
information about groups of objects, such as the average speed of all the car objects in a racing
game. You might also use the properties of one object to help determine the behavior of other
objects that are dependent on it.