[英]AS3: Pick color from object
I make some object and colored with Color Transform
. 我做一些物体,并用Color Transform
着色。 Here's my code: 这是我的代码:
function createColorItems():void
{
for (var i:int = 0; i < colorLength; i++)
{
var myColor:Object = new colorArea ;
var colorTrans:ColorTransform = new ColorTransform ;
arrColorTrans[i] = myXML.bag.color.item[i];
arrItem.push(myColor);
arrItem[i].x = 40 * i + 40;
arrItem[i].y = 300;
addChild(arrItem[i]);
colorTrans.color = Number(arrColorTrans[i]);
arrItem[i].transform.colorTransform = colorTrans;
arrItem[i].addEventListener(MouseEvent.CLICK,changeColor);
}
}
This is where i change the color. 这是我改变颜色的地方。
function changeColor():void
{
trace(e.target.color);
myBox.graphics.beginFill(0x000000,0.5);
myBox.graphics.drawRect(myImg.x,myImg.y,bagImg.width,bagImg.height);
myBox.graphics.endFill();
myBox.transform.colorTransform = publicColor;
addChild(myBox);
}
what i want is when the object is clicked, the other object's color is change. 我想要的是单击对象时,另一个对象的颜色改变了。 I trace it with trace(e.target.color)
but it's wrong. 我用trace(e.target.color)
跟踪它,但这是错误的。 i use publicColor
to pick the color from colorTrans
, but i don't know how to pick the color? 我使用publicColor
从colorTrans
选择颜色,但是我不知道如何选择颜色? is it possible?? 可能吗??
Sorry for my bad grammar, please help. 对不起,我的语法不好,请帮忙。
You can use getPixel
method of BitmapData. 您可以使用BitmapData的getPixel
方法。 Here is a documentation. 这是一个文档。 Example of using: 使用示例:
import flash.events.MouseEvent;
import flash.display.DisplayObject;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Matrix;
import flash.display.Sprite;
var s0:Sprite = new Sprite();
var s1:Sprite = new Sprite();
var testS:Sprite = new Sprite();
var bmd:BitmapData = new BitmapData(1,1);
s0.graphics.beginFill(0x010FFF);
s0.graphics.drawRect(0, 0, 100, 100);
s0.graphics.endFill();
s0.x = 200;
s0.y = 200;
s1.graphics.beginFill(0x555FFF);
s1.graphics.drawRect(0, 0, 100, 100);
s1.graphics.endFill();
s1.x = 300;
s1.y = 200;
addChild(s0);
addChild(s1);
addChild(testS);
addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void{
const clicked:DisplayObject = event.target as DisplayObject;
bmd.fillRect(bmd.rect, 0x000000);
bmd.draw(clicked, new Matrix(1, 0, 0, 1, clicked.x - mouseX, clicked.y - mouseY));
const color:uint = bmd.getPixel(0, 0);
testS.graphics.beginFill(color);
testS.graphics.drawRect(0, 0, 100, 100);
testS.graphics.endFill();
trace("color:" + color);
}
BitmapData.getPixel(x:int, y:int):uint
or 要么
BitmapData.getPixel32(x:int, y:int):uint
Returns an ARGB color value that contains alpha channel data and RGB data. 返回包含Alpha通道数据和RGB数据的ARGB颜色值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.