Skip to main content
Known Participant
February 22, 2012
Answered

Pixel position, based on its color - Attaching a movieclip along a path

  • February 22, 2012
  • 1 reply
  • 775 views

Hi.

How could I find, a pixel´s position, based on its color?

Let´s say:

We have a red path drawn on stage, we make it a movieclip, we make a bitmapdata of that movieclip.

Then, I want to find the position (x and y coordenate) of a given red pixel, within the bitmapdata.

I have a movieclip, called 'footstep';

I have a red path movieclip, called 'path'.

How could I attach, the 'footstep' movieclip, a number of times, within an interval, along that 'path' movieclip?

And also, calculate the 'footstep' rotation, based on the path direction.

Thanks.

This topic has been closed for replies.
Correct answer kglad

you have a black bitmapdata so there shouldn't be any match.  it looks like you missed a draw() method that you might want to use:

marcelozepgames wrote:

I´ve tried this:

package

{

          import flash.display.BitmapData;

          import flash.display.Bitmap

          import flash.geom.Point

          import flash.geom.Rectangle

          import flash.display.MovieClip;

          import flash.events.*

          public class Test2 extends MovieClip

          {

var red_bmpd:BitmapData

var red_bmp:Bitmap

                    public function Test2()

                    {

                              red_bmpd = new BitmapData(red_mc.width, red_mc.height, false, 0);

red_bmpd.draw(red_mc);

                              red_bmp = new Bitmap(red_bmpd)

                              var targetColor:uint = 0xff0000

                              var coords:Point = null;

                              var color = null

                              for (var i:int = red_bmpd.rect.x; i < red_bmpd.rect.width; i++)

                              {

                                        for (var j:int = red_bmpd.rect.y; j < red_bmpd.rect.height; j++)

                                        {

  if( red_bmpd.getPixel(i, j) == targetColor ){

                                                            trace("ok")

                                                            color =red_bmpd.getPixel(i, j)

                                                            coords = new Point(i, j)

                                                            trace(coords, color)

                                                            break

                                                  }

                                        }

                              }

                    }

          }

}

1 reply

kglad
Community Expert
Community Expert
February 22, 2012

you can loop through your bitmapdata's x,y and use getPixel() or getPixel32().

Known Participant
February 22, 2012

I´ve tried this:

package

{

          import flash.display.BitmapData;

          import flash.display.Bitmap

          import flash.geom.Point

          import flash.geom.Rectangle

          import flash.display.MovieClip;

          import flash.events.*

          public class Test2 extends MovieClip

          {

var red_bmpd:BitmapData

var red_bmp:Bitmap

                    public function Test2()

                    {

                              red_bmpd = new BitmapData(red_mc.width, red_mc.height, false, 0)

                              red_bmp = new Bitmap(red_bmpd)

                              var targetColor:uint = 0xff0000

                              var coords:Point = null;

                              var color = null

                              for (var i:int = red_bmpd.rect.x; i < red_bmpd.rect.width; i++)

                              {

                                        for (var j:int = red_bmpd.rect.y; j < red_bmpd.rect.height; j++)

                                        {

  if( red_bmpd.getPixel(i, j) == targetColor ){

                                                            trace("ok")

                                                            color =red_bmpd.getPixel(i, j)

                                                            coords = new Point(i, j)

                                                            trace(coords, color)

                                                            break

                                                  }

                                        }

                              }

                    }

                             

          }

}

It doesn´t work.

It doesn´t find the targetColor.

It´s a 0xFF0000 red path.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
February 22, 2012

you have a black bitmapdata so there shouldn't be any match.  it looks like you missed a draw() method that you might want to use:

marcelozepgames wrote:

I´ve tried this:

package

{

          import flash.display.BitmapData;

          import flash.display.Bitmap

          import flash.geom.Point

          import flash.geom.Rectangle

          import flash.display.MovieClip;

          import flash.events.*

          public class Test2 extends MovieClip

          {

var red_bmpd:BitmapData

var red_bmp:Bitmap

                    public function Test2()

                    {

                              red_bmpd = new BitmapData(red_mc.width, red_mc.height, false, 0);

red_bmpd.draw(red_mc);

                              red_bmp = new Bitmap(red_bmpd)

                              var targetColor:uint = 0xff0000

                              var coords:Point = null;

                              var color = null

                              for (var i:int = red_bmpd.rect.x; i < red_bmpd.rect.width; i++)

                              {

                                        for (var j:int = red_bmpd.rect.y; j < red_bmpd.rect.height; j++)

                                        {

  if( red_bmpd.getPixel(i, j) == targetColor ){

                                                            trace("ok")

                                                            color =red_bmpd.getPixel(i, j)

                                                            coords = new Point(i, j)

                                                            trace(coords, color)

                                                            break

                                                  }

                                        }

                              }

                    }

          }

}