首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AS3 AI hitTestObject本身?

AS3 AI hitTestObject本身?
EN

Stack Overflow用户
提问于 2012-01-06 06:25:49
回答 2查看 409关注 0票数 0

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

代码语言:javascript
运行
复制
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?有没有办法给每一个新的AI一个ID?比如第一个被称为"AI1“,第二个被称为”AI2“,然后我可以编写一个类似If(AT1.hitTestObject(AT2 || AT3))的代码。

希望您能理解我试图解释的内容!:)

EN

回答 2

Stack Overflow用户

发布于 2012-01-06 07:23:42

您应该将它们全部放入一个数组中。然后,您可以遍历数组并对每个数组执行命中测试。根据您拥有的数量,您可能需要将它们划分为多个组,这样您就不必在每个帧中执行太多检查。

我非常确定您不能像那样在hitTestObject方法中只使用逻辑或。

票数 0
EN

Stack Overflow用户

发布于 2012-10-22 16:08:28

考虑到您在root上,关键字"this“指的是root。如果你创建了“敌方”类的实例,那么它的所有对象都会有“敌方”类型。

代码语言:javascript
运行
复制
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;
    }
}

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8750765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档