Actionscript从Sprite鼠标事件中检索BitmapData

时间:2010-03-03 10:44:24

标签: actionscript-3 mouseevent bitmapdata

我已经创建了一个包含数据的位图并将其放入一个精灵中以便接收鼠标事件。但是,我正在努力阅读精灵中的BitmapData。

function showBitmapData(e:Event):void
    {
    var bData:BitmapData = new BitmapData(video.width, video.height);
    bData.draw(video);

    var bmap:Bitmap = new Bitmap(bData);
    bmap.x = 220;
    bmap.y = 20;
    bmap.scaleX = bmap.scaleY = 2;

    canvas = new Sprite;
    addChild(canvas);
    canvas.addChild(bmap);

    //Mouse Track Pixel Colors
    canvas.addEventListener(MouseEvent.CLICK, readPixel);
    }

function readPixel(e:MouseEvent):void
    {
    var hex:uint = e.bmap.bData.getPixel32(mouseX, mouseY); // <- is the problem?
    var pixelAlpha:int = (hex >>> 0x18) & 0xff;
    var red:int = (hex >>> 0x10) & 0xff;
    var green:int = (hex >>> 0x08) & 0xff;
    var blue:int = hex & 0xff;

    colorText.text = "Red:" + red + " Green:" + green + " Blue:" + blue + " Alpha:" + pixelAlpha;
    }

3 个答案:

答案 0 :(得分:1)

您正在尝试阅读来自bmap e并且没有此类字段的字段MouseEvent

此外,Bitmap没有名为bData的字段,但bitmapData

从精灵中获取位图的一种方法是使用事件的目标并使用getObjectsUnderPoint来获取位图(如果你的精灵中有多个位图)

另外请不要忘记从bmap中获取鼠标坐标,否则您必须使用globalToLocalLocalToGlobal进行点转换

// function to get the bitmap from a display object container
// using the mouse coordinate
function findBitmap(container:DisplayObjectContainer):Bitmap {
 if (container === null)
   return null;

 var childs:Array = container.getObjectsUnderPoint(
    new Point(container.mouseX, container.mouseY)
 );

 while (childs.length > 0) {
   var ret:Bitmap = childs.pop() as Bitmap;
   if (ret !== null)
     return ret;
 }

 return null;
}

// ....
canvas = new Sprite;
addChild(canvas);
canvas.addChild(bmap);
//Mouse Track Pixel Colors
canvas.addEventListener(MouseEvent.CLICK, readPixel);
// ...

function readPixel(e:MouseEvent):void {
    // found the bitmap from the currentTarget
    var bmap:Bitmap=findBitmap(e.currentTarget as DisplayObjectContainer);

    var hex:uint=0;

    if (bmap!==null) {
     hex = bmap.bitmapData.getPixel32(bmap.mouseX, bmap.mouseY); 
    }

    var pixelAlpha:int = (hex >>> 0x18) & 0xff;
    var red:int = (hex >>> 0x10) & 0xff;
    var green:int = (hex >>> 0x08) & 0xff;
    var blue:int = hex & 0xff;

    colorText.text =
         "Red:" + red + " Green:" + green + " Blue:" + blue + " Alpha:" + pixelAlpha;
    }

答案 1 :(得分:1)

最简单的方法是使您的位图成为画布的属性,以便可以从画布中轻松引用它。该事件从画布对象触发,因此e.target将成为您的画布。从那里,你可以点击你的位图,你的位图的bitmapData属性将引用你的位图数据。

function showBitmapData(e:Event):void
    {
    var bData:BitmapData = new BitmapData(video.width, video.height);
    bData.draw(video);

    var bmap:Bitmap = new Bitmap(bData);
    bmap.x = 220;
    bmap.y = 20;
    bmap.scaleX = bmap.scaleY = 2;

    canvas = new MovieClip(); //sprites can't have arbitrary properites
    addChild(canvas);
    canvas.bmap = bmap; //*** Look at me! I can be referenced later!
    canvas.addChild(bmap);

    //Mouse Track Pixel Colors
    canvas.addEventListener(MouseEvent.CLICK, readPixel);
    }

function readPixel(e:MouseEvent):void
    {

    var hex:uint = e.target.bmap.bitmapData.getPixel32(mouseX, mouseY); // e.target is your "canvas" from before
    var pixelAlpha:int = (hex >>> 0x18) & 0xff;
    var red:int = (hex >>> 0x10) & 0xff;
    var green:int = (hex >>> 0x08) & 0xff;
    var blue:int = hex & 0xff;

    colorText.text = "Red:" + red + " Green:" + green + " Blue:" + blue + " Alpha:" + pixelAlpha;
    }

答案 2 :(得分:0)

问题是“e”是一个没有bmap属性的事件。它将具有目标属性,但这将是一个事件调度程序,在本例中是您的画布。

我建议:

创建一个扩展sprite并包含位图的自定义类。 创建该类的实例并将其添加到舞台。 将事件侦听器添加到该对象而不是舞台。 在您的事件监听器中检查event.target是您的自定义类的实例。 如果是这样,您可以使用事件的localX和localY来获取对象的位图属性的像素值。