0

這裏我們開始吧!我創建了一個拖放遊戲(足夠簡單),只有在提交按鈕被擊中後才能識別錯誤的棋子。我原本以爲這可以通過使用數組和一個檢查它們的for循環來完成,但每當我這樣做時,它都不會將數據存儲到拖動的剪輯中,因此它們都顯示爲「錯誤」。從那以後,我創造了一個漫長的條件,否則,如果,老實說,感覺笨重,但完美的作品。有什麼想法嗎?如果有條件地將其簡化成一個循環(沒有拋出錯誤)

import flash.filters.* 
import flash.display.Sprite; 


var dragArray:Array = [it_1, it_2, it_3, it_4, it_5, it_6, it_7, it_8, it_9]; 
var matchArray:Array = [mat_1, mat_2, mat_3, mat_4, mat_5, mat_6, mat_7, mat_8, mat_9]; 
var currentClip:MovieClip; 
var startX:Number; 
var startY:Number; 
var wrongGlow:GlowFilter= new GlowFilter(); 
wrongGlow.alpha=1; 
wrongGlow.color = 0xFF0000; 
wrongGlow.blurX = 20; 
wrongGlow.blurY = 20; 


submit_btn.addEventListener(MouseEvent.CLICK, objCheck); 


function objCheck(event:MouseEvent):void { 
// var index:int = dragArray.indexOf(dragArray); 
// var matchClip:MovieClip = MovieClip(matchArray[index]); 

for(var c:int=0; c < dragArray.length; c++){ 
    if(dragArray[c].Boolean == false){ 
//   dragArray[c].removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown); 
    dragArray[c].buttonMode = false; 
    dragArray[c].filters = []; 
}else{ 
dragArray[c].filters = [wrongGlow]; 
} 
} 

} 

for(var i:int = 0; i < dragArray.length; i++) { 
dragArray[i].buttonMode = true; 
dragArray[i].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown); 
} 

function item_onMouseDown(event:MouseEvent):void { 
currentClip = MovieClip(event.currentTarget); 
addChild(currentClip); 
currentClip.startDrag(); 
stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp); 

} 



function stage_onMouseUp(event:MouseEvent):void { 
// stage.removeEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp); 
currentClip.stopDrag(); 
var index:int = dragArray.indexOf(dragArray); 
var matchClip:MovieClip = MovieClip(matchArray[index]); 
/* if(currentClip.hitTestObject(matchClip)){ 
    currentClip.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown); 
    currentClip.Boolean= false; 
}else{ 
    currentClip.Boolean=true; 
} 
}*/ 

if (currentClip == it_1){ 
if (it_1.hitTestObject(mat_1)){ 
    it_1.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown); 
    it_1.Boolean= false; 
}else{ 
    it_1.Boolean= true; 
} 
} else if (currentClip == it_2){ 
if (currentClip.hitTestObject(mat_2)){ 
    it_2.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown); 
    it_2.Boolean= false; 
}else{ 
    it_2.Boolean= true; 
} 
} else if (currentClip == it_3){ 
if (currentClip.hitTestObject(mat_3)){ 
    it_3.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown); 
    it_3.Boolean= false; 
}else{ 
    it_3.Boolean= true; 
} 
} else if (currentClip == it_4){ 
if (currentClip.hitTestObject(mat_4)){ 
    it_4.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown); 
    it_4.Boolean= false; 
}else{ 
    it_4.Boolean= true; 
} 
} else if (currentClip == it_5){ 
if (currentClip.hitTestObject(mat_5)){ 
    it_5.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown); 
    it_5.Boolean= false; 
}else{ 
    it_5.Boolean= true; 
} 
} else if (currentClip == it_6){ 
if (currentClip.hitTestObject(mat_6)){ 
    it_6.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown); 
    it_6.Boolean= false; 
}else{ 
    it_6.Boolean= true; 
} 
} else if (currentClip == it_7){ 
if (currentClip.hitTestObject(mat_7)){ 
    it_7.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown); 
    it_7.Boolean= false; 
}else{ 
    it_7.Boolean= true; 
} 
} else if (currentClip == it_8){ 
if (currentClip.hitTestObject(mat_8)){ 
    it_8.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown); 
    it_8.Boolean= false; 
}else{ 
    it_8.Boolean= true; 
} 
} else if (currentClip == it_9){ 
if (currentClip.hitTestObject(mat_9)){ 
    it_9.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown); 
    it_9.Boolean= false; 
}else{ 
    it_9.Boolean= true; 
} 
} 
} 

回答

1

你也許可以縮短到了這樣的循環:

for (var i:int=0; i < dragArray.length; i++){ 
    if (currentClip == dragArray[i]){ 
     if (currentClip.hitTestObject(matchArray[i])) { 
      dragArray[i].removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown); 
      dragArray[i].Boolean = false; 
     } else { 
      dragArray[i].Boolean = true; 
     } 
    } 
} 
+0

我有一個這樣的循環,但它不會布爾值保存到MC的後,他們是活躍的。所以最終它最終只會說,他們所有的布爾人都是真的。 –

+0

這很奇怪。我的猜測是,當你定義數組時,它實際上是創建並添加'it_1,it_2,...'的副本,而不是存儲對同一對象的引用。我會研究它,看看是否有辦法確保數組存儲對象的引用而不是副本。然而,到目前爲止我看到的所有東西都表明,它應該實際上是參考文件.. – jonhopkins

+0

是的,我發現它也很奇怪,但後來我只是看着鼠標,意識到它使每個mcs的孩子...也許而已。我想知道如何解決這個問題。 –