Actionscript 3 AS3 AI是否将测试对象与自身关联?

Actionscript 3 AS3 AI是否将测试对象与自身关联?,actionscript-3,timer,artificial-intelligence,hittest,Actionscript 3,Timer,Artificial Intelligence,Hittest,我已经创建了一个计时器,每1秒启动一次。 这是每1秒发生的代码 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

我已经创建了一个计时器,每1秒启动一次。 这是每1秒发生的代码

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;
嗯。然后我得到了一个名为AI的类,我在那里制作了它,所以AI跟随我的玩家。问题是,我需要做一个命中测试,测试一个AI是否命中另一个AI?有没有办法给每一个新AI一个ID?像第一个被称为“AI1”,第二个被称为“AI2”,然后我可以编写一个代码,上面写着像If(AT1.hitTestObject(AT2 | | AT3))


希望你能理解我想解释的内容!:)

你应该把它们都放在一个数组中。然后你可以在数组中循环,并对每个数组进行命中测试。根据你有多少个,你可能需要将它们分成若干组,这样你就不必对每帧执行太多检查


我很确定你不能只在hitTestObject方法中使用logical or这样的方法。

考虑到你在根目录上,关键字“this”引用根目录。如果你创建类“敌军”的实例,那么它的所有对象都将具有类型“敌军”

导入flash.events.Event;
//对于你创造的每一个敌人,都要加入它
//它将迫使自己与他人相互制约
敌方。添加事件列表(事件。输入帧,检查命中);
//所有敌人都可以使用此功能
//将通知自己正在打击敌人
函数检查命中(e:事件){
//例如,对象在x方向上移动
//使其保持简单,以便可以在新文件中运行
//有两个目标,一个称为敌人,另一个称为敌人
//在你的情况下,它改变了位置
e、 目标x+=1;
//和所有的孩子一起打圈,打人时要断开

for(var i:uint=0;iThanks!我可能忘了说我几乎是AS3的新手,所以如果您可以并且想写一些代码,那就太好了!我没有太多使用数组。
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;
    }
}

}