Skip to main content
Participant
May 9, 2014
Question

How to add Event to every single pixel with BitmapData?

  • May 9, 2014
  • 2 replies
  • 261 views

What I want to accomplish: when I drag the cursor over a sprite I want the sprite to break down and every single pixel moves through the screen at random x and y axis.

To make it simple the draw this is what I want but with much more fragments(pixels) around, thousands of them. How come could I approach this?

simple.png

Thanks in advance.

This topic has been closed for replies.

2 replies

Amy Blankenship
Legend
May 10, 2014

I think you're looking for MouseEvent.MOUSE_MOVE. Note there are lots of pixel manipulation examples at flashAndmath.com.

kglad
Community Expert
Community Expert
May 10, 2014

if mc is the object whose pixels you want to randomize and display in a bitmap:

var bmpd:BitmapData = new BitmapData(mc.width,mc.height);

bmpd.draw(mc);

var bmp:Bitmap = new Bitmap(bmpd);

addChild(bmp);

mc.visible=false;

mc.addEventListener(MouseEvent.MOUSE_OVER,randomizeF);

function randomizeF():void{

    var a:Array = [];

    for(var x:int=1;x<=bmpd.width;x++){

        for(var y:int=1;y<=bmpd.height;y++){

            a.push(bmpd.getPixel32(x,y));

        }

    }

    shuffle(a);

    var i:int=0;

    for(x=1;x<=bmpd.width;x++){

        for(y=1;y<=bmpd.height;y++){

            bmpd.setPixel32(x,y,a);

            i++;

        }

    }

}

function shuffle(a:Array) {

    var p:int;

    var t:*;

    var ivar:int;

    for (ivar = a.length-1; ivar>=0; ivar--) {

        p=Math.floor((ivar+1)*Math.random());

        t = a[ivar];

        a[ivar] = a

;

        a

= t;

    }

}