A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [RESOLVED] BitmapData.hitTest with slanted lines

  1. #1
    Junior Member
    Join Date
    Nov 2008
    Posts
    9

    resolved [RESOLVED] BitmapData.hitTest with slanted lines

    Hi,
    I'm trying to perform a BitmapData hitTest between a dynamically loaded BitmapData object and a dynamically created slanted line. The problem is, even when I draw the line as BitmapData, it still hitTests the entire bounding box of the slanted line.

    What I start with is
    Code:
    public var Line:Shape = new Shape();
    Then I make a new line every frame between two points:
    Code:
    Line.graphics.clear();
    Line.graphics.lineStyle(1, 0xFF0000, 1);
    Line.graphics.moveTo(mc.x, mc.y)					Line.graphics.lineTo(mouseX,mouseY)
    Then, draw it to a bitmapData:
    Code:
    var bd:BitmapData = new BitmapData(Line.width, Line.height);
    bd.draw(Line);
    Then, the hitTest:
    Code:
    if (Object.bitmapData.hitTest(FirstPoint(works), 1, bd, new Point(0,0))){
      //execute code
    }
    I think the problem's with the 'second point', but the livedocs and an hour of google-searching have yielded nothing :/

  2. #2
    Senior Member Orkahm52's Avatar
    Join Date
    Jan 2006
    Location
    Perth, Australia
    Posts
    335
    HitTest() will always check the bounding box no matter the shape of the contents, movie clip and bitmap data alike. So instead of using a hitTest() you could use getPixel();

    Code:
    if (Object.bitmapData.getPixel(point.x, point.y)==0xFF0000){
      //execute code
    }
    Though that would only work if the colour is always the same, so you might want to check a range of colours like so:

    Code:
    if (Object.bitmapData.getPixel(point.x, point.y)>0x660000 || Object.bitmapData.getPixel(point.x, point.y)<0xFF0000){
      //execute code
    }
    ~Ork.
    Last edited by Orkahm52; 02-12-2009 at 08:47 AM.

  3. #3
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    No, if you use 2 bitmapdata objects, you can find hittest between actual pixels and not just bounding boxes:
    http://www.8bitrocket.com/newsdispla...?newspage=9947
    In AS3 displayObject has 2 methods (hitTestObject and hitTestPoint) and bitmapData has a method called hitTest.
    Last edited by tonypa; 02-12-2009 at 12:41 PM.

  4. #4
    Junior Member
    Join Date
    Nov 2008
    Posts
    9
    ... and hitTest between two BitmapData objects is what I'm trying to do... but somehow it hitTests the entire bounding box rather than the actual bitmapData, which is a slanted line.

  5. #5
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Maybe you need to make the bitmapdatas transparent first.

  6. #6
    Junior Member
    Join Date
    Nov 2008
    Posts
    9
    Both bitmapData's transparent properties are true, I checked.

    EDIT: Apparently when creating a new bitmapData, I was supposed to indicate that the fill was 0x000000(nothing), because by default my bitmapData became a white rectangle, which consituted the problem.

    Thanks a lot, Tonypa
    Last edited by Tartarus; 02-12-2009 at 04:38 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center