Actionscript 从 Sprite 鼠标事件中检索位图数据
我创建了一个带有数据的位图并将其放入精灵中以便接收鼠标事件。但是,我正在努力读取精灵中的位图数据。
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;
}
i've created a bitmap with data and placed it into a sprite so to receive mouse events. however, i'm struggling with reading the BitmapData within the sprite.
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在尝试从
e
读取字段bmap
,该字段是MouseEvent
并且没有此类字段。此外,
Bitmap
没有名为 bData 的字段,只有bitmapData
字段。从精灵获取位图的一种方法是使用事件的目标并使用 getObjectsUnderPoint 获取位图(如果你的精灵中有多个位图)
也不要忘记从bmap中获取鼠标坐标,否则你将不得不使用Point使用 globalToLocal 和 LocalToGlobal
You are trying to read the field
bmap
frome
who is aMouseEvent
and don't have such field.Also the
Bitmap
has no field named bData butbitmapData
.One way to get the bitmap from the your sprite is to use the target of the event and use getObjectsUnderPoint to get the bitmap (in case you have multiple bitmap into your sprite)
Also don't forget to take the mouse coordinate from the bmap, otherway you will have to play with Point conversion using globalToLocal and LocalToGlobal
最简单的方法是使位图成为画布的属性,以便可以轻松地从画布中引用它。该事件是从画布对象触发的,因此
e.target
将成为您的画布。从那里,您可以点击位图,位图的 bitmapData 属性将引用您的位图数据。Easiest way is to make your bitmap a property of the canvas so it can easily be referenced from the canvas. The event is firing from the canvas object so
e.target
will be your canvas. From there, you can hit your bitmap, and the bitmapData property of your bitmap will reference your bitmap data.问题是“e”是一个事件,它没有 bmap 属性。它将有一个目标属性,但这将是一个事件调度程序,在本例中是您的画布。
我建议:
创建一个扩展精灵并包含位图的自定义类。
创建该类的一个实例并将其添加到舞台中。
将事件侦听器添加到该对象而不是舞台。
在您的事件侦听器中,检查 event.target 是否是您的自定义类的实例。
如果是这样,您可以使用事件的 localX 和 localY 来获取对象的位图属性的像素值。
The problem is that "e" is an event, which doesn't have a bmap property. It will have a target property, but that will be a event dispatcher, in this case your canvas.
I would suggest:
Create a custom class that extends sprite and contains your bitmap.
Create an instance of that class and add it to the stage.
Add your event listener to that object instead of the stage.
In your event listener check that event.target is an instance of your custom class.
If so, you can use the event's localX and localY to get the pixel value of the object's bitmap property.