views:

51

answers:

3

So I'm dealing with two classes at the moment, a Laser class, (which corresponds to a movieclip) and an Enemy class. The enemy class is linked to multiple enemy movieclips. When it's created, it will look for collisions with an instantiation of the laser class. Here is where I'm having trouble. I want to work things out so that when the Enemy class is instantiated, the Laser class can be passed into it for said collision detection purposes. I can't seem to find a way to do this.

The problem is that the Laser class needs the specific instantiation of the Enemy class in which to pass itself into. And because I'm dealing with multiple Enemy classes at once, it's crucial that the Laser class can pass itself into the right one.

If none of this makes sense, just comment and I'll try to clarify some more.

A: 

the enemy class should dispatch a custom event when he was hit by the laser. and then you can setup where ever you like an eventlistener that listens for the event (like this one enemyEvent.DAMAGE_TAKEN) and fires a function that handles the enemy damage. that will save you the wired step of passing laser class into the enemy class, which doesn't make any sens to me.

antpaw
Okay, but I need to pass the laser class into the enemy class in the first place to even begin to detect the collisions. The rest is all easy.
Miles
+1  A: 

Ideally you want to have a one big loop in your game for detecting collisions. So perhaps when a laser beam is created, push it into an array and have the enemies also in another array/

Then in you collision loop you would have:

//call this function on every ENTER_FRAME
public function updateCollisions():void

//TODO other collisions

   for (var i:int = 0; i < laserArray.length;i++)
   {
         for (var k:int = 0; k < enemiesArray.length;k++)
         {
              if (laser[i].hitTestObject(enemiesArray[k]))
              {
               enemiesArray[k].health-=10;
              }

         }

    }
}

Of course when dealing with collisions there is plenty of things to consider, per pixel hit testing, tunneling, spatial partitioning but those can be left for later (and only if needed).

Allan
+1  A: 

Dear Miles, If I am understanding the problem correct, you have two different classes 'Laser' (L) and Enemy (E) and you want to pass instance of 'L' in corresponding instances of 'E'. Now as you said

'The problem is that the Laser class needs the specific instantiation of the Enemy class in which to pass itself into. And because I'm dealing with multiple Enemy classes at once, it's crucial that the Laser class can pass itself into the right one.'

So does it mean that the L is the main class that can access to an array of instantiated Enemy objects and now you want L to be somehow visible/accessible in these Enemy objects. For such cases you can simpley have a variable of type E that could be instantiated through a setter function. For e.g.

==============================================================================

// main Laser class

function process():void

{

var myEnemy:E = getDesiredEnemyInstance();

myEnemy.laser = this;

}

// Inside class E

// some code private var _laser:Laser;

//some code

public function set laser(l:Laser):void

{

this._laser = l ;

}

=============================================================================

After this accessiblity is acheived you can always use event handling across these classes as suggested by antpaw. You may need to use 'metadata' tag here. I hope that helps. Please correct me if I am still unclear about the problem definition.

Wishes, Ashine

Ashine