2

这是我的代码:

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 的属性,所以有没有不同的方法来更改我的矩形的填充颜色并保持它周围的边框?

4

1 回答 1

1

ColorTransformMovieClip无论在其图形属性中绘制什么,都适用于整体。您可以在每次需要时重新绘制矩形:

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的(外部和内部)构建您的轮廓矩形,并Sprite仅使用内部定位ColorTransform

import flash.display.Sprite;

function getRectangle(w:Number, h:Number, x:Number, y:Number, clr:Number):Sprite{
   var sprite:Sprite = new Sprite();
   sprite.name = "rectName";
   sprite.graphics.beginFill(clr);
   sprite.graphics.drawRect(x, y, w, h);
   sprite.graphics.endFill();

   return sprite;
}

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

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

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

// Outer rectangle for the outline
var outer:Sprite = getRectangle(8, 8, 0, 0, 0x000000);
rect.addChild(outer);

// Smaller inner rectangle which can be targeted with the color transform
var inner:Sprite = getRectangle(6, 6, 1, 1, 0xffff00);
rect.addChild(inner);
于 2012-11-21T23:38:15.233 回答