PDA

View Full Version : Stuck on how to change the speed of the enemies diving at the player


Shadowmaster
07-15-2009, 09:25 PM
(Sorry I forgot to put in the title this is for Action Script 3.0, the forum won't allow me to edit the thread title X_____x)

I've been modifying the Zapper game from the Foundation ActionScript for Flash 8. I'm stumped on how to change the speed of the enemies while they're diving towards the player. Below is the entire coding used for the enemies.

FUNCTIONS AND EVENTS FOR 'ALIEN'

*/
function spaceAlien():Void {
//
// Initialize alien...
this.alienSpawn = function() {
this.alienX = this._x=this.startX;
this.alienY = this._y=this.startY;
this.targetX = Math.random()*(gSCREEN_RIGHT-gSCREEN_LEFT);
this.targetY = 100;
this.alienAdvance = 4;
};
//
// main living alien logic...
this.alienBrain = function() {
this.distX = this.targetX-this.alienX;
this.distY = this.targetY-this.alienY;
// Apply inertia equation
this.alienX = this.alienX+(this.distX/accel);
this.alienY = this.alienY+(this.distY/accel);
if ((this.distX<15) && (this.distY<15)) {
this.alienAdvance -= 0.05;
this.alienInitialize();
}
this._x = this.alienX;
this._y = this.alienY;
// check for collisions
if (this.hitTest(bullet) && fired) {
fired = false;
this.gotoAndPlay("explodeMe");
this.alienHit();
}
if (this.hitTest(ship)) {
shipDead = true;
this.targetY = 100;
}
};
//
// main dead alien logic...
this.alienReincarnate = function() {
if (Math.random()<0.015) {
this.gotoAndStop("showMe");
this.alienSpawn();
this.onEnterFrame = function() {
this.alienBrain();
};
}
};
//
// function to set alien's current destination...
this.alienInitialize = function() {
if (skill<Math.random()) {
this.targetX = Math.random()*(gSCREEN_RIGHT-gSCREEN_LEFT);
this.targetY = gSCREEN_TOP+Math.random()*(gSCREEN_BOTTOM/this.alienAdvance);
} else {
this.targetX = ship._x+((Math.random()*100)-50);
this.targetY = ship._y-5;
}
if (targetY>gSCREEN_BOTTOM) {
targetY = gSCREEN_BOTTOM+10;
}
};
//
// function to handle alien being hit...
this.alienHit = function() {
score += level*100;
score_txt.text = score;
this.onEnterFrame = this.alienReincarnate;
};
//
// Spawn a living alien on first run...
this.startX = (gSCREEN_RIGHT-gSCREEN_LEFT)/2;
this.startY = gSCREEN_TOP-500;
this.alienSpawn();
this.onEnterFrame = this.alienBrain;
}
function makeAlien(quantity:Number):Void {
var i, clip;
for (i=0; i<quantity; i++) {
clip = this.attachMovie("alien", "alien"+i, i+200);
spaceAlien.apply(clip);
}
}

The accel parameter is what determines how fast the enemy moves (The lower the accel parameter is, the faster the enemies move). I think accel is used instead of speed because I know the speed parameter is used to determine how fast the player's ship moves.

The part of coding below, I definately know is what causes the enemies to dive towards the player.

} else {
this.targetX = ship._x+((Math.random()*100)-50);
this.targetY = ship._y-5;
}

I've tried adding a line that changes the accel parameter, either it doesn't work, or it also changes the speed of the enemy while they're not diving towards the player. I've tried using true and false activaters (Sorry don't know their real name), I only got the same problems. I've also tried putting the code above under it's own function called Dive, but I've pretty much keep getting the same problems.

Not only have I tried modifiying the code above, I've also tried adding to the code below.

this.alienX = this.alienX+(this.distX/accel);
this.alienY = this.alienY+(this.distY/accel);


This one determines the speed of the enemies. I tried using the same techniques as with the previous code, but they still all failed.

If it helps, here's a link address containing part of the book that has the chapters on the Zapper game below. It should contain all the coding used for the game (It's in a pdf file).

http://www.mediafire.com/?nmjyt2jzdty

prabakar.sjn@zapak.com
07-16-2009, 12:07 PM
declare the variable for speed and dynamicaly add the speed for alien

Shadowmaster
07-16-2009, 01:16 PM
Accel is the variable that modify's the speed for the aliens. Sadly it isn't as simple as just inputting a line that adds more to accel with the set of code below (Which is how the alien dives for the player).

} else {
this.targetX = ship._x+((Math.random()*100)-50);
this.targetY = ship._y-5;
}

Because if I try changing it to this:

} else {
this.targetX = ship._x+((Math.random()*100)-50);
this.targetY = ship._y-5;
accel = accel*2
}

Their speed doesn't return to normal after they've finished diving, and it keeps increasing every time they dive.

And if I try changing it to this:

} else {
this.targetX = ship._x+((Math.random()*100)-50);
this.targetY = ship._y-5;
accel = 10
}

They stay at that set speed after they've finished diving.