EDIT This is all in Actionscript 2.0, by the way. I forgot to mention that…EDIT
I posted on the Actionscript.org forums a while ago with a problem: I’m making a missile command-like game and my hitTests won’t detect any bullets other than the first one that’s fired. I was told to try looking into arrays…but I’m new to arrays, so this was a rather… difficult… thing for me to get into. With a few days of study under my belt, I was finally able to get my hitTest to work with arrays. Unfortunately, I find that I still have the same problem as before. The hitTest will recognize the first bullet, but completely ignores any other bullet until after the bullet animations stop.
There are other problems as well (like the score sticking a ‘2’ to the end of the number rather than actually adding the numbers. So instead of a 4, I get a 022) but this is the most significant at the moment.
Urrggg… This is getting really frustrating… Can someone please help me?
//constants
score = 0;
//add baddies
var N:Number=0;
baddie1 = attachMovie("badguyA", "badguyA"+N, 0, {_x:300, _y:-25});
baddie2 = attachMovie("badguyB", "badguyB"+N, 1, {_x:300, _y:-75});
baddie3 = attachMovie("badguyC", "badguyC"+N, 3, {_x:300, _y:-150});
baddie4 = attachMovie("boss", "boss"+N, 4, {_x:300, _y:-300});
baddie1.guy.text = "2";
baddie2.guy.text = "4";
baddie3.guy.text = "7";
baddie4.guy.text = "5000";
//key press listener
var myListener:Object = new Object();
myListener.onKeyDown = function() {
if (Key.getCode() == "53") {
attack.text = "5";
//create new bullet
var N:Number=0;
newBullet5 = _root.attachMovie("bulletf","bulletf"+N,_root.getNextHighestDepth(),{_x:300, _y:450});
//allign new bullet to mouse
angle = Math.atan2(_root._ymouse-newBullet5._y, _root._xmouse-newBullet5._x)/(Math.PI/180);
newBullet5._rotation = angle;
//play new bullet animation
newBullet5.gotoAndPlay(2);
}else if (Key.getCode() == "52") {
attack.text = "4";
//create new bullet
var N:Number=0;
newBullet4 = _root.attachMovie("bullete","bullete"+N,_root.getNextHighestDepth(),{_x:300, _y:450});
//allign new bullet to mouse
angle = Math.atan2(_root._ymouse-newBullet4._y, _root._xmouse-newBullet4._x)/(Math.PI/180);
newBullet4._rotation = angle;
//play new bullet animation
newBullet4.gotoAndPlay(2);
} else if (Key.getCode() == "51") {
attack.text = "3";
//create new bullet
var N:Number=0;
newBullet3 = _root.attachMovie("bulletd","bulletd"+N,_root.getNextHighestDepth(),{_x:300, _y:450});
//allign new bullet to mouse
angle = Math.atan2(_root._ymouse-newBullet3._y, _root._xmouse-newBullet3._x)/(Math.PI/180);
newBullet3._rotation = angle;
//play new bullet animation
newBullet3.gotoAndPlay(2);
} else if (Key.getCode() == "50") {
attack = "2";
//create new bullet
var N:Number=0;
newBullet2 = _root.attachMovie("bulletb","bulletb"+N,_root.getNextHighestDepth(),{_x:300, _y:450});
//allign new bullet to mouse
angle = Math.atan2(_root._ymouse-newBullet2._y, _root._xmouse-newBullet2._x)/(Math.PI/180);
newBullet2._rotation = angle;
//play new bullet animation
newBullet2.gotoAndPlay(2);
}
//hit detection
bullets = [newBullet2, newBullet3, newBullet4, newBullet5];
baddies = [baddie1, baddie2, baddie3, baddie4];
setInterval (detect, 1);
function detect() {
for (i = 0; i<1; i++) {
if (bullets*.hitTest(baddies*)) {
baddies*.guy.text = baddies*.guy.text - attack;
bullets*.removeMovieClip();
score = score + attack;
trace("hit!");
trace(attack)
}//else {
//trace (bullets*);
//trace (newbullet2);
//trace (baddie2);
//}
}
if (baddies*.guy.text == 0) {
baddies*.removeMovieClip();
}
}
}
Key.addListener(myListener);
//canon angle
onEnterFrame = function () {
angle = Math.atan2(_root._ymouse - _root.canon._y, _root._xmouse - _root.canon._x) / (Math.PI / 180);
_root.canon._rotation = angle;
};
//bullet align
this.onMouseMove = function(){
this.clear();
}
onMouseDown = function(){
this.clear();
this.lineStyle(2, 0xAA0000, 50);
this.moveTo(300, 450);
this.lineTo(_xmouse, _ymouse);
}
onMouseUp = function(){
this.clear();
}
onRelease = function(){
this.clear();
}