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!