views:

379

answers:

2

Pretty basic question here, but its still got me a little confused..

I have an object(navigation menu bar) that I want to change the colors on with code, so in an updateColor function, I get the bounds of the object (which is a drawing shape contained in a movieclip) and redraw a new shape on top of it with the new color, but I've noticed that the last shape still exists behind this redraw.

I tried using obj.graphics.clear(); before the redraw but that didn't get rid of the original shape. Is there another command that I'm overlooking?

A: 

Unless you drew the object you wish to remove within the same graphics object, clearing won't work. You need to remove the DisplayObject.

Depending on the number of children you can do:

obj.removeChildAt(0);

This also removes movieclips / buttons you placed on the stage manually. If you have a reference to the DisplayObject you wish to remove you can simply do

obj.removeChild(backgroundClip);

Note that you can also change the color of a DisplayObject directly:

import flash.geom.ColorTransform;
...
public var test:MovieClip; //instance on stage
...

var cf:ColorTransform = test.transform.colorTransform;
cf.color = 0xff0000;
test.transform.colorTransform =  cf;
Les
A: 

while(this.numChildren) { this.removeChildAt(0); }

Will clear child object on this MovieClip, if it's clearing too much, then put the shape drawing in a subclip, and clear the subclip.

JTtheGeek