以到达一个鼠标点击在AS3的颜色最好的方法 [英] Best way to get the color where a mouse was clicked in AS3

查看:164
本文介绍了以到达一个鼠标点击在AS3的颜色最好的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形象(MX),我想被点击的像素的UINT。

I have an image (mx) and i want to get the uint of the pixel that was clicked.

任何想法?

推荐答案

下面是一个更简单的实现。所有你要做的就是使用画()的位图数据的方法,然后使用与getPixel()在鼠标下的像素搭舞台的快照。这样做的好处是,你可以品尝到任何的被吸引到舞台上,而不仅仅是一个给定的位图。

Here's an even simpler implementation. All you do is take a snapshot of the stage using the draw() method of bitmapData, then use getPixel() on the pixel under the mouse. The advantage of this is that you can sample anything that's been drawn to the stage, not just a given bitmap.

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

stage.addEventListener(MouseEvent.CLICK, getColorSample);

function getColorSample(e:MouseEvent):void {
    var bd:BitmapData = new BitmapData(stage.width, stage.height);
    bd.draw(stage);
    var b:Bitmap = new Bitmap(bd);
    trace(b.bitmapData.getPixel(stage.mouseX,stage.mouseX));
}

希望这是有帮助的!

Hope this is helpful!

修改

该编辑的版本使用单个的BitmapData ,并删除创建一个位图的不必要的步骤。如果你在采样 MOUSE_MOVE颜色那么这是必要的,以避免内存问题。

This edited version uses a single BitmapData, and removes the unnecessary step of creating a Bitmap. If you're sampling the color on MOUSE_MOVE then this is essential to avoid memory issues.

注:如果您使用的是自定义光标精灵,你将不得不使用对象而不是国家,否则你会被抽样自定义精灵,而不是什么下它的颜色等

Note: if you're using a custom cursor sprite you'll have to use an object other than 'state' or else you'll be sampling the color of the custom sprite instead of what's under it.

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

private var _stageBitmap:BitmapData;

stage.addEventListener(MouseEvent.CLICK, getColorSample);

function getColorSample(e:MouseEvent):void 
{
    if (_stageBitmap == null) {
        _stageBitmap = new BitmapData(stage.width, stage.height);
    }
    _stageBitmap.draw(stage);

    var rgb:uint = _stageBitmap.getPixel(stage.mouseX,stage.mouseY);

    var red:int =  (rgb >> 16 & 0xff);
    var green:int =  (rgb >> 8 & 0xff);
    var blue:int =  (rgb & 0xff);

    trace(red + "," + green + "," + blue);
}

这篇关于以到达一个鼠标点击在AS3的颜色最好的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆