Actionscript 3 在Flash AS3中删除一个电影剪辑并替换为另一个?

Actionscript 3 在Flash AS3中删除一个电影剪辑并替换为另一个?,actionscript-3,textures,movieclip,Actionscript 3,Textures,Movieclip,我有一个简单的代码,删除一个MovieClip,然后给对象分配一个新的MovieClip。出于某种原因,它将旧剪辑重新添加到新剪辑下面。如果删除旧剪辑,则无法添加第二个剪辑 public function addAnimation(clip:Class):void { _texture.parent.removeChild(_texture); var effect:MovieClip = new clip();

我有一个简单的代码,删除一个MovieClip,然后给对象分配一个新的MovieClip。出于某种原因,它将旧剪辑重新添加到新剪辑下面。如果删除旧剪辑,则无法添加第二个剪辑

public function addAnimation(clip:Class):void 
        {

            _texture.parent.removeChild(_texture);

            var effect:MovieClip = new clip();

            texture.addChild(effect as MovieClip);

            //sync up points
            _texture.x = _body.GetPosition().x * Constants.PIXELS_TO_METRE;
            _texture.y = _body.GetPosition().y * Constants.PIXELS_TO_METRE;

            _texture = texture;
            addChild(texture);

            updateTexture();

        }

我想能够删除一个剪辑,然后分配给它一个新的。我尝试过各种变体,并对代码进行了重新排序,分解成不同的函数等,但我在这方面遇到了僵局。

您的代码示例不清楚:

  • 什么是可变纹理,什么是纹理
  • 无法从movieclip本身
    \u-texture.parent.removeChild(\u-texture)
  • 也许你的意思是:

    private var _texture: MovieClip;
    public function addAnimation(clip:Class):void 
    {
        // remove previously added texture
        if (_texture) {
            removeChild(_texture);
        }
    
        // create new texture
        _texture = new clip();
    
    
        //sync up points
         _texture.x = _body.GetPosition().x * Constants.PIXELS_TO_METRE;
         _texture.y = _body.GetPosition().y * Constants.PIXELS_TO_METRE;
    
         addChild(_texture);
         updateTexture();
    
     }
    

    据我所知,您正在向
    纹理添加新的子对象,而不删除旧的子对象。这应该可以解决问题

    var effect:MovieClip = new clip();
    texture.removeChildren(0, texture.numChildren - 1);
    texture.addChild(effect as MovieClip);
    

    什么是
    texture
    这里
    texture.addChild(效果为MovieClip)?似乎您在添加新剪辑时没有删除旧剪辑
    \u texture.parent.removeChild(\u texture)
    不是从自身移除
    \u纹理
    ,而是从其
    .parent
    移除,这很好。