Interactive Authoring
Week 4 Notes
Constructing your own game, Part 3
When we left off last week we had converted our game to run with the arrow keys and added sound and colllision detection. Your version of the game should be working something like the following. You can download the source file here.
The sound was added with the following code:
mySound = new Sound();
mySound.attachSound("beep4");
mySound.start();
Remember to RMC on the sound file in the library and add a linkage name; "beep4" in the above example.
Collision detection was added with this code:
if(_root.ship.hitTest(_root.target)){
ship.gotoAndPlay("boom");}
Note that when the collision conditions are met, we want a specific portion of the movie clip to play ("boom"). For fun we also added some code to play an explosion sound. The finished code looks like this:
if(_root.ship.hitTest(_root.target)){
ship.gotoAndPlay("boom");
mySound = new Sound();
mySound.attachSound("grunt");
mySound.start();
}
In summary we have created a rudimentary game that uses the keyboard to control movement, detects collisions, and plays sounds. But the game play is still not very satisfactory. So, now we'll focus on making it operate more like a real game.
First we need to make our spaceship fly properly. To begin with replace the code that we wrote for the button with the following:
on (keyPress "<Space>") {
// fire one bullet
shipFire();
}
on (keyPress "<Right>") {
// turn 30 degrees to right
shipTurn(30);
}
on (keyPress "<Left>") {
// turn 30 degrees to left
shipTurn(-30);
}
on (keyPress "<Up>") {
// move forward
shipThrust();
}
on (keyPress "<Down>") {
// stop ship
shipBreak();
}
Then replace the code on the keyframe with the following:
stop();
function startLevel() {
// ship stationary
ship.dx = 0.0;
ship.dy = 0.0;
}
function shipTurn(amt) {
// rotate the ship
ship._rotation += amt;
}
function shipThrust() {
// thrust ship in direction it faces
ship.dx += Math.cos(2.0*Math.PI*(ship._rotation-90)/360.0);
ship.dy += Math.sin(2.0*Math.PI*(ship._rotation-90)/360.0);
}
function shipBreak() {
// stop ship
ship.dx = 0;
ship.dy = 0;
}
function shipMove() {
// move ship horizontally and wrap
ship._x += ship.dx;
if (ship._x > 550) ship._x -= 550;
if (ship._x < 0) ship._x += 550;
// move ship vertically and wrap
ship._y += ship.dy;
if (ship._y > 400) ship._y -= 400;
if (ship._y < 0) ship._y += 400;
}