Mitchell Ingram
Mitchell Ingram

Reputation: 706

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 (because I have many colors inside the bitmap). Any help is greatly appreciated because I am officially stumped.

Upvotes: 0

Views: 3097

Answers (2)

bitmapdata.com
bitmapdata.com

Reputation: 9600

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

Upvotes: 1

bitmapdata.com
bitmapdata.com

Reputation: 9600

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;

Upvotes: 3

Related Questions