AS3:从对象中选择颜色

时间:2012-08-13 05:14:30

标签: actionscript-3 colortransform

我制作了一些物体并用Color Transform着色。这是我的代码:

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

这是我改变颜色的地方。

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

我想要的是当点击对象时,另一个对象的颜色会发生变化。我用trace(e.target.color)跟踪它,但这是错误的。我使用publicColorcolorTrans中选择颜色,但我不知道如何选择颜色?有可能??

抱歉我的语法不好,请帮帮忙。

2 个答案:

答案 0 :(得分:0)

您可以使用BitmapData的getPixel方法。 Here是一份文档。使用示例:

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

答案 1 :(得分:0)

BitmapData.getPixel(x:int, y:int):uint

BitmapData.getPixel32(x:int, y:int):uint

返回包含Alpha通道数据和RGB数据的ARGB颜色值。