0

开始了!我正在创建一个拖放游戏(足够简单),它只在点击提交按钮后识别错误的部分。我最初认为这可以使用数组和检查它们的 for 循环来完成,但每次我这样做时,它都不会将数据存储到拖动的剪辑中,因此它们都显示“错误”。从那时起,我创建了一个冗长的条件 else if,老实说感觉很笨拙,但效果很好。有什么想法吗?

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

1 回答 1

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;
        }
    }
}
于 2013-10-28T16:58:41.160 回答