Using Director > Parent Scripts > Parent script and child object basics |  |

Parent script and child object basics

A parent script is a set of handlers and properties that will define a child object; it is not a child object itself. A child object is a self-contained, independent instance of a parent script. Children of the same parent have identical handlers and properties, so child objects in the same group can have similar responses to events and messages.

Typically, parent scripts are used to build child objects that make it easier to organize movie logic. These child objects are especially useful when a movie requires the same logic to be run several times concurrently with different parameters. You can also add a child object to a sprite's `scriptInstanceList` or the `actorList` as a way to control animation.

Because all the child objects of the same parent script have identical handlers, those child objects respond to events in similar ways. However, because each child object maintains independent values for the properties defined in the parent script, each child object can behave differently than its sibling objects—even though they are instances of the same parent script.

For example, you can create a parent script that defines child objects that are editable text fields, each with its own property settings, text, and color, regardless of the other text fields' settings. By changing the values of properties in specific child objects, you can change any of these characteristics as the movie plays without affecting the other child objects based on the same parent script.

Similarly, a child object can have a property set to either `TRUE` or `FALSE` regardless of that property's setting in sibling child objects.

Differences between child objects and behaviors

While child objects and behaviors are similar in that they both can have multiple instances, they have some important differences as well. The main difference between child objects and behaviors is that behaviors are associated with locations in the Score because they are attached to sprites. Behavior objects are automatically created from initializers stored in the Score as the playback head moves from frame to frame and encounters sprites with attached behaviors. In contrast, child objects from parent scripts must be created explicitly by a handler.

Behaviors and child objects differ in how they become associated with sprites. Director automatically associates a behavior with the sprite that the behavior is attached to, but you must explicitly associate a child object with a sprite.