Search code examples
flashactionscriptadobe

Actionscript 3 sprite clear with alpha


I wanna clear sprite with 0.1 transparency, but sprite.graphics.clear() doesnt have that option. It needs to be cleared constantly every frame. How can I do that? i tried with filled rectangle on the whole stage with transparency 0.1, but my game is slowing down every frame.


Solution

  • May be you should put all the sprite into a Vector and you manualy remove them when you need.

        private var vec:Vector.<Sprite> = new Vector.<Sprite>();
    
        private function update():void
        {
            // reduce the alpha of all other sprites
            // check all other sprites from the newest to the oldest (so we can make a splice without getting an index error in case of there is other sprites to remove)
            for(var i:int = this.vec.length -1; i >=  0; i--)
            {
                this.vec[i].alpha -= 0.1;
    
                if(this.vec[i].alpha == 0)
                {
                    if(this.contains(this.vec[i]))
                    {
                        this.removeChild(this.vec[i]);
                    }
    
                    // we clear the sprite so it doesn't use any memory
                    this.vec[i].graphics.clear();
    
                    // remove the sprite from the vector
                    this.vec.splice(i,1);
                }
            }
    
            // create and draw the new sprite you want to add
            var addingSprite:Sprite = new Sprite();
            addingSprite.graphics.beginFill(0x005500);
            addingSprite.graphics.drawCircle(0, 0, 10);
    
            this.addChild(addingSprite);                    // add the sprite to the stage
    
            this.vec.push(addingSprite);                    // add the sprite to the vector so you can reduce his alpha easily
        }