I’m currently working on a game, a top-down shooter, and am having one major roadblock: I need to test the proxymity of each player-fired bullet to the enemy/boss to then choose it’s reaction to said bullet:
for each (var i in shots_MC) {/* do stuff */}
Now, the problem is, that simply doesn’t work, at all. The loop doeesn’t even run once.
The problem isn’t in the synthax of the loop itself, for flash CS3 doesn’t return any error in the format, so I am thinking it’s with the changes they made in AS3.
The only other alternative I see to this is:[INDENT] - attaching an addEventListener(event.enterFrame, shot);
- having the shot(); function point to enemyManager(bullet.x, bullet.y, bullet.va);
(bullet.va being each individual bullet’s velocity angle) - from that, in the enemyManager function, calculate weather each bullet is heading towards the enemy and choose the appropriate action.
[/INDENT]My question is: Does anyone know a better/faster way of doing this, or what it is that I am doing wrong for the loop to not work? I have a distinct feeling that what i’m doing isn’t the best solution, since I have no idea how I’m going to make every enemy check every bullet without a “for(var i in object)” loop.
P.S. if I don’t declare “var i” ( i.e. simply “for(i in …)” ) in the loop, an error is returned, so that’s not it.
Note: the cross object is the crosshair that replaces the mouse pointer
function mouseHandler(event:MouseEvent){
var xdiff:Number = cross.x - ship.x;
var ydiff:Number = cross.y - ship.y;
var angle:Number = Math.atan(ydiff/xdiff) * 180/Math.PI + offsetAngle;
if(shotCount > 1) {angle -= shotAngle/(shotCount - 1) + shotAngle/2;}
if((cross.x - ship.x) >= 0){angle += 180;}
for (var i:Number = 0; i < shotCount; i++){
var bullet:WhiteLine = new WhiteLine();
this.shots_MC.addChild(bullet);
if(shotCount > 1) {angle += shotAngle/(shotCount - 1);}
bullet.x = ship.x;
bullet.y = ship.y;
bullet.rotation = angle - 90;
bullet.vx = shotSpeed * Math.cos(angle * Math.PI/180); if(real_Phys == true){bullet.vx -= ship.vx;} else {}
bullet.vy = shotSpeed * Math.sin(angle * Math.PI/180); if(real_Phys == true){bullet.vy -= ship.vy;} else {}
bullet.ox = ship.x;
bullet.oy = ship.y;
bullet.di = angle;
bullet.addEventListener(Event.ENTER_FRAME, shoot);
}};
function shoot(event:Event){ var shot:MovieClip = MovieClip(event.target);
var shotHyp:Number = (shot.x - shot.ox) * (shot.x - shot.ox);
shotHyp += (shot.y - shot.oy) * (shot.y - shot.oy);
shotHyp = Math.sqrt(shotHyp);
shot.x -= shot.vx;
shot.y -= shot.vy;
if(shotHyp > shotRange){shot.removeEventListener(Event.ENTER_FRAME, shoot);
shots_MC.removeChild(shot);}
else{enemyManager(shot.x, shot.y, shot.di);}};