0 Replies Latest reply on Jul 2, 2009 12:28 PM by meshgraphics

    BitmapData.hitTest is Transparent Opacity Enough

    meshgraphics Level 2

       

      /**

      *

      * Just thought I'd give you guys a crack at this before alerting the documentation crew.

      * Are they still accepting comments in livedocs?

      *

      * http://livedocs.adobe.com/flex/3/html/help.html?content=Working_with_Bitmaps_06.html

      *

      * It looks like the doc for BitmapData.hitTest is real screwy.

      * It should probably be called transpFence() - is the pixel transparent enough?

      *

      * The first parm, Point1 is arbitrary. It becomes the de facto (0,0).

      * It is not related to screen coords (see trace 9000).

      * hitTest doesnt care if the obj is on screen. It works on (its own) bitmap data.

      *

      * I've added Bitmap bm3 with transparency on, set to 0x33. Next I added a solid cross of red at (50,50).

      *

      * The pixels at 49 and 51 are transparent enough to fail.

      *

      * You can put this in an ActionScript project and run with debug. The trace is in the Console window.

      *

      *

      */

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

      package

      {

      import

       

       

      flash.display.Bitmap;

      import

       

       

      flash.display.BitmapData;

      import

       

       

      flash.display.Sprite;

      import

       

       

      flash.geom.Point;

       

       

       

       

       

      public class BitmapDataHitTest extends

      Sprite

      {

       

       

      public function

      BitmapDataHitTest()

      {

       

       

      var bmd1:BitmapData = new BitmapData(100, 100, false

      , 0x000000FF);

       

       

      var bmd2:BitmapData = new BitmapData(20, 20, false

      , 0x00FF3300);

       

       

      // full green totally transparent

       

       

      var bmd3:BitmapData = new BitmapData(100, 100, true

      , 0x0000ff00);

       

       

      // not added to stage

       

       

      var bm3:Bitmap = new

      Bitmap(bmd3);

       

      // cross maker from previous page

       

       

      for (var

      i:uint = 0; i < 100; i++)

      {

       

       

      // full opacity is assumed -- wrong!!!

       

       

      var

      red:uint = 0xFFFF0000;

      bmd3.setPixel32(50, i, red);

      bmd3.setPixel32(i, 50, red);

      }

       

       

       

       

      var bm1:Bitmap = new

      Bitmap(bmd1);

       

       

      this

      .addChild(bm1);

       

       

       

      // Create a red square.

       

       

      var redSquare1:Bitmap = new

      Bitmap(bmd2);

       

       

      this

      .addChild(redSquare1);

      redSquare1.x = 0;

       

       

       

      // Create a second red square.

       

       

      var redSquare2:Bitmap = new

      Bitmap(bmd2);

       

       

      this

      .addChild(redSquare2);

      redSquare2.x = 150;

      redSquare2.y = 150;

       

       

       

      // Define the point at the top-left corner of the bitmap.

       

       

      var pt1:Point = new

      Point(0, 0);

       

       

      // Define the point at the center of redSquare1.

       

       

      var pt2:Point = new

      Point(20, 20);

       

       

      // Define the point at the center of redSquare2.

       

       

      var pt3:Point = new

      Point(160, 160);

       

       

       

      trace(bmd1.hitTest(pt1, 0xFF, pt2));

      // true

       

       

      trace(bmd1.hitTest(pt1, 0xFF, pt3));

      // false

       

       

      trace(bmd1.hitTest(new Point(0,4), 0xFF, new

      Point(0,105)));

       

       

      trace(bmd1.hitTest(new Point(101,101), 0x00, new

      Point(101,202)));

       

       

      trace(bmd1.hitTest(new Point(101,101), 0x00, new

      Point(201,201)));

       

       

      trace(bmd1.hitTest(new Point(9001,101), 0x00, new

      Point(9001,201)));

       

       

      trace("-------------------------"

      );

       

       

      trace(bmd3.hitTest(new Point(0,0), 0xFF, new

      Point(0,49)));

       

       

      trace(bmd3.hitTest(new Point(0,0), 0xFF, new

      Point(0,50)));

       

       

      trace(bmd3.hitTest(new Point(0,0), 0xFF, new

      Point(0,51)));

       

       

      trace("-------------------------"

      );

       

       

      trace(bmd3.hitTest(new Point(0,0), 0x01, new

      Point(0,49)));

       

       

      trace(bmd3.hitTest(new Point(0,0), 0x01, new

      Point(0,50)));

       

       

      trace(bmd3.hitTest(new Point(0,0), 0x01, new

      Point(0,51)));

       

       

      trace("-------------------------"

      );

       

       

      trace("px next to cross is transparent enough?"

      );

       

       

      trace("-------------------------"

      );

       

       

      trace(bmd3.hitTest(new Point(0,0), 0x32, new

      Point(0,49)));

       

       

      trace(bmd3.hitTest(new Point(0,0), 0x33, new

      Point(0,49)));

       

       

      trace(bmd3.hitTest(new Point(0,0), 0x34, new

      Point(0,49)));