开发者

Color under a point or from a SHAPE

开发者 https://www.devze.com 2023-02-17 16:49 出处:网络
I am using flash as3. I have some shapes I have drawn on the screen.They are made using: graphics.beginFill(0x00ff00);

I am using flash as3.

I have some shapes I have drawn on the screen. They are made using:

graphics.beginFill(0x00ff00);
graphics.drawRect(0,0,开发者_运维知识库50,50);
graphics.endFill();

to draw them. Each shape has a different color for fun (different beginFill).

How can I get the color of the shape under the mouse pointer as I move the mouse around the shapes?

I see two choices so far but don't know how to do either of them:

Get color under the mouse point get the color from the shape properties (preferred)

I have no idea how to do either of these...Any tips?


s1 = instancename of the movieclip holding your first shape
s2 = instancename of the movieclip holding your second shape
s3 = instancename of the movieclip holding your third shape

(must be different movieclips)

and so on...

So you'll need to change those values.

If you need any explanation just drop a comment and i'll help you

otherwise you can simply copy/paste this code:

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

var myColor:String;

s1.addEventListener(MouseEvent.MOUSE_OVER, storeColor1); s2.addEventListener(MouseEvent.MOUSE_OVER, storeColor2); s3.addEventListener(MouseEvent.MOUSE_OVER, storeColor3);

function storeColor1(e:MouseEvent):void { var BmD:BitmapData = new BitmapData(s1.width, s1.height); var Bm:Bitmap = new Bitmap(BmD); BmD.draw(s1); s1.addChild(Bm); myColor = BmD.getPixel(s1.mouseX,s1.mouseY).toString(16);

while(myColor.length < 6)
{
    myColor = "0" + myColor;
}

trace(myColor);}

function storeColor2(e:MouseEvent):void { var BmD:BitmapData = new BitmapData(s2.width, s2.height); var Bm:Bitmap = new Bitmap(BmD); BmD.draw(s2); s2.addChild(Bm); myColor = BmD.getPixel(s2.mouseX,s2.mouseY).toString(16);

while(myColor.length < 6)
{
    myColor = "0" + myColor;
}

trace(myColor);}

function storeColor3(e:MouseEvent):void { var BmD:BitmapData = new BitmapData(s3.width, s3.height); var Bm:Bitmap = new Bitmap(BmD); BmD.draw(s3); s3.addChild(Bm); myColor = BmD.getPixel(s3.mouseX,s3.mouseY).toString(16);

while(myColor.length < 6) { myColor = "0" + myColor; } trace(myColor);}

Btw, this is just some code i wrote in 5 minutes to give you an idea of how it works. This is not an optimized code at all.


Easiest way to go is probably to create your own class, so you can keep your color (even random) in a variable, and handle different mouse events (use of graphics need an extends from MovieClip anyway) inside of the same class, or instead of handling the mouse itslef, dispatch your own event when mouse is over your sprite.

Another way is to draw your rects into a bitmap object, and then get pixel color via mouse position.

Maybe you can give more details on what you want to achieve (maybe you want to avoid using a class)


You can get the BitmapData for your area, and then use the getPixel method, which will return the color of that pixel.

0

精彩评论

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