A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: [F8] Bitmap HitTesting

  1. #1
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167

    [F8] Bitmap HitTesting

    Hey everyone, been a while since I've posted here, hope you're all doing well those who remember me.


    Anyways, working on a little project with a bunch of people, and I figured it's about time I finally worked out hitTesting for good. So I've resulted in learning bitMap hitTesting.

    With a little research (though it's hard to find answers when you don't know what to look for), we managed to find the help files on how to do bitmap hitTesting, and better yet, how to attach a bitmap movieclip from the library.

    However, trying the code used in the Flash Help files (almost copy and paste), it didn't work. Here is the code:

    code:

    import flash.display.BitmapData;
    //
    var linkageId:String = "box";
    var myBitmapData:BitmapData = BitmapData.loadBitmap(linkageId);
    trace(myBitmapData instanceof BitmapData);
    // true
    _root.mc_1.attachBitmap(myBitmapData, this.getNextHighestDepth());



    So ...

    I have the linkage name box taken from the library, and then create the BitmapData object, which loads the movieclip into it. I then attempt to attach that bitmap movieclip into a holder moveclip on the stage dubbed mc_1.

    As you can see, when run, the trace should return and display true but that isn't the case, so that is why I'm seeking help here.

    Anyone have any ideas?



    Oh and by the way, if you have any suggestions to do hitTesting that is easier and more efficient then bitmap hitTesting, please enlighten me.

    Thanks.

    Matt.

  2. #2
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    Oh and by the way, if you have any suggestions to do hitTesting that is easier and more efficient then bitmap hitTesting, please enlighten me.
    depends on what you need it for. For some projects this method suits perfectly while others could improve greatly with other attempts.

    an old friend of me who is a c++ developer told me that pixel collision checking is brute force,- and to avoid it as much as possible in mass calculations such as game collisions with lots of objects.
    What it means its not just the HitTesting of the bitmap you need for a game but also a raw detection method that tells the engine if the object is close of beeing hit.



    regarding your problem
    Code:
    _root.mc_1.attachBitmap(myBitmapData, this.getNextHighestDepth());
    I am fairly new with the bitmap API stuff but If I recall correclty this attaches the Bitmap only as data to a bitmap data object,- not any visible object.
    So what you need to do is drawing a shape onto the stage that contains the bitmap data e.g:
    Code:
    _root.createEmptyMovieClip("mc_shape",1);
    mc_shape.beginBitmapFill(myBitmapData,false,0x00000000);
    mc_shape.lineTo(myBitmapData.width,0);
    mc_shape.lineTo(myBitmapData.width,myBitmapData.height);
    mc_shape.lineTo(0,myBitmapData.height);
    mc_shape.lineTo(0,0);
    not sure though if the above code is error free (syntax wise),- test it yourself

    now I am a little bit irritated because your thread title says something about hitTest,- but your actual code has nothing to do with it yet?

  3. #3
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167
    Sorry about the misunderstanding, I just though bitmapping was only used in hitTesting, I'm new to it as well - didn't know any better. :P

    Anyways, thanks for the code! I'm going to try it out now.


    As for what I'm using it for. Simple character movement, and hitting against objects. The way we're doing it is creating an invisible movieclip that 'traces' out the areas in which the player cannot touch.

    This saves us from making many movieclips in which you have to check for each.

    So in the end, this means the hitTest 'area' is a very irregular shape, which is why normal hitTesting won't work ... or I don't think anyways.

  4. #4
    Truimagz.com everfornever's Avatar
    Join Date
    Sep 2006
    Location
    St. Louis
    Posts
    1,306
    If I am to understand you corretly you are doing art based hitTests? I have been using that type for a long time, and finally cam to the conclusion that tile based collision is the best whenever you are able to go this route, you do say that your hitTests are irregular shapes so that leads me to believe this is a hitTests were your using something like (!stage.bound.hitTest(player._x,player._y,true)).

    In a top down game this has worked fine for me, but again to answer your last question tile based games give the best hittest results I have found so far.

    If you are new to tile based games look in the knowledge base for tonypas tuts, there written in flash 5 but with little tweaking the code and ideas can be implemented in as2
    Last edited by everfornever; 10-31-2006 at 12:44 AM.

  5. #5
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    I had a thought about this and will probably do some experiments in a week or so, if you were to use a mask for each sprite and then use XOR compare to see if a pixel in the mask was being inverted, that would detect a hit, meaning pixel perfect hit detection. (in theory lol)

    Like I say, if anyone understands that then go ahead and give it a whirl, Im not like some on the board ...if not I'll give it a do it as soon as I finish my current game and make it open source if it works!!

    RipX

  6. #6
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    yeah, look up getColorBoundsRect, i was going to add some bitmap hitTest methods like what ripX said to the simple hitTest and Collision thread, tony and GSkinner already have an open source file which does what you want. Ask them
    lather yourself up with soap - soap arcade

  7. #7
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    It's ok, i'll play around with it when I get the chance, i've been doing some similar stuff in my emulation projects so it'll be fun to apply it to hittesting!

    RipX

  8. #8
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Tested the code in first post and it works fine. Possible reasons why it would not work:

    *item with linkage name "box" in the library must be bitmap, not mc
    *movie must be exported for Flash8 or 9

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