2012-01-05 2 views
0

매 1 초마다 시작하는 타이머를 만들었습니다. 이것은 1 초마다 발생하는 코드입니다.AS3 AI hitTestObject 자체가 있습니까?

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를 만들어야합니다. 모든 새로운 인공 지능에 ID를 부여 할 수있는 방법이 있습니까? 첫 번째 호출은 "AI1"및 두 번째 AI2 "라고 말하면서 다음과 같은 코드를 만들 수 있습니다. If (AT1.hitTestObject (AT2 || AT3))

희망하는 내용을 이해하고 싶습니다. :)

답변

0

배열에 루프를 넣은 다음 각 배열에 대해 적중 테스트를 수행 할 수 있습니다. 개수에 따라 그룹화하여 그룹화하지 않아도됩니다. 많은 검사 각 프레임을해야한다.

나는 꽤 당신이 단지 논리적 사용할 수 있는지 또는 같은 hitTestObject 방법에있어.

+0

감사! 아마 AS3에 익숙하다고 말한 것을 잊어 버렸기 때문에 코드를 작성할 수 있다면 정말 좋을 것입니다! 배열을 너무 많이 사용하지 않았습니다. – user1133188

0

일을 고려 당신은 루트에 있고 "this"는 루트를 나타내는 키워드입니다. "적"클래스의 인스턴스를 만들면 그 클래스의 모든 객체가 "적"타입이됩니다.

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

}