AS3拖放到一个单个对象

时间:2011-03-03 10:54:23

标签: actionscript-3

(我在AS不擅长所以请你好:) :)

我正在开发简单的Flash游戏,其中有一个大圆圈外面有8个小圆圈。我想将这8个小圆逐个拖到大圆圈。

基本上是1盘用他们的食物。他们的食物项目他的盘子。我希望这会给你一个更好的主意。 我有同样的事情,但无法得到我想要的东西:(

请建议我使用脚本或方法。

如果有任何链接或啧啧请告诉我,我真的很感激。

谢谢,

Kunal(网站设计师)

1 个答案:

答案 0 :(得分:0)

此代码已经过测试并且有效。 DummyCircle 类只是在实例化时为其图形对象绘制一个居中的圆圈。

private var plate:Sprite;
private var stage:Stage;

public function execute(stage:Stage):void 
{
    this.stage = stage;
    // number of pieces around the centered plate
    const numPieces:int = 8;
    const plateRadius:int = 50;
    plate = new DummyCircle(plateRadius);
    // center plate on the stage
    plate.x = stage.stageWidth / 2;
    plate.y = stage.stageHeight / 2;
    stage.addChild(plate);
    // for each piece to be created
    for (var i:int = 0; i < numPieces; i++) {
        // instantiate the appropriate sprite, here with a radius argument
        var piece:Sprite = new DummyCircle(plateRadius / numPieces);
        // add event listener for dragging
        piece.addEventListener(MouseEvent.MOUSE_DOWN, mouseListener);
        piece.addEventListener(MouseEvent.MOUSE_UP, mouseListener);
        // pieces are in the top left corner of the stage, plate is centered
        piece.x = 0;
        piece.y = 0;
        // a transformation matrix is used for positioning the pieces
        // get the current matrix
        var pieceMatrix:Matrix = piece.transform.matrix;
        // move a bit more than the plate radius on the y axis
        pieceMatrix.translate(0, -plateRadius * 1.5);
        // rotate around the origin
        pieceMatrix.rotate(i * (2 * Math.PI / numPieces));
        // move again, this time to our plate
        pieceMatrix.translate(plate.x, plate.y);
        // apply the matrix
        piece.transform.matrix = pieceMatrix;
        stage.addChild(piece);
    }
}

private function mouseListener(e:MouseEvent):void 
{
    if (e.target is Sprite) {
        var target:Sprite = e.target as Sprite;
        if (e.type == MouseEvent.MOUSE_UP) {
            target.stopDrag();
            if (target.hitTestObject(plate)) {
                stage.removeChild(target);
            }
        }
        else if (e.type == MouseEvent.MOUSE_DOWN) {
            target.startDrag();
        }
    }
}