AS3 - 可拖动的正方形从数组生成

时间:2013-02-14 14:12:32

标签: actionscript-3 flash

早上好, 我有一个由bankgrid生成的网格(var bankArray:Array),它将数组的值显示为gotoAndStop(该数字(f))一个彩色网格。  所有正方形都是来自同一MovieClip(TBank)的子节点,一个接一个地生成数组中的水平(i)和垂直(j)位置。 我试图让那些正方形可以拖延......徒劳无功:

var bankgrille:MovieClip = new MovieClip();
var bankList:Vector.<TBank> = new Vector.<TBank>();

var bankArray:Array = [
                     [0,0,0,0,0,0,0,0],
                     [0,0,0,0,0,0,0,0],
                     [0,0,0,0,0,0,0,0],
                     [0,0,1,0,0,0,0,0],
                     [0,0,2,0,1,0,0,0],
                     [0,2,2,2,2,0,0,0],
                     [0,1,2,2,1,0,0,0],
                     [0,2,1,1,2,1,1,0]
                     ];
var bankstock:Array = [
                     [0,0,0,0,0,0,0,0],
                     [0,0,0,0,0,0,0,0],
                     [0,0,0,0,0,0,0,0],
                     [0,0,0,0,0,0,0,0],
                     [0,0,0,0,0,0,0,0],
                     [0,0,0,0,0,0,0,0],
                     [0,0,0,0,0,0,0,0],
                     [0,0,0,0,0,0,0,0]
                     ];

function colorBank():void {
    for (var i:int=0; i<8; i++){                                    // boucle sur les 20 colonnes
        for (var j:int=0; j<8; j++){                                // boucle sur les 15 lignes de chaque colonne
            var f:int = bankArray[j][i]                             // valeur de la case

            if(f > 0)
            {                                                       // si la valeur de la case est supérieure à 0
                var tBank:TBank = new TBank()
                bankList.push(tBank);
                // création du modèle
                tBank.x = 320+i*20;                                     // position sur X
                tBank.y = 100+j*20;                                     // position sur Y
                tBank.gotoAndStop(f);
                bankgrille.addChild(tBank); 

            }

            else 
            {
                bankstock[j][i] = []                                
            }

        }
    }
}
colorBank();
addChild(bankgrille);

我未来的项目(也许让我的问题更有意义): '之后,我想允许拖动方块并将它们放在另一个阵列上以改变第二种颜色'

2 个答案:

答案 0 :(得分:1)

在拖动影片剪辑时,

startDrag()是一个很好的起点。

您可以为整个网格分配MouseEvent侦听器,并使用event.currentTarget拖动特定元素:

bankgrille.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
bankgrille.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);

function onMouseDown(e:MouseEvent):void
{
  e.currentTarget.startDrag();
}

function onMouseUp(e:MouseEvent):void
{
  e.currentTarget.stopDrag();
}

答案 1 :(得分:0)

和Voilà!再次感谢Sim!

var bankgrille:MovieClip = new MovieClip();
var bankList:Vector.<TBank> = new Vector.<TBank>();

var bankArray:Array = [
                     [0,0,0,0,0,0,0,0],
                     [0,0,0,0,0,0,0,0],
                     [0,0,0,0,0,0,0,0],
                     [0,0,1,0,0,0,0,0],
                     [0,0,2,0,1,0,0,0],
                     [0,2,2,2,2,0,0,0],
                     [0,1,2,2,1,0,0,0],
                     [0,2,1,1,2,1,1,0]
                     ];
var bankstock:Array = [
                     [0,0,0,0,0,0,0,0],
                     [0,0,0,0,0,0,0,0],
                     [0,0,0,0,0,0,0,0],
                     [0,0,0,0,0,0,0,0],
                     [0,0,0,0,0,0,0,0],
                     [0,0,0,0,0,0,0,0],
                     [0,0,0,0,0,0,0,0],
                     [0,0,0,0,0,0,0,0]
                     ];

function colorBank():void {
    for (var i:int=0; i<8; i++){                                    // boucle sur les 20 colonnes
        for (var j:int=0; j<8; j++){                                // boucle sur les 15 lignes de chaque colonne
            var f:int = bankArray[j][i]                             // valeur de la case

            if(f > 0)
            {                                                       // si la valeur de la case est supérieure à 0
                var tBank:TBank = new TBank()
                bankList.push(tBank);
                // création du modèle
                tBank.x = 320+i*20;                                     // position sur X
                tBank.y = 100+j*20;                                     // position sur Y
                tBank.gotoAndStop(f);
                bankgrille.addChild(tBank); 

            }

            else 
            {
                bankstock[j][i] = []                                
            }

        }
    }
}
colorBank();
addChild(bankgrille);

draggable();
function draggable()
{
    for each(var t:TBank in bankList)
    {
t.addEventListener(MouseEvent.MOUSE_DOWN, clik);
t.addEventListener(MouseEvent.MOUSE_UP, lache);
var posX=t.x;
  if (t.x != posX)
  {
      t.x = posX;
  }
function clik(e:MouseEvent):void
{
  e.currentTarget.startDrag();
}

function lache(e:MouseEvent):void
{
  e.currentTarget.stopDrag();  
}
}
}