PDA

View Full Version : [AS2] Piercing Missiles


Kizzi
09-25-2009, 06:55 PM
(this is all class-based)
I have made a 2D scrolling shooter, following the kongregate Shootorials until recently when I decided to start adding my own stuff. The problem I am having is I am adding a "Rocket", which is a powerup that gives you piercing bullets. My basic solution was to simply copy the "Missile" class and remove the "this.removeMovieClip()" bit when it hits something. However, when it hits a miniboss or a boss, instead of reducing their health once, it does it once per frame, which means that they are too powerful. I am unsure of how to make them only hit minibosses / bosses once.

Note: The _root.ship.enemies is an array. Every time an enemy is spawned, the ship class adds the enemy to it. The takeDamage function is different depending on the type of enemy: With normal enemies that die instantly, it just calls the explode() function, which simply kills the enemy. In minibosses and bosses it lowers their health and adds a small explosion. Also, I have the "getting powerups" bit sorted and it works for the other 3 powerups.

Code for the Rocket class:

class Rocket extends MovieClip{
var speed = 20;
function onEnterFrame(){
_x += speed;
for(var i in _root.ship.enemies){
if(this.hitTest(_root.ship.enemies[i])){
_root.ship.enemies[i].takeDamage();
}
}
if (_x>600){this.removeMovieClip()}
}
}


Code for the takeDamage() function on the boss class:

function takeDamage(){
health -= _root.ship.bossDamage * 2;
_root.enemyHealthMeter.enemyBar._xscale = health;
var explosion = _root.attachMovie( "Explosion" , "Explosion" + _root.getNextHighestDepth(), _root.getNextHighestDepth() );
explosion._x = _x;
explosion._y = _y;
if(health < 1){
explode();
}
}


PS. If you need any more info then just ask.

krayzeebean
09-26-2009, 05:40 AM
In boss class you can add an array that contains the piercing missiles that hit it. In the take damage function you check that array to see if the missile currently hitting it has already done damage.

Kizzi
09-26-2009, 08:08 PM
Okay, I've got the array working, and when I trace the array it says _level0.Rocket21, etc. This sounds like a stupid question, but how do I check if it's in the array?

krayzeebean
09-27-2009, 04:22 AM
Make the takeDamage function take a missile object as a parameter, and then in the beginning of the takeDamage function you loop through the array and check to see if the missile currently hitting the boss is in the array.

function takeDamage(pMissile:MovieClip){
for (var obj in missileArray) {
if (missileArray[obj] == pMissile)
return;
}
missileArray.push(pMissile);
health -= _root.ship.bossDamage * 2;
_root.enemyHealthMeter.enemyBar._xscale = health;
var explosion = _root.attachMovie( "Explosion" , "Explosion" + _root.getNextHighestDepth(), _root.getNextHighestDepth() );
explosion._x = _x;
explosion._y = _y;
if(health < 1){
explode();
}
}

Kizzi
09-27-2009, 12:03 PM
It works fine mostly, but each rocket is named as "Rocket" + _root.getNextHighestDepth(), and it seems that sometimes they have the same name. How should I change the naming? Also I'll probably need to fix the other weapons now, but that can wait. =p

krayzeebean
09-27-2009, 04:56 PM
Are rockets with the same name not doing damage? The method above isn't comparing their names, it's comparing the instances. It shouldn't matter if they have the same name.

Kizzi
09-30-2009, 08:56 PM
It seems that it still fails sometimes with different names. As far as I can tell if you're continuously firing 2 bullets per second then it works fine, but if you stop firing then start again it seems to be randomly missing.

krayzeebean
09-30-2009, 09:50 PM
It might be the way you're looping through the enemy ships to do your hittest. Are you removing ships in the explode() function or do you wait until the next update? You might have better luck looping backwards through the enemy ship array, then if you remove one of the ships in the middle of the loop only the index of ships you've already hittested will be changed.


for (i = enemyArray.length - 1; i >= 0; i--) {
//hit test and damage code
}


If this doesn't fix it then you'll need to start tracing out values or little messages so you know what's going on when certain parts of your code execute.