2014-07-06 33 views
1

我有這段代碼用於拖放遊戲。我該如何檢查動作3如果數組中的每個項目都有hitTestObject項目的第二個數組

如何檢查動作3如果數組中的每個項目都有hitTestObject第二個數組的項目,那麼可以發生其他情況,例如顯示完成的消息。

var hitArray:Array = new Array(hitTarget1,hitTarget2,hitTarget3,hitTarget4,hitTarget5); 
var dropArray:Array = new Array(drop1,drop2,drop3,drop4,drop5); 
var positionsArray:Array = new Array(); 

for (var i:int = 0; i < dropArray.length; i++) { 
    dropArray[i].buttonMode = true; 
    dropArray[i].addEventListener(MouseEvent.MOUSE_DOWN, mdown); 
    dropArray[i].addEventListener(MouseEvent.MOUSE_UP, mUp); 
    positionsArray.push({xPos:dropArray[i].x, yPos:dropArray[i].y}); 
} 

function mdown(e:MouseEvent):void { 
    e.currentTarget.startDrag(); 
    setChildIndex(MovieClip(e.currentTarget), numChildren - 1); 
} 

function mUp(e:MouseEvent):void { 

    var dropIndex:int = dropArray.indexOf(e.currentTarget); 
    var target:MovieClip = e.currentTarget as MovieClip; 

    target.stopDrag(); 

    if (target.hitTestObject(hitArray[dropIndex])) { 
     target.x = hitArray[dropIndex].x; 
     target.y = hitArray[dropIndex].y; 
     playSound(sosto); 
    }else{ 
     target.x = positionsArray[dropIndex].xPos; 
     target.y = positionsArray[dropIndex].yPos; 
    } 
} 

reset.addEventListener(MouseEvent.CLICK, backObjects); 

function backObjects(e:MouseEvent):void{ 
    for(var i:int = 0; i < dropArray.length; i++){ 
     if(dropArray[i].x == hitArray[i].x && dropArray[i].y == hitArray[i].y){ 
      dropArray[i].x = positionsArray[i].xPos; 
      dropArray[i].y = positionsArray[i].yPos; 
     } 
    } 
} 

function playSound(SoundName:Class):void{ 
    var sound = new SoundName(); 
    var channel:SoundChannel = sound.play(); 
} 
playSound(sosto); 
+0

做一個循環,循環遍歷數組A,在每次迭代中hitTest B的每個元素。顯示消息時,點擊。打破循環。 –

+0

我需要一些更多的幫助來了解如何在數組A中循環,hitTest B的每個元素在每次迭代中 – Marianna

+0

http://stackoverflow.com/questions/24496848/as3-enemies-in-an-array-hit-testing-每個其他 –

回答

1

讓我們創建一個函數,告訴我們每個「hitTarget」及其相應的「drop」是否相互接觸。

function allCorrect():Boolean { 
    //for each item in the hitArray, we repeat the action 
    //we assume that length of hitArray and dropArray are the same 
    for (var i=0;i<hitArray.length;i++) { 
     //if the corresponding target and drop do not hit each other, return false 
     if (!(hitArray[i].hitTestObject(dropArray[i])) { 
      return false 
      //if we return false, this function will continue no further 
     } 
    } 
    //We have cycled through all of the items in the arrays, and none of the 
    //hitTestObjects have been false, so we return true 
    return true 
} 

我們想要測試一下,當我們更新我們的「滴」的位置時,一切是否正確。所以,在你mUp功能,加入這一行的底部

if (allCorrect()) { 
    //display "youz a cool cat, bro" 
    //remove all of your event listeners to improve performance 
} 

希望這個回答您的問題!

+0

謝謝,你的答案有用! – Marianna

0

嘗試加入這一行:

stage.addEventListener(Event.ENTER_FRAME, loop); 

其中 「環」 是包含你的代碼,它看起來像其餘的功能...

function loop (e:Event):void 
{ 
//Your Code 
} 

好運!

相關問題