如果有条件地进入循环(不抛出错误),则简化其他

时间:2013-10-28 16:47:26

标签: actionscript-3 flash loops if-statement conditional-statements

我们走吧!我正在创建一个拖放游戏(足够简单),只有在点击提交按钮后才能识别错误的部分。我原本相信这可以使用数组和一个检查它们的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 个答案:

答案 0 :(得分:1)

您可以将它缩短为像这样的for循环:

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