2012-08-15 3 views
0

기본적으로 플레이어가 특정 항목을 피하는 게임을 만들려고합니다. 지금까지 무작위로 3 마리의 상어를 무대에 추가하는 코드 조각이 있습니다.for 루프에서 둘 이상의 변수에 대한 충돌 감지를 작성 하시겠습니까?

아이디어는 플레이어가 시작 위치로 돌아 오면 상어의 속도, 속도 등을 포함하는 액션 스크립트 파일을 가지고 있으며, 프로그램이 상어를 실행할 때마다 다른 위치에 나타납니다.

그러나 상어의 충돌 테스트를 시도 할 때 상어 중 하나만 반응합니다. 3 가지 상어가 모두 플레이어 (square_mc)에 미치는 영향을 파악할 수 없습니다. 어떤 도움이라도 대단히 감사하겠습니다.

//Pirate game, where you have to avoid particular object and get to the finish line to move onto the final level. 

stage.addEventListener(KeyboardEvent.KEY_DOWN, moveMode); 
function moveMode(e:KeyboardEvent):void { 

//movements for the pirate ship, this will allow the ship to move up,down,left and right. 

if (e.keyCode == Keyboard.RIGHT) { 
    trace("right"); 
square_mc.x = square_mc.x + 25; 
} 
else if (e.keyCode == Keyboard.LEFT) { 
    trace("left"); 
square_mc.x = square_mc.x - 25; 
} 
else if (e.keyCode == Keyboard.UP) { 
    trace("up"); 
square_mc.y = square_mc.y - 25; 
} 
else if (e.keyCode == Keyboard.DOWN) { 
    trace("down"); 
square_mc.y = square_mc.y + 25; 
} 
} 

//for.fla 
//this program uses a for loop to create my Sharks 
//a second for loop displays the property values of the sharks 

function DisplayShark():void{ 
for (var i:Number=0;i<3;i++) 
{ 
    var shark:Shark = new Shark(500); 
    addChild(shark); 

    shark.name=("shark"+i); 
    shark.x=450*Math.random(); 
    shark.y=350*Math.random(); 

} 
} 
DisplayShark(); 

for(var i=0; i<3;i++){ 
var currentShark:DisplayObject=getChildByName("shark"+i); 

trace(currentShark.name+"has an x position of"+currentShark.x+"and a y position of"+currentShark.y); 
} 



//here we will look for colliosion detection between the two move clips. 

addEventListener(Event.ENTER_FRAME, checkForCollision); 
function checkForCollision(e:Event):void { 

if (square_mc.hitTestObject(currentShark)) 
{ 
trace("The Square has hit the circle"); 
    square_mc.x=50 
    square_mc.y=50 //these lines of code return the square back to it's  original location 
} 

}

답변

0

그냥 ENTER_FRAME로 루프에 대한 귀하의 이동 :

addEventListener(Event.ENTER_FRAME, checkForCollision); 
function checkForCollision(e:Event):void { 

    for(var i=0; i<3;i++){  
     var currentShark:DisplayObject=getChildByName("shark"+i); 
     if (square_mc.hitTestObject(currentShark)) 
     { 
      trace("The Square has hit the circle"); 
      square_mc.x=50; 
      square_mc.y=50; 
     } 
    } 

} 

당신은 한 번만 루프의 통과와 currentShark 변수를 설정할 수 없습니다 - 당신은 그냥 겁니다 충돌 테스트를 수행 할 때마다 한 상어를 상대로 한 오히려 충돌을 확인하려고 할 때마다 모든 상어를 반복하고 충돌 테스트를 수행해야합니다.

+0

정말 고마워요! 왜 내가 그 생각을하지 않았는지 모르겠지만, 지금 당연한 것처럼 보인다! 빠른 응답 주셔서 감사합니다;) – dMo

관련 문제