开发者

How to check a pixels color value and use it for mouse hit detection

I have a bitmap with many colors inside it. I want each color, when clicked, to preform a different method. How do I determine the pixel's color value and use it for a mouse event? I found bitmap hit detection but I cannot figure out how to use it (be开发者_开发问答cause I have many colors inside the bitmap). Any help is greatly appreciated because I am officially stumped.


you can use this method Bitmapdata.getPixel() or BitmapData.getPixel32()

import flash.display.BitmapData;

var bmd:BitmapData = new BitmapData(80, 40, false, 0xFF0000);

var pixelValue:uint = bmd.getPixel(1, 1);
trace(pixelValue.toString(16)); // ff0000;


BitmapClass is DisplayObject. if you want add Mouse or KeyboardEvent is Must be sthClass is InteractiveObject Class.

So, one invisible Container must be Making... because Sprite is InteractiveObject.

import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.MouseEvent;
import flash.display.Sprite;

var container:Sprite = new Sprite();
var bmd:BitmapData = new BitmapData(400, 400, false, 0xFFFFFF * Math.random());
var bmp:Bitmap = new Bitmap(bmd);

this.addChild(container);
container.addChild(bmp);

container.addEventListener(MouseEvent.CLICK, onClick);

function onClick(e:MouseEvent):void
{
    var obj:Sprite = e.currentTarget as Sprite;

    var bmp:Bitmap = Bitmap(obj.getChildAt(0));

    var pixelValue:uint = bmp.bitmapData.getPixel(mouseX,mouseY);

    trace(pixelValue.toString(16));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜