2016-08-19 72 views
1

我創建了一個謎題,您可以拖放16個部分。我使用了一個數組,以便代碼不會太大。現在,我想添加一個功能,讓每個拼圖碎片一旦到達目的地就會正確對齊。使用ActionScript 3創建一個捕捉函數的數組

我的問題是,我不知道如何創建一個可以實現我的目標的數組。我嘗試以下(不包括數組而是產生太多的代碼,如果我用所有16​​塊拼圖做到這一點):

if(target1_mc.hitTestObject(piece1_mc.tar1_mc)) 
     { 
      piece1_mc.x = 207,15; 
      piece1_mc.y = 119,25; 
     } 

代碼:

import flash.events.Event; 
import flash.events.MouseEvent; 

    var puzzleArr:Array = new Array (piece1_mc, piece2_mc, piece3_mc, piece4_mc, 
piece5_mc, piece6_mc, piece7_mc, piece8_mc, 
piece9_mc, piece10_mc, 
piece11_mc, piece12_mc, piece13_mc, piece14_mc, piece15_mc, piece16_mc); 


for (var i:uint =0; i < puzzleArr.length; i++) { 
puzzleArr[i].addEventListener(MouseEvent.MOUSE_DOWN, drag); 
puzzleArr[i].addEventListener(MouseEvent.MOUSE_UP, drop); 
} 


function drag(event:MouseEvent):void { 
event.currentTarget.startDrag(); 
} 


function drop(event:MouseEvent):void { 
event.currentTarget.stopDrag(); 
} 

回答

1

有幾個方法可以做到這一點。最簡單的方法是爲您的作品添加動態屬性,以存儲作品的目標(正確的位置對象)。

var puzzleArr:Array = []; //you don't really even need the array in my example 
var tmpPiece:MovieClip; //this stores the current dragging piece, and I also reuse it in the loop below 

//I don't like typing a lot, so let's use a loop for all 16 pieces and their targets 
for(var i:int=1;i<=16;i++){ 
    tmpPiece = this["piece" + i + "_mc"]; //get a reference to piece whose number matches i 

    if(!tmpPiece){ 
     trace("Sorry - there is no piece called: 'piece" + i + "_mc'"); 
     continue; 
    } 

    //give the piece a dynamic property that is a reference to it's target spot 
    tmpPiece.targetTile = this["target" + i + "_mc"]; 

    if(!tmpPiece.targetTile){ 
     trace("Sorry - there is no target called: 'target" + i + "_mc'"); 
     continue; 
    } 

    tmpPiece.tar_mc = tmpPiece["tar" + i + "_mc"]; //it would be better to just take the number out of each pieces tar_mc child object making this line uneccessary 

    //track where the piece is placed 
    tmpPiece.startingPos = new Point(tmpPiece.x, tmpPiece.y); 

    //only add the mouse down listener to the piece (not mouse up) 
    tmpPiece.addEventListener(MouseEvent.MOUSE_DOWN, drag); 

    //if still using the array, add the piece to the array 
    puzzleArr.push(tmpPiece); 
} 

接下來,添加一個鼠標移動偵聽器只在拖動時

function drag(event:MouseEvent):void { 
    tmpPiece = event.currentTarget as MovieClip; //assign the dragging object to the tmpPiece var 


    tmpPiece.startDrag(); 

    //add a mouse move listener so you can check if snapping is needed 
    tmpPiece.addEventListener(MouseEvent.MOUSE_MOVE, moving); 

    //add the mouse up listener to the stage - this is good because if you drag fast, the mouse can leave the object your dragging, and if you release the mouse then it won't trigger a mouse up on the dragging object 
    stage.addEventListener(MouseEvent.MOUSE_UP, drop); 
} 

function drop(event:MouseEvent):void { 
    //stop all dragging 
    this.stopDrag(); 

    if(tmpPiece){ 
     //remove the mouse move listener 
     tmpPiece.removeEventListener(MouseEvent.MOUSE_MOVE, moving); 

     //ensure a snap at the end of the drag 
     if(!checkSnapping()){ 
      //if not snapped, reset it's position 
      tmpPiece.x = tmpPiece.startingPos.x; 
      tmpPiece.y = tmpPiece.startingPos.y; 
     } 
    } 

    //remove the mouse up listener 
    stage.removeEventListener(MouseEvent.MOUSE_UP, drop); 
} 

現在讓我們做的鼠標移動處理程序中捕捉:

function moving(e:MouseEvent = null):void { 
    checkSnapping(); 
} 

//return true if snapped 
function checkSnapping():Boolean { 
    if(tmpPiece && tmpPiece.tar_mc.hitTestObject(tmpPiece.targetTile)){ 
     tmpPiece.x = tmpPiece.targetObj.x - tmpPiece.tar_mc.x; 
     tmpPiece.y = tmpPiece.targetObj.y - tmpPiece.tar_mc.y; 
     return true; 
    } 

    return false; 
} 
+0

在你的代碼中,你的地址是否與target_mc不同,它的地址是「tar1_mc」,「tar2_mc」等於意思是tar_mc =/= target_mc – bulletproof

+0

總是會出現這個錯誤:TypeError:Error#1010:沒有屬性。 – bulletproof

+0

如果你澄清了所有這些對象是什麼,也許會很好。 'tarx_mc'的目的是什麼。錯誤指向什麼代碼行? – BadFeelingAboutThis

1
for (var i:int = 0; i < puzzleArray.length; i++) 
{ 
    if(puzzleArray[i].hitTestObject(puzzleArray[i]._target)) 
    { 
     puzzleArray[i].x = puzzleArray[i]._xGoal; 
     puzzleArray[i].y = puzzleArray[i]._yGoal; 
    } 
} 

顯然你需要爲拼圖塊添加一些屬性(_xGoal_yGoal_target),你可以做到這一點,但你想。你可能可以使用一個循環,但只有當它們有某種順序時。如果它們的大小和網格不相同,那麼您必須明確地手動輸入這些內容。

如果它們位於網格中並且每個部分的尺寸相同,請告訴我您是否需要幫助在循環中創建這些屬性。

+0

他們在一個網格中,每一塊是相同的大小。你能告訴我如何用正確的屬性創建一個循環嗎? – bulletproof

+0

我會盡力在我午餐時間到達 –

相關問題