AS3 AI hitTestObject 与自身?

发布于 2024-12-25 07:08:08 字数 495 浏览 2 评论 0原文

我创建了一个每 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 跟随我的玩家。问题是,我需要做一个 hitTest 来测试一个 AI 是否击中另一个 AI?有没有办法给每个新的人工智能一个ID?就像第一个被称为“AI1”和第二个 AI2“一样,然后我可以编写一个类似 If(AT1.hitTestObject(AT2 || AT3)) 的代码

希望你明白我试图解释的内容!:)

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! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔日梦未散 2025-01-01 07:08:08

你应该把它们全部放在一个数组中。然后您可以循环遍历数组并对每个数组进行命中测试。根据您拥有的数量,您可能需要将它们分成几组,这样您就不必在每帧上进行如此多的检查。

我很确定你不能只在 hitTestObject 方法中使用逻辑或。

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.

吾性傲以野 2025-01-01 07:08:08

考虑到您位于根目录上,并且关键字“this”指的是根目录。如果您创建“enemy”类的实例,那么它的所有对象都将具有“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;
    }
}

}

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;
    }
}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文