I have the following class, that represents a red circle:
public class AElement extends UIComponent {
public var radius:int;
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
graphics.beginFill(0xFF0000);
graphics.drawCircle(x, y, radius);
graphics.endFill();
}
}
I would like to add a method that changes the color of the circle, so I came up with this solution:
public function updateColor(color:uint):void {
graphics.beginFill(color);
graphics.drawCircle(x, y, radius);
graphics.endFill();
}
It works, but I believe this only draws another circle on top of the first one.
Is there any way to change the first circle’s color instead of drawing another one ?
Just call .clear() before you start drawing
then you can redraw in a new colour.
Edit:
To change the colour of an object you can use ColorTransform:
Where r,g and b are the red,green and blue parts of the colour, and a is the alpha value (all between 0-255). eg:
or for colours without alpha:
However, this affects the whole display object and any children – not just whatever is drawn to graphics. So, assuming your class contains other visual objects, you’d be better sticking with the clear() option (imho).