|
Simple child object
The best way to start learning about parent scripts and child objects is to study a simple movie that creates one child object from a simple parent script. The sample movie "Simple Child Object" shown below is a finished example of a simple parent script. You can download and examine a copy of this movie from the links below. See Chapter 12, "Parent Scripts and Child Objects" in the Learning Lingo manual for more details about parent scripts and child objects.
Click the Left and Right buttons to move the ball left and right.
Clicking New instructs Lingo to create a child objectthe ball. This child object has several obvious properties: its size, color, shape, and the direction in which it moves. Clicking the Left or Right button instructs Lingo to move the ball to the left or right.
The following are important parts of the Lingo that creates and controls the ball child object:
 |
This example displays the Lingo script associated with cast member 8, Ball Parent Script. The script contains a line that starts with the property keyword and the handlers named on new and on moveBall :
property horizPos
on new me
--when the sprite is created,
-- it's placed on the stage
set the horizPos of me to 200
set the locH of sprite 2 to the horizPos of me
set the locV of sprite 2 to 120
return me
end new
on moveBall me, direction
set the horizPos of me to direction * 50 + the horizPos of me
set stageWidth to the stageRight - the stageLeft
-- when more than half of the sprite
--has passed off stage, it is made to appear
-- at the opposite end of the stage
if the horizPos of me > (stageWidth + 99) then
set the horizPos of me to 0
end if
if the horizPos of me < -99 then
set the horizPos of me to stageWidth
end if
set the locH of sprite 2 to horizPos
end moveBall
The on new handler creates the new child objects and sets its initial properties. The on moveBall handler controls the ball's motion to the left and right. |
 |
The script assigned to cast member 4, the New button, calls the on createBall handler. |
 |
This example displays the Lingo script associated with the on createBall handler in cast member 9.
on createBall
global ball1
set ball1 to 0
set ball1 to new(script "Ball Parent Script")
set the visible of sprite 3 to FALSE
set the visible of sprite 4 to FALSE
repeat with n = 5 to 8
set the visible of sprite n to TRUE
end repeat
end createBall
This handler declares ball1 a global variable and issues a new statement. The remaining statements turn on or off other sprites that appear on the Stage as part of the movie's user interface. |
 |
The on prepareMovie handler in cast member 9 uses the puppetSprite command to put sprite channel 2 under Lingo control. This is a convenient way to ensure that changes Lingo makes to sprite channel 2 remain in effect if the playback head leaves the sprite. When authoring, the puppetSprite command isn't necessary if changes to sprite properties aren't to last beyond the life of the current sprite. |
Download the Windows source file for this piece (32K)
Download the Macintosh source file for this piece (64K)
|