as3如何从重置阵列中移除/移动图形的位置

时间:2013-03-28 22:17:13

标签: arrays actionscript-3 graphics for-loop

我正在尝试重置场景,将每个东西移动到其原始位置,重置功能重置阵列将颈背体添加回舞台并附加图形,但原始图形仍处于舞台上的任何位置在调用重置时

private var brickGraphic:MovieClip = new Brick();
private var brick:Body;
private var brickArray:Array;

private function setUp():void
{
    brickArray = new Array  ;

    for (var i:int = 0; i < 10; i++)
    {
        var brick:Body = new Body(BodyType.DYNAMIC);
        var brickShape:Polygon = new Polygon(Polygon.box(10,25));
        var brickGraphic = new Brick();
        brickGraphic.width = 10;
        brickGraphic.height = 25;
        addChild(brickGraphic);
        brickGraphic.cacheAsBitmap = true;
        brick.shapes.add(brickShape);
        brick.position.setxy(450, ((ag ) - 30 * (i + 0.5)));
        brick.angularVel = 0;
        brick.shapes.at(0).material.elasticity = .5;
        brick.shapes.at(0).material.density = 150;
        brick.cbTypes.add(brickType);
        brick.space = space;
        brickGraphic.stop();
        brick.userData.sprite = brickGraphic;
        brick.userData.sprite.x = brick.position.x;
        this.brickArray.push(brick);
    }

    private function reset():void
    {
        if (contains(brickGraphic)) removeChild(brickGraphic);
        space.clear();
        setUp();
    }
}

这是我在这个应用程序上的最后一个问题,非常感谢您的帮助

1 个答案:

答案 0 :(得分:1)

那是因为您没有使用removeChild删除它们。

您需要为添加到舞台的每个removeChild对象调用brickGraphic

类似的东西:

private function setUp():void
{
    brickArray = [];

    for (var i:int = 0; i < 10; i++)
    {
        var brick:Body = new Body(BodyType.DYNAMIC);
        var brickShape:Polygon = new Polygon(Polygon.box(10,25));
        var brickGraphic = new Brick();
        brickGraphic.width = 10;
        brickGraphic.height = 25;
        addChild(brickGraphic);
        brickGraphic.cacheAsBitmap = true;
        brick.shapes.add(brickShape);
        brick.position.setxy(450, ((ag ) - 30 * (i + 0.5)));
        brick.angularVel = 0;
        brick.shapes.at(0).material.elasticity = .5;
        brick.shapes.at(0).material.density = 150;
        brick.cbTypes.add(brickType);
        brick.space = space;
        brickGraphic.stop();
        brick.userData.sprite = brickGraphic;
        brick.userData.sprite.x = brick.position.x;
        this.brickArray.push(brick);
    }


}

private function removeAllBricks():void
{
    for(var i:int=0; i<brickArray.length; i++)
    {
       var dp:DisplayObject = brickArray[i].userData.sprite as DisplayObject;
       if(dp && dp.parent)
           dp.parent.removeChild(dp);
    }
}

private function reset():void
{
    removeAllBricks();
    space.clear();
    setUp();
}