views:

44

answers:

3

i've programatically created a vector graphic (rect), repositioned the graphic, and set up an MOUSE_MOVE eventListener to trace color information of the graphic using getPixel(). however, the bitmapData is placed at 0,0 of the stage and i don't know how to move it so that it matches the graphic's location.

var coloredSquare:Sprite = new GradientRect(200, 200, 0xFFFFFF, 0x000000, 0xFF0000, 0xFFFF00);
coloredSquare.x = 100;

addChild(coloredSquare);

var coloredSquareBitmap:BitmapData = new BitmapData(coloredSquare.width, coloredSquare.height, true, 0);
coloredSquareBitmap.draw(coloredSquare);

coloredSquare.addEventListener(MouseEvent.MOUSE_MOVE, readColor);
function readColor(evt:Event):void
    {
    var pixelValue:uint = coloredSquare.getPixel(mouseX, mouseY);
    trace(pixelValue.toString(16));
    }
+2  A: 

Use the square's transformation matrix(which contains the translation as well) as the second parameter of the draw() method

e.g.

coloredSquareBitmap.draw(coloredSquare,coloredSquare.transform.matrix);

HTH, George

George Profenza
hi george. i had tried that but the mouse event is lost on the right half of the coloredSquare. it seems to work only for the left half.
TheDarkInI1978
+1. although `concatenatedMatrix` is better, since it works in all cases, an not just if the parent has no transformation applied.
back2dos
@back2dos thanks for pointing that!
George Profenza
oh i see my mistake now. you did actually answer the question but my mouse event was causing errors. thanks.
TheDarkInI1978
+1  A: 

Use

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

That way the mouseX / mouseY will be local to the coloured square and thus the bitmap duplicate .

quoo
thanks. this worked. :-)
TheDarkInI1978
+2  A: 

I do not fully understand the problem or the code. Maybe this helps:

coloredSquareBitmap.draw(coloredSquare, coloredSquare.transform.concatenatedMatrix);

greetz
back2dos

back2dos