Flex / ActionScript更改形状填充颜色

时间:2012-11-21 21:51:43

标签: actionscript colortransform

这是我的代码:

private function drawRect():Sprite{
   var rect:Sprite = new Sprite();
   rect.name = "rectName";
   rect.graphics.beginFill(0xffff00);
   rect.graphics.lineStyle(1,0x000000);
   rect.graphics.drawRect(0,0,6,6);
   rect.graphics.endFill();
   rect.addEventListener(MouseEvent.MOUSE_OVER, changeColor);
   rect.addEventListener(MouseEvent.MOUSE_OUT, changeColorBack);
   return rect;
}

private function changeColor(e:MouseEvent):void{
   var newColor:ColorTransform = new ColorTransform();
   newColor.color = 0x00ffff;
   e.target.transform.colorTransform = newColor;
}

private function changeColorBack(e:MouseEvent):void{
   var newColor:ColorTransform = new ColorTransform();
   newColor.color = 0xffff00;
   e.target.transform.colorTransform = newColor;
}

changeColor和changeColorBack函数可以工作,但不是我希望它们如何工作。它们改变了我的Sprite的整个颜色,包括矩形周围的线条边框(笔触)。我想只改变矩形内的颜色并保持矩形的边框。我没有在ColorTransform中看到允许我指定lineStyle的属性,所以有不同的方法来改变我的rectagle的填充颜色并保持它周围的边框?

1 个答案:

答案 0 :(得分:1)

ColorTransform适用于整个MovieClip,无论其图形属性中绘制了什么。您可以在每次需要时重绘矩形:

import flash.display.Sprite;

function drawRect(target:Sprite, clr:Number):void{
   target.graphics.clear();
   target.graphics.beginFill(clr);
   target.graphics.lineStyle(1,0x000000);
   target.graphics.drawRect(0,0,6,6);
   target.graphics.endFill();
}

function changeColor(e:MouseEvent):void{
   drawRect(Sprite(e.target), 0x00ffff);
}

function changeColorBack(e:MouseEvent):void{
   drawRect(Sprite(e.target), 0xffff00);
}

var rect:Sprite = new Sprite();
rect.addEventListener(MouseEvent.MOUSE_OVER, changeColor);
rect.addEventListener(MouseEvent.MOUSE_OUT, changeColorBack);
this.addChild(rect);

drawRect(rect, 0xffff00);

或者,如果您出于某种原因开始使用ColorTransform,则可以使用两个单独的Sprites(外部和内部)构建轮廓矩形并定位内部{{1只有Sprite

ColorTransform