简体   繁体   English

Flex / ActionScript更改形状填充颜色

[英]Flex/ActionScript Change Shape Fill Color

Here is the code I have: 这是我的代码:

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;
}

The changeColor and changeColorBack functions work but not how I would like them to. changeColor和changeColorBack函数可以工作,但是我不希望它们如何工作。 They change the whole color of my Sprite including the line border(stroke) around the rectangle. 它们会更改我的Sprite的整个颜色,包括矩形周围的线条边框(描边)。 I want to change just the color inside the rectangle and maintain the rectangle's border. 我只想更改矩形内部的颜色并保持矩形的边框。 I don't see a property in the ColorTransform that allows me to specify the lineStyle so is there a different approach to changing the fill color of my rectagle and maintain the border around it? 我在ColorTransform中没有看到允许我指定lineStyle的属性,所以有没有其他方法可以改变我的矩形的填充颜色并保持其边框?

ColorTransform applies to the whole MovieClip regardless of what's drawn in its graphics property. ColorTransform应用于整个MovieClip无论在其graphics属性中绘制了什么。 You could either redraw the rectangle each time you need to: 您可以在每次需要时重新绘制矩形:

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);

Or, if you're set on using ColorTransform for some reason, you could build your outlined rectangle out of two separate Sprites (an outer and an inner) and target the inner Sprite only with the ColorTransform : 或者,如果出于某种原因而设置使用ColorTransform ,则可以从两个单独的Sprites (外部和内部)构建轮廓矩形,并仅使用ColorTransform定位内部Sprite

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM