Parent/Child Scripting 2
Deeper into P/C objects
In the first P/C tutorial, parents, children and ancestors were confined to the message window. This emphasises the fact that these objects exist entirely in RAM. The next step is to use them to control physical aspects of your presentation.
- Open movie ParentChild.dir.
You will notice 4 balls of different colours on the Stage.There are 2 scripts, one names "Fall" the other name "Roll", which have slight differences. Open Fall:
property pChannel, pSpeed, pStart, pRotation
on new me
**return me
end
on animateIt
**set the puppet of sprite pChannel to TRUE
***if the locV of sprite pChannel > 480 then
*** set the locV of sprite pChannel = 1
** else
*** set the locV of sprite pChannel = (the locV of sprite pChannel) + pSpeed
***set the rotation of sprite pChannel = (the rotation of sprite pChannel) + pRotation
* *end if
**set the locH of sprite pChannel = pStart
**updatestage
end
on listIt me
** append (the actorlist), me
end
We have 3 properties - pChannel, pSpeed and pStart. Two actions are defines animateIt and listIt. The properties don't have any initialsation values as we will set them in the message window. The animateIt handler puppets the sprite, checks to see if it has moved out of the Stage, and moves the postion and rotation. The listIt handler creates add data to an actorlist, which will be described later.
- In the message window
type:
set GravT to new (script "Fall")
set MomentM to new (script "Roll")
You have just created two child objects, one from each parent script.
- In the message window
type:
set the pChannel of GravT = 1
set the pChannel of MomentM = 2
This sets the values for the property pChannel.
- In the message window
type:
set the pSpeed of GravT = 20
set the pSpeed of MomentM = 5
set the pPlace of GravT = 100
set the pPlace of MomentM = 200
set the pRotation of GravT = 0
set the pRotation of MomentM = 10
- Open the message window
and type:
animateIt GravT
animateIt MomentM
The child objects manifest themselves. In Movie Scripts, handlers can be called by their name, but the methods (handlers) contained in a child object are kept separate. Therefore, you need to specify which object you want to get the message.
Try issue several of these commands and watch the balls move.
Much of the sophistication of P/Cs lies in the writing of lists to manage the objects. The actorlist is a list automatically creted by Director. All objects in the list will receive messages directed to it as a whole. Objects can be added or removed from the list without affecting its functionality.
- Open script AllMove
on allMove
* if count(the actorList) > 0 then
** repeat with x = 1 to count(the actorList)
*** animateIt getAt(the actorList, x)
** end repeat
* end if
end
This script sends animateIt to every entry in the actorlist, but only when it isn't empty. By using the property count to get the total number of entries in the list, the actorList can expand or contract and animateIt will still be passes on to all entries.
- Run the movie.
The movie appears to do nothing. Now we use the listIt handler.
- Open the message window
and type:
listIt GravT
listIt MomentM
Now you're fully controlling the motion of the 2 sprites entirely through child objecs.
Try variations by making
new entries in the message window. Change the different properties to
various values. Change one of the pChannel properties to 5.
Look around at other sites for examples of use of parent/child objects. Here's one example
made by Pat Knoff using P/C scripting to give a creative way of animating
a credits page.