我不知道为什么这个 for 循环只显示我数组中的最后一个精灵。我对 AS3 有点陌生,想知道它为什么这样做:
private function loadComplete(event:Event):void {
var image:Bitmap = Bitmap(loader.content);
for (var i:int = 0; i < 4; i++ ) {
var pB:BitmapData = new BitmapData(image.width / 2, image.height / 2, false, 0x000000FF);
pB.copyPixels(image.bitmapData, new Rectangle(i * 20 , i * 20, image.width / 2, image.height / 2), new Point(0, 0));
pieces.push(new Piece(new Sprite(), 0, 0));
pieces[i].getSprite().graphics.beginBitmapFill(new Bitmap(pB.clone()).bitmapData, null, false);
pieces[i].getSprite().graphics.drawRect(0, 0, image.width / 2, image.height / 2);
pieces[i].getSprite().graphics.endFill();
pieces[i].getSprite().addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
pieces[i].getSprite().addEventListener(Event.ENTER_FRAME, dragPiece);
addChild(pieces[i].getSprite());
}
stage.addEventListener(MouseEvent.MOUSE_UP, upHandler);
}
单品类:
public class Piece
{
private var pieceX:int;
private var pieceY:int;
private var sprite:Sprite;
public function Piece(sprite:Sprite, pieceX:int, pieceY:int):void
{
this.sprite = sprite;
this.pieceX = pieceX;
this.pieceY = pieceY;
}
public function getSprite():Sprite {
return sprite;
}
}