0
votes

Im've created a timer that starts every 1 second. This is the code what's happening every 1 second.

var Random_Value_X:Number = Math.ceil(Math.random() * 1500);
var Random_Value_Y:Number = Math.ceil(Math.random() * 2000);

var enemy:MovieClip = new AI(stage);
addChild(hero);
enemy.x = Random_Value_X;
enemy.y = Random_Value_Y;

Ok. Then I got the class called AI where I've made it so the AI follows my player. The thing is, I need to make a hitTest that testes if an AI hitting another AI? Is there a way I can give every new AI a ID? Like the first gets called "AI1" and second AI2" and then I can make a code that says like If(AT1.hitTestObject(AT2 || AT3))

Hope you understand what I trying to explain! :)

2

2 Answers

0
votes

You should just put them all in an array. Then you can loop through the array and do the hit testing for each one. Depending on how many you have, you might need to split them up into groups so you don't have to do so many checks each frame.

I'm pretty sure you can't just use logical or in the hitTestObject method like that.

0
votes

Considering that you are on root and keyword "this" referring root. If you make instance of class "enemy" then all objects of it will have type "enemy".

import flash.events.Event;

// for every enemy you create, addlistener to it
// it will force to check itself with others
enemy.addEventListener(Event.ENTER_FRAME,checkHit);

// this function will be available to all enemies
// will inform itself that it is hiting enemy instance

function checkHit(e:Event){
// for e.g. object is moving in x direction
// to keep it simple so you can run it in new file
// with two object one is called enemy and other enemy1

// in your case its changing position
e.target.x += 1;


// loop with all children, break when hit someone   
for(var i:uint=0;i<this.numChildren;i++){
// in current situation e.target is also a child of root
// therefore avoid checking it
    if(e.target==this.getChildAt(i)) continue;//trace("Its me");

// if hit
// currently testing hit with all objects on stage
// you can change it to check specific type
    if(e.target.hitTestObject(this.getChildAt(i))){
        trace("I got hit by: "+this.getChildAt(i).toString());
        break;
    }
}

}