Director Tutorials

 

Parent/Child Scripting 1

Parent/Child Objects

Director's scripting capability is extended to the full with the use of Parent scripts and child objects. While it may seem intimidating at first to new-comers of object-oriented programming, it is not too difficult once you get the hang of it.

Parent scripts can be used to create multiple copies (instances) known as child objects, that behave and respond similarly, but still can be unique on their own. These objects can be created on demand as the movie calls for them.

During a movie, you might want to have sprites that disappear from the Stage as the conditions change.?The sprites may all look the same but move in different directions and speeds.?If two sprites collide, each could respond differently, one may reverse in direction, the other might shatter.

Each child object (also called an instance of a parent script) is a self-contained entity residing in RAM with handlers (also called methods) and properties defined by its parent script.?Director does not limit the amount of child objects that can be created from the same parent script; you can have as many as the computer’s memory can support.

Each child object has a different memory address, so you need some way of addressing messages to the object (so the right object gets the messages). This address is called the 'instance variable', and by convention its written as "me".

When you create a new object from a parent script, you usually do it with the 'on new me' method. For example, consider this parent script. It has an 'on new' handler (also called "constructor method") and a handler (/method) which shows a property that gets set when a child is created.

--parent script
property myName

on new me, pName
?myName = pName
?return me
end

on mSayHi me
?put (myName & " says g’day")
end

The return me line at the end of the on new handler is so that Director can store the address of the newly created object for later use. This address is important, otherwise we do not have any way of sending messages to the object. If there are no references to the object (ie no variables containing the 'me' variable), then the object is destroyed and removed from memory.

Handlers in Movie Scripts can be called by their name, but the methods (handlers) contained in a behaviour or child object are kept separate. With child objects, you need to specify which object you want to get the message.

Before we tackle parent scripting ourselves, take a quick look at Macromedia's demo pages.
Creating a simple child object.
Creating multiple child objects

Writing a Parent Script

  1. Open a new movie.
    We will not be playing the movie, so Stage size or colour does not matter

  2. In the script window type
    on HowIsUni
    **put "No worries mate!"
    end

    on WhatDoYouStudy
    **put "Arts"
    end

    on WhereAreYou
    **put "Wollongong"
    end

    Name this script "CoolCucumber"

  3. Create another scriopt and enter:
    on HowIsUni
    **put "I have a headache!"
    end

    on WhatDoYouStudy
    **put "Architecture"
    end

    on WhereAreYou
    **put "University of New South Wales"
    end

    Name this script "StressedGrad"

  4. In the properties dialg boxes of both scripts, use teh pull-down menu to change their script type to "Parent".

  5. Open the message window and type:
    set Bruce = new (script "CoolCucumber")
    set Dean = new (script "StressedGrad")

    We have just had the 'birth' of 2 child objects, one from each parent script. the primary syntax for creating child objects is:
    set Object = new (script "Parent Script")

  6. In the message window, enter:
    HowIsUni Bruce
    The object performs as instructed
    -- "No worries mate!"

  7. In the message window, enter:
    HowIsUni Dean
    The object performs as instructed
    -- "I have a headache!"

    You can continue asking bruce and Dean questions, using WhatDoYouStudy and WhereAreYou, and see how they respond.

It is important to note that child objects, like variables, exist entirely in RAM. It's possible to add scriping to control a physical object, like a sprite on the stage, but the manifestation of the child object itself is has a ghost-like presence in your computer.