views:

18

answers:

1

I have this code

public function TalentBox(x:int, y:int, arg_color:int = 0xFFFFFF):void
{
    this.graphics.beginFill(arg_color);
    this.graphics.lineStyle(1.0, 0x000000, 0.7);
    this.graphics.drawRect(0, 0, 7, 13);
    this.alpha = 1.0;
    this.x = x;
    this.y = y;
    this.graphics.endFill();
}

Where I construct the class (that extends from sprite). Then I need to have a function that changes the color of the sprite. Currently I have this

public function setColor(arg_color:int):void
{
    color = arg_color;

    this.graphics.beginFill(color);
    this.graphics.drawRect(0, 0, 7, 13);
    this.graphics.endFill();
}

And It seems to work but this is creating a new rect. Which I do not want.

And I have tried ColorTransform, and that changes everything, even the border, which is not what I wanted. And I am not able to colortransform and then set the border color.

So how can I change the color of a sprite without changing the border color?

A: 

I have found the answer.

You create two sprites in the class. The body and the border. Set those individually and then change the color with transform only on the body sprite.

Here is the modified constructor

public function TalentBox(x:int, y:int, arg_color:int = 0xFFFFFF):void
{
    body.graphics.beginFill(arg_color);
    body.graphics.drawRect(x + 1, y + 1, 6, 12);
    body.graphics.endFill();

    border.graphics.beginFill(0xFFFFFF);
    border.graphics.lineStyle(1.0, 0x000000, 0.7);
    border.graphics.drawRect(x, y, 7, 13);
    border.graphics.endFill();

    this.addChild(border);
    this.addChild(body);
}
Ólafur Waage