0
votes

Array Help?

Just having a little bit of an issue with a current build of my multimedia project (I'm using ActionScript 2 as it's required by our school curriculum).

The code (below) attaches a movie clip in which an enemy entity is stored. The code then pushes it into an array with 4 values (the first three work without any issues).

Every entity inside the array is given actions under EnemyBehaviour() to do (which is to have its own individual health and to face the player via a little Pythagoras fun; the facing works fine).

The bug I am having is - when there are two or more enemies spawned (via the CreateEnemy()) function, only the last enemy can be hit by 'bullets' I am generating from the 'player' - this code is also given below.

I am trying to render all enemies 'hit-able' through the bullets - again, the last generated enemy (via CreateEnemy()) can be hit, but the previous ones cannot, despite the player facing script working.

If there's anything you don't understand just ask please.


Array Code:

http://pastebin.com/WAMVWW9g

onEnterFrame = function (){

        EnemyBehaviour();
}

var Enemies:Array = new Array();
var EnemyIndex:Number = 0;

function CreateEnemy() {

        E = _root.attachMovie ( "mc_enemy" , "mc_enemy" + _root.getNextHighestDepth() , _root.getNextHighestDepth() , { _x:824, _y:402 } );

        E.Diffx;
        E.Diffy;
        E.Distance;

        E.health = 100;

        Enemies.push(E);

        trace('Enemy has been spawned');
}



function EnemyBehaviour() {

    for (i=0; i < _root.Enemies.length; i++) {

                curEnemy = _root.Enemies[i];

                diffx = _root.mc_player._x - curEnemy._x;
                diffy = _root.mc_player._y - curEnemy._y;

                curEnemy.Distance = Math.sqrt( diffx^2 + diffy^2 );

                                curEnemy._x += Math.cos( Math.atan2( diffy, diffx ) ) * 2.5;
                                curEnemy._y += Math.sin( Math.atan2( diffy, diffx ) ) * 2.5;

                                curEnemy._rotation = Math.atan2( diffy, diffx ) / Math.PI * 180;



    }
}

CreateEnemy()

Bullet code:

http://pastebin.com/zmtC74xa

if(this.hitTest(_root.curEnemy)){

                        unloadMovie(this);

                        trace("hit")
                        _root.curEnemy.health -= 10;

                        trace("Enemy health is now " + _root.curEnemy.health)

                        if(_root.curEnemy.health <= 0){

                                unloadMovie(_root.curEnemy)

Thanks!

1
Man, why won't anyone come check out my post... - user3713258
Probably because it's tagged as2, and not as3. as2 is not much used anymore, and thus there is not many people to help. - Aralicia
Could you help me with my code one more time? Haha! - user3713258

1 Answers

0
votes

Your issue is that, in your bullet code, you only check if the bullet hit "curEnemy", and not all Enemies.

In each frame, the EnemyBehaviour() function is executed in one go, and the bullet code after it finishes. That means that hitTest is executed once, after the whole for loop was executed. Out of this loop, curEnemy is always egal to the last given value, in your case the last element.

To fix this issue, you need either to execute your hitTest within the existing loop; or in your bullet code to loop again through the enemy array.

The second solution is probably the quickest to make from your current code. You just have to change your Bullet code :

for (i=0; i < _root.Enemies.length; i++) {
  curEnemy = _root.Enemies[i];
  if(this.hitTest(curEnemy)){
    // TODO : Delete the bullet, and makes enemy lose HP.
  }
}