0

我一直在使用 AS3 处理 Flash 拖放场景。场景分为 4 个“区域”,每个区域有 5 个目标。我使用数组来允许将 mc 放在各自的区域中,没有任何特定的顺序。所有这些都可以正常工作。

我的问题是,有 2 个“拼图”块 (MC) 在屏幕上的视觉显示方式方面彼此相同。我已经设置了阵列以允许将两者都放入区域 1 或区域 2。但是,我想设置它以便如果“相同的部分 1”被放入区域 1,那么“相同的部分 2” " 也不能放入同一个区域。

有关如何执行此操作的任何建议?

4

2 回答 2

2

如何在每个区域设置一个标志,告诉它是否已经附加了一块?

如果您的区域是电影剪辑,您可以简单地向它们添加变量,并设置mcTarget.isTaken = true片段何时附加到它。如果它们只是坐标,您可以使用这样的全局数组

private mZoneTaken:Array = new Array();

for (var i:int=0; i<4; i++)
{
   mZoneTaken[i] = new Array();
   for (var j:int = 0; j<5; j++)
   {
      mZoneTaken[i][j] = false;
   }
}

然后,无论何时将一块棋子放在区域上,您都可以将其标志设置为true,从而防止任何其他棋子附着在该区域上。

于 2012-10-12T23:30:05.133 回答
0

Personally, I would have an array/Vector (Vectors are much, much faster. Though they can be a royal pain to use) of objects within each individual zone. You keep this updated at all times. If you remove one, you use splice(indexOf(obj),1) to remove it from the array.

For your problem, I would do this:

var p1:Sprite = new Sprite();
var p2:Sprite = new Sprite();
var zoneArray:Array = new Array();//there would be one for each zone, simplified it here

//this code would run whenever an object enters the zone
function zoneEnter(e:Event = null):void{
    var currentZone:Sprite; //set this equal to the zone the sprite just entered
    var currentObject:Sprite = e.currentTarget as Sprite; //this way we know which object just entered currentZone

    if ( currentObject == p1 &&  zoneArray.indexOf(p2) >= 0 ) {
        //prevent it from entering the zone
    }

    if ( currentObject == p2 &&  zoneArray.indexOf(p1) >= 0 ) {
        //prevent it from entering the zone
    }
}

There are obviously some limitations here if you are looking at a lot of objects. But if you know, for sure, there will only be two, I think this would be the way to go. If there will be more, you would need to create some kind of control structure that would allow you to check an object against other objects. I could elaborate and give a sample of what I am talking of here if it's needed. It's a tad bit complex, but not hard to implement.

EDIT 10-17-12: Added in example code of how the more advanced logic would look.

var puzzleMap:Array = new Array();

var piece1:Piece = new Piece(); //this is the puzzle piece we'll be checking
var piece2:Piece = new Piece(); 
var piece3:Piece = new Piece();
var piece1Objs:Array = [piece2,piece3];
var piece2Objs:Array = [piece1,piece3];

puzzleMap.push({piece:piece1,others:piece1Objs});
puzzleMap.push({piece:piece2,others:puzzle2Objs);

private function stopDragHandler(e:MouseEvent = null):void{
    var space:Space = space; //this is the space the object was dropped into (I don't know how you identify them, so this is pseudo code)
    var valid:Boolean = true; //we'll check for a bad drop since it's easiest
    if (event.target.dropTarget != null && MovieClip(event.target.dropTarget.parent).allowed.indexOf(event.target) > -1){
        for ( var i:Number = 0; i < puzzleMap.length; i++ ) {
            var current:Object = puzzleMap[i];
            if ( current == e.currentTarget ) {
                for ( var j:Number = 0; j < current.others.length; j++ ) {
                    var checkAgainst:Piece = current.others[i] as Piece;
                    if ( space.currentObjects.indexOf(checkAgainst) < 0 ) {
                        valid = false;
                        break;
                    }
                }
            }
        }
        if ( !valid ) {
            //prevent the drop here
        }
    }
}
于 2012-10-12T23:52:47.393 回答