Air Raid II

In Chapter 5’s Air Raid game, you moved an anti-aircraft gun back and forth using the arrow keys. This allowed you to aim at different parts of the sky as you shot upward.

Now with the power of Math.sin and Math.cos, we can change this game to keep the gun stationary, but allow it to aim at an angle to hit different targets.

NOTE

From these two simple examples can come some interesting results if you combine them. For instance, what if the car were steered by the location of the mouse relative to the car? The car would point at the mouse, and then when you move, it would move toward the mouse at all times. It would, essentially, chase the mouse. So, what if the player were to drive the car like in the first example, but a second car points at the mouse and drives by itself? The second car would chase the first car! See http://flashgameu.com for an example.

SOURCE FILES

AAGun.as

Airplane.as

AirRaid2.fla

Bullet.as

Altering the Gun

The first thing we need to do is to change the AAGun movie clip to allow for rotating
gun barrels. We’ll take the base of the turret out of the movie clip completely, and place it in its own movie clip, AAGunBase. The gun barrels will remain in AAGun, but we’ll recenter it so that the pivot point is at the center and the barrels point to the right, as in Figure 7.6.

Figure 7.6

The barrels must point to the right to correspond with cos and sin values.

The idea is to change the original Air Raid game as little as possible. We’ll be taking the same values for arrow-key presses and using them to change the rotation property of the AAGun, rather than the y value.

The x and y values of the gun are still set, but the rotation value is also set, to –90. The value of –90 means that the gun starts pointed straight up. We’ll restrict the value of the rotation in the same way that we restricted horizontal movement in the first ver- sion of Air Raid. In this case, the values stay between –170 and –20 degrees, which is 50 degrees to the left or right of straight up.

Air Raid II 2

NOTE

Alternatively, you could have a different set of keys set the rotation (for example, A and S or the command and period). Then, leave the arrow keys to move the gun, and you could have both a moving gun and a rotating barrel.

37

238

Chapter 7: Direction and Movement: Space Rocks
So, here is our new AAGun.as code. Look for the lines in the following code that

involve the newRotation variable and rotation property:

package {
import flash.display.*; import flash.events.*; import flash.utils.getTimer;

public class AAGun extends MovieClip {
static const speed:Number = 150.0;
private var lastTime:int; // animation time

public function AAGun() {
// initial location of gun this.x = 275;
this.y = 340; this.rotation = -90;

// movement

addEventListener(Event.ENTER_FRAME,moveGun); }

public function moveGun(event:Event) {
// get time difference
var timePassed:int = getTimer()-lastTime; lastTime += timePassed;

// current position
var newRotation = this.rotation;

// move to the left
if (MovieClip(parent).leftArrow) {

newRotation -= speed*timePassed/1000; }

// move to the right
if (MovieClip(parent).rightArrow) {

newRotation += speed*timePassed/1000; }

// check boundaries
if (newRotation < -170) newRotation = -170; if (newRotation > -20) newRotation = -20;

// reposition

this.rotation = newRotation; }

} }

// remove from screen and remove events public function deleteGun() {

parent.removeChild(this);

removeEventListener(Event.ENTER_FRAME,moveGun); }

Notice that the speed value of 150 stays the same. It is very likely that switching from horizontal movement to rotational movement would mean a change in the speed value, but in this case the value of 150 works well for both.

Changing the Bullets

The Bullets.as class needs to change to have the bullets move upward at an angle, rather than straight up.

The graphic must change, too. The bullets need to point to the right, and they should be centered on the registration point. Figure 7.7 shows the new Bullet movie clip.

Figure 7.7

The new Bullet movie clip re- centers the graphic and points it to the right.

The class needs to change to add both dx and dy movement variables. They will be cal- culated from the angle at which the bullet was fired, which is a new parameter passed into the Bullet function.

Air Raid II 2

39

240

Chapter 7: Direction and Movement: Space Rocks

In addition, the bullet needs to start off at some distance from the center of the gun; in this case, it should be 40 pixels away from center. So, the Math.cos and Math.sin val- ues are used both to compute the original position of the bullet and to compute the dx and dy values.

Also, the rotation of the Bullet movie clip will be set to match the rotation of the gun. So, the bullets will start just above the end of the turret, pointed away from the turret, and continue to move directly away at the same angle:

package {
import flash.display.*; import flash.events.*; import flash.utils.getTimer;

public class Bullet extends MovieClip { private var dx,dy:Number; // speed private var lastTime:int;

public function Bullet(x,y:Number, rot: Number, speed: Number) { // set start position
var initialMove:Number = 35.0;
this.x = x + initialMove*Math.cos(2*Math.PI*rot/360);

this.y = y + initialMove*Math.sin(2*Math.PI*rot/360); this.rotation = rot;

// get speed
dx = speed*Math.cos(2*Math.PI*rot/360); dy = speed*Math.sin(2*Math.PI*rot/360);

// set up animation
lastTime = getTimer(); addEventListener(Event.ENTER_FRAME,moveBullet);

}

public function moveBullet(event:Event) {
// get time passed
var timePassed:int = getTimer()-lastTime; lastTime += timePassed;

// move bullet
this.x += dx*timePassed/1000; this.y += dy*timePassed/1000;

// bullet past top of screen if (this.y < 0) {

deleteBullet(); }

}

// delete bullet from stage and plane list public function deleteBullet() {

MovieClip(parent).removeBullet(this); parent.removeChild(this); removeEventListener(Event.ENTER_FRAME,moveBullet);

}

Changes to AirRaid2.as

Changes are needed to the main class to facilitate the new versions of AAGun and Bullet. Let’s look at each change. We’ll be creating a new class called AirRaid2.as and changing the movie’s document class to match it. Remember to also change the class definition at the top of the code to be AirRaid2 rather than AirRaid.

In the class variable definitions, we need to add the new AAGunBase movie clip as well as keep the AAGun movie clip:

private var aagun:AAGun;
private var aagunbase:AAGunBase;

In startAirRaid, we need to account for the fact that there are two movie clips repre- senting the gun, too. The AAGunBase does not have a class of its own, so we need to set its position to match that of the AAGun.

// create gun
aagun = new AAGun(); addChild(aagun);
aagunbase = new AAGunBase(); addChild(aagunbase); aagunbase.x = aagun.x; aagunbase.y = aagun.y;

The only other necessary change is down in the fireBullet function. This function needs to pass on the rotation of the gun to the Bullet class, so that it knows what

} }

Air Raid II 2

NOTE

You could also remove the AAGunBase entirely by using a different design, or seating the barrels into a graphic that exists at part of the background.

41

242

Chapter 7: Direction and Movement: Space Rocks
direction to shoot the bullet at. So, we’ll add that third parameter to match the third

parameter in the Bullet function that creates a new bullet:
var b:Bullet = new Bullet(aagun.x,aagun.y,aagun.rotation,300);

NOTE

If we were building this game from scratch, we might not even include the first two parameters, which refer to the position of the gun. After all, the gun won’t be moving, so it will always remain at the same position. Because we already had code that dealt with relating the bullet start point to the gun position, we can leave it in and gain the benefit of having only one place in the code where the gun position is set.

We’ve succeeded in changing the AirRaid2.as class. In fact, if we hadn’t needed to add the cosmetic AAGunBase to the movie, we would have only needed that last change in AirRaid2.as. This demonstrates how versatile ActionScript can be if you set it up with a different class for each moving element.

Now we have a fully transformed Air Raid II game that uses a rotating, but stationary gun.