AS3 Flash - 根据鼠标位置更改鼠标移动时的对象颜色

AS3 Flash - Change an object's color on mouse move based off of mouse position

我有一个对象,我需要根据鼠标在其上移动时的 x 和 y 位置更改其颜色。我如何在代码中执行此操作?

我可以逐帧做,放不同的按钮,鼠标悬停在按钮上,我们可以转到其他帧,但这种方法很累人。有没有更简单的方法?

我想要的是将图像分成彩色的行和列,并突出显示鼠标悬停的列和行。

我是如何解释这个问题的:

You need to change the color of an object based off the mouse's x & y position relative to that object

如果是这样的话,这里有一个例子:

//listen for the mouse move event
object.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler):

function mouseMoveHandler(e:MouseEvent){

    //make a reference to the object that dispatched this event
    var object:DisplayObject = e.currentTarget as DisplayObject;

    //do your logic to determine the color based off the mouse position.


    //get the percentage of the mouse postion from 0 - 1
    var xPer:Number = e.localX / object.width;
    var yPer:Number = e.localY / object.height; 


    //One way you could do this, is change the Red/Green/Blue based of the mouse position like so:
    //create a color transformation object
    var colorTransform = new ColorTransform(1,1,1,1,0xFF * xPer,0xFF * xPer,0xFF * yPer,1);



    //OR, you could do it quadrant based like this:
    var color:uint = 0; //black default
    if(xPer >= .5 && yPer >= .5){
        //if the mouse is on the right bottom side of the object
        color = 0xFF0000; //make it red
    }
    if(xPer < .5 && yPer >=.5){
        //if the mosue is on the left bottom of the object
        color = 0x00FF00; //make it green
    }
    if(xPer >= .5 && yPer < .5){
        //if mouse is on the top right side
        color = 0x0000FF; //make it blue
    }
    if(xPer < .5 && yPer < .5){
        //if mouse is on the top left side
        color = 0xFF00FF; //make it purple
    }
    var colorTransformation = object.transform.colorTransform;
    colorTransform.color = color;




    //assign it to the object
    object.transform.colorTransform = colorTransform;
}

编辑

根据您的评论,听起来您想要这样的东西。

假定您在舞台上有一个名为 obj 的 MovieClip,其中最底部的子项是您要使用的位图。

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

//get a reference to the actual bitmap
//this assumes the bitmap is the bottom most child of the MovieClip container called obj
var bitmap:Bitmap = Bitmap(obj.getChildAt(0));
var bitmapData:BitmapData = bitmap.bitmapData;

//store the original pixel data
var originalPixels:ByteArray = bitmapData.getPixels(bitmap.getBounds(bitmap));


var rows:int = 3;
var rowHeight:Number = Math.ceil(bitmap.height / rows);
var curRow:int = -1;

var cols:int = 3;
var colWidth:int = Math.ceil(bitmap.width / cols);
var curCol:int = -1;

//listen for the mouse move event
obj.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
obj.addEventListener(MouseEvent.MOUSE_OUT, restoreBitmap);

function restoreBitmap(e:Event = null):void {
    //restore original
    if(e){
        //if this function was triggered from the mouse event
        curRow = -1;
        curCol = -1;
    }
    originalPixels.position = 0;
    bitmapData.setPixels(bitmap.getBounds(bitmap),originalPixels);
}

function mouseMoveHandler(e:MouseEvent){
    //figure out what row the mouse is in and make the row rectangle
    var row:int = Math.floor(bitmap.y + e.localY / rowHeight);
    var col:int = Math.floor(bitmap.x + e.localX / colWidth);

    var rect:Rectangle;

    //if either the row or column has changed, restore the pixels to original and draw new ones
    if(row != curRow || col != curCol){
        restoreBitmap();

        curCol = col;
        rect = new Rectangle(curCol * colWidth, 0, colWidth, bitmap.height);

        colorRect(bitmapData, rect,0x0000FF);

        curRow = row;
        rect = new Rectangle(0, row * rowHeight, bitmap.width, rowHeight);

        colorRect(bitmapData, rect,0x00FF00);
    }
}

function colorRect(bmd:BitmapData, rect:Rectangle, color:uint) {
    var row:int = 0;
    var col:int = 0;

    while(row * col <= rect.width * rect.height){
        bmd.setPixel(int(rect.x) + col, int(rect.y) + row, color);

        //incriment the row/column
        if (col < rect.width -1) {
            col++;
        }else {
            row++
            col = 0;
        }
    }
}

请记住,FlashPro 会将位图拖到时间线上 Shape,除非您转到位图的属性并告诉它 导出动作脚本

另外,位图无法缩放。对包含位图的 container/MovieClip 进行任何缩放。


这是相同的结果,但使用遮罩后,您可能会发现此方法的速度有多快:

//get a reference to the actual bitmap
//this assumes the bitmap is the bottom most child of the MovieClip container called obj
var bitmap:Bitmap = Bitmap(obj.getChildAt(0));
var bitmapData:BitmapData = bitmap.bitmapData;

//clone it twice and add the clones to container (obj)
//also add mask objects
var cloneCol:Bitmap = new Bitmap(bitmapData);
var cloneColMask:Shape = new Shape();
cloneCol.mask = cloneColMask;
obj.addChild(cloneCol);
obj.addChild(cloneColMask);

//tint the clones
var ct:ColorTransform = cloneCol.transform.colorTransform;
ct.color = 0x00FF00;
cloneCol.transform.colorTransform = ct;


var cloneRow:Bitmap = new Bitmap(bitmapData);
var cloneRowMask:Shape = new Shape();
cloneRow.mask = cloneRowMask;
obj.addChild(cloneRow);
obj.addChild(cloneRowMask);

ct.color = 0x0000FF;
cloneRow.transform.colorTransform = ct;


//setup your columns and rows

var rows:int = 3;
var rowHeight:Number = Math.ceil(bitmap.height / rows);
var curRow:int = -1;

var cols:int = 3;
var colWidth:int = Math.ceil(bitmap.width / cols);
var curCol:int = -1;

//listen for the mouse move event
obj.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
obj.addEventListener(MouseEvent.MOUSE_OUT, restoreBitmap);

function restoreBitmap(e:Event = null):void {
    cloneRowMask.graphics.clear();
    cloneColMask.graphics.clear();
}

function mouseMoveHandler(e:MouseEvent){
    //figure out what row the mouse is in and make the row rectangle
    var row:int = Math.floor(bitmap.y + e.localY / rowHeight);
    var col:int = Math.floor(bitmap.x + e.localX / colWidth);

    var rect:Rectangle;

    if(row != curRow || col != curCol){
        curCol = col;
        cloneColMask.graphics.clear();
        cloneColMask.graphics.beginFill(0);
        cloneColMask.graphics.drawRect(curCol * colWidth, 0, colWidth, bitmap.height);

        curRow = row;
        cloneRowMask.graphics.clear();
        cloneRowMask.graphics.beginFill(0);
        cloneRowMask.graphics.drawRect(0, row * rowHeight, bitmap.width, rowHeight);
    }
}

非常感谢 我也做了其他解决方案。我将图像分成不同的小部分,每个部分都做了一个按钮,然后当鼠标悬停在按钮上时我更改了每个按钮的颜色