Author: Matt Slaybaugh
Date: October 25, 2006
Level: Beginner
Scuba Diver is a game in which the player uses the arrow keys to move the swimmer to the goal, off the right side of the screen, before running out of air. Avoid colliding with the fish, which makes you lose air faster!
In this tutorial, you will change the values of a variable inside one of the movie clips to make the game harder
- Basic Flash experience
- Flash 8
- myglife_scuba.fla
This software is licensed to the public under the CC-GNU GPL.
In Scuba Diver and many other Flash applications, most of the programming is in the main timeline. But often its useful to include code directly inside one of the elements of the application.
In this case, the movie clip named diver includes code that determines how the swimmer moves and what happens when the swimmer collides with a fish.
The last part of the timeline for the diver movie clip is the animation of the swimmer crashing.
At the end of the animation is scripting that tells the application how much oxygen was just lost from the tank.
// lose air
if (_parent.air > 100) {
_parent.air -= 100;
}
else {
_parent.air = 0;
}
The code above uses a simple condition statement that says, if the amount of air is more than 100 units, reduce the air by 100 units. Otherwise, set the amount of air to 0 units. (The game begins with 1,500 units of air in the tank)
The syntax _parent.air is used because air is the name of a variable in the main, or 'parent' timeline, not in the diver timeline.
The syntax _parent.air -= 100 means to reduce '_parent.air' by 100. It could also be written:
_parent.air = _parent.air - 100;
Let's change the amount of air that's lost whenever there is a collision, in order to make the game more challenging.
The diver movie clip includes scripting that determines
what happens when the swimmer collides with a fish.
- Open myglife_scuba.fla in Flash.
- Expand the Library, or if you don't see the Library, select the main Window menu and click Library.
- In the Library, locate the folder named gameGraphics and double-click to open it.
- In the gameGraphics folder, locate the movie clip named diver and double-click to open it.
- In the timeline, locate the layer named Actions and select the last frame in the timeline in that layer.
- Expand the Actions panel. If you don't see it, select Actions from the Windows menu. In the Actions panel you should see the same code displayed above.
- Make the game a little more challenging by reducing the oxygen in the tank by 200 instead of 100. Line 3 should look like:
_parent.air -= 200; - Save your changes and publish the application.
- Run the game in a browser and you will see that the swimmer loses much more oxygen than before whenever it collides with a fish.
