VARIABLES AND HANDLERS

This example explores how to animate sprites with Lingo, how to swap sprites, how to detect collisions, and how to keep score using custom variables. It also demonstrates the use of conditional statements and the utility of custom handlers.

Click here to see the asDIRoid shockwave file.

The Objective of the Game

The object of the game is to steer the rocket ship through the meteors. Exiting the screen to the left is a completed mission. Collision with the meteors or the asteroid at the bottom of the screen is a crash. The number of completed missions and the number of crashes is incremented in the 2 fields at the top of the screen.


The Game's Elements

The movement of the meteors is controlled by a "drag and drop" behavior called "Random Movement and Rotation" available from the library palette. The 4 meteor sprites are actually just 2 bitmap cast members with slightly different properties.

The rocket's movement horizontally is dictated by Lingo. Moving up or down to avoid the meteors is controlled by the user and the UP and DOWN buttons. The rocket is actually 2 sprites - the rocket itself and the thrust. The thrust has an attached behavior to fade in and out, and another to follow the rocket sprite.

When a collision is detected the rocket sprite is swapped with a film loop of a particle explosion rendered in Infini-D. The puppet sound of the ship's hyperdrive is swapped with an explosion effect.


Setting up the Interaction:

Let's look at the score to see how Lingo sets up the interaction.

1. While loading the movie or shockwave file, a startMovie script is used to declare and initialize two global variables - gCrash and gMission. It also clears the two score display fields.

on startMovie
global gCrash
set gCrash to 0
put EMPTY into field "crashes"
global gMission
set gMission to 0
put EMPTY into field "missions"
end


2. Frame 1 of the score has a script that resets the rocket after it has been exploded. It invokes a sound from Lingo, not the score, that is the sound of the rocket engine. It switches the cast member represented in the sprite channel from the explosion animation back to the rocket. It also resets the size of the sprite.

on exitFrame me
puppetSound "engine"
set the memberNum of sprite 3 to 13
sprite (3).height = 23
sprite (3).width = 74
end


3. The script in frame 2 of the score sets up a loop and starts polling for collisions or completed missions. It aligns the thrust and the rocket, and then sets the rocket in motion from right to left across the screen by decrementing its horizontal location. It also calls a custom handler, "shipCrash", if a collision is detected. If a mission is completed it resets the rockets position to the right side of the screen at a random vertical starting point, and updates the "missions completed" field.

on exitFrame me

global gCrash

global gMission

set the locH of sprite 2 = the locH of sprite 3
set the locV of sprite 2 = the locV of sprite 3
set the locH of sprite 3 = the locH of sprite 3 - 5

if the locH of sprite 3 < 0 then
set gMission = gMission + 1
set missionDisplay = gMission & " missions completed"
put missionDisplay into field "missions"
set the locH of sprite 3 = 640
set the locV of sprite 3 = random(320) + 50
end if

if sprite 1 intersects sprite 3 then
shipCrash
end if

if sprite 5 intersects sprite 3 then
shipCrash
end if

if sprite 6 intersects sprite 3 then
shipCrash
end if

if sprite 7 intersects sprite 3 then
shipCrash
end if

if sprite 8 intersects sprite 3 then
shipCrash
end if

go the frame

end


4. The shipCrash handler describes what happens when the meteors and the rocket intersect. The rocket sprite is switched with the film loop of the explosion and the size of the sprite is reset. A puppet sound of an explosion replaces the engine sound. The playback head is released from the loop on frame 2.

on shipCrash
set the memberNum of sprite 3 to the number of member "explode"
sprite (3).height = 120
sprite (3).width = 160
puppetSound "blast"
go the frame + 1
end


5. Two button scripts (UP & DOWN) were written to move the rocket up and down. They set up a loop to do this while the mouse button is being pressed. The DOWN button increments the sprite's vertical location, the UP button decrements it. Collisions have to be checked within the loop, since it preempts the frame script which is also polling for intersecting sprites. The UpdateStage command has to be used to refresh the display since, again, the mouseDown loop preempts the frame from looping and refreshing the display.

on mouseDown

repeat while the mouseDown
set the locH of sprite 2 = the locH of sprite 3
set the locV of sprite 2 = the locV of sprite 3
set the locV of sprite 3 = the locV of sprite 3 + 1

if the locV of sprite 3 > 470 then
set the locV of sprite 3 = 470
end if

if sprite 1 intersects sprite 3 then
shipCrash
end if

if sprite 5 intersects sprite 3 then
shipCrash
end if

if sprite 6 intersects sprite 3 then
shipCrash
end if

if sprite 7 intersects sprite 3 then
shipCrash
end if

if sprite 8 intersects sprite 3 then
shipCrash
end if

updateStage

end repeat

end


6. Finally, the last frame of the movie recalculates the number of crashes and updates the crash field. It then loops the movie back to frame 1.

on enterFrame me
global gCrash
set gCrash = gCrash + 1
set crashDisplay = gCrash & " crashes"
put crashDisplay into field "crashes"
end
on exitFrame
go frame 1
end