A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: hitTest?

  1. #1
    Senior Member
    Join Date
    Nov 2000
    Location
    Allentown PA
    Posts
    350

    resolved

    Ok, I have a quick question.. I've created my first game in Flash 5 and up till now when I did collision detection in Flash 4 I didn't have the option of "hitTest" so I have a question...

    Does hit test check the reqtangular box that holds the movie clip, or does it test the content in the clip?
    What I mean is if I have a triangle inside a one movie clip, and a circle in another does it only test to see if the Triangle touches the Circle or does it test to see if the 'outline/box' that holds the movie clips touch?

    I've looked around for this answer and can't seem to find it anywhere?

    If you'd like to see the game I'm working on you can see it in the games section of my half done site: http://members.aol.com/_ht_a/craigfriebolin/freqfestv2/

    Thanks

    - Frequent-C

  2. #2
    Junior Member
    Join Date
    Jun 2001
    Posts
    24
    First off, your site rocks, even on half quality.

    Secondly, whenever you call myMovieClip.hitTest(x,y,true); flash evaluates whether or not myMovieClip's shape is touching the point (x,y) so long as the third argument (shapeflag) is set to true. Unfortunately, that only works one way, and only for one point (x,y).
    Consequently, myMovieClip.hitTest(triangleMovieClip); checks the bounding boxes (rectangluar) of both for a hit.

    I'm not sure if this will help, the only way that I have found for a reliable hitTest between two odd shapes takes alot of math and coding and slows down the movie. Good luck.

  3. #3
    Senior Member
    Join Date
    Nov 2000
    Location
    Allentown PA
    Posts
    350
    Thanks for the compliment
    And yeah, I was figuring that it tested the boxes and not the actual shape in the box. Hmmmmmmm.... so.. Anybody know how to figure out a hit test for darts on a dart board?

    Thanks anyway

    Frequent-C

    http://members.aol.com/_ht_a/craigfriebolin/freqfestv2/

  4. #4
    Junior Member
    Join Date
    Jun 2001
    Posts
    24
    do you want a hit test for a dart hitting another dart or for finding it's position on the board?

    First off, I'm assuming that you have a way to find the tip point of the dart (where it hits the board) if that isn't the (0,0) point of the movie clip, find the offset.

    secondly, your program should be able to tell where its going to actually hit on the board (x,y) as soon as you let go of the mouse. It's probably something like
    dartHitX = dartReleaseX + aimX

    so to find any darts that are going to land close or on a dart's coordinates use:
    for(i=0;i<=numberOfDartsThrown;i++){
    if (Math.abs(dartX[throwNumber] - dartX[throwNumber-i])<1){ // this dart is going to be less than 1 away from another dart.
    collision = true;
    }
    }

    for finding the score on the board, you could use hitTest, via the hitTest(x,y,true) method if you use the TriangleMovieClip.hitTest method and have the x,y be what ever the tip of the dart's x and y location in global coordinates.

    Or you could use Trig. Up to you.

  5. #5
    Senior Member
    Join Date
    Nov 2000
    Location
    Allentown PA
    Posts
    350
    Actually I'm trying to find out the where on the board the dart landed. And I have to admit, I'm new to the whole 'hitTest' thing So what I'm really trying to find out is if it's posible to use that to pin-point where the dart landed. And from reading your posts 'I THINK' you've started to lead me in the right direction, but i'm not 100% sure?

    So help me get this straight.. I can set ONE movieclip to do a hit test that will test weather or not the triangle was hit, rather than the bounding box of the triangle. And set up my other clip to be '1 pixel'-ish for the tip of the dart? And that will work? And if so... How? I'm just soooo confused.

    I REALLY apperciate the help!

    - Frequent-C

  6. #6
    Senior Member
    Join Date
    Jun 2001
    Location
    Virginia, USA
    Posts
    437
    Actually I'm trying to find out the where on the board the dart landed
    If you have your dart as a MC and a board is another MC, then you have to use this function globalToLocal(point); and localToGlobal(point)

    First convert the tip of the dart to global (stage's coord) then convert that point (stage's coord) to local inside the board MC.

    Hope this helps

  7. #7
    Senior Member
    Join Date
    Nov 2000
    Location
    Allentown PA
    Posts
    350
    Ahhhhhhhhhhhhhhhhhhhh..... ok, I'm better... anyway i've been working on this now for a while and I actually managed to get somewhere. I now figured out how to use the hit test function to figure out when the x,y,(point of the dart) hit a pie shaped piece of the dart board. *However* I think I'm doing something wrong!? I had to adjust the coords a bit to get it to work. And now I thought I would just be able to rotate the pie and use it for the next space but they all seem a little off? I can't figure out what I'm doing wrong, but I think it has something to do with rapidcarbon's post?

    Can anyone explain to me how to use globalToLocal(point); and localToGlobal(point); I tried to mess with them a bit, but I don't entirely understand them?

    Thanks for the help

    - Frequent-C

  8. #8
    Junior Member
    Join Date
    Jun 2001
    Posts
    24
    Don't worry about global to local stuff... Just remember that a local coordinate depends on that little + thing being 0, and everything above and left is negative y and x respectively. In global coordinates, the top corner of the movie is 0 and everything you see is positive y and x.

    What does that mean?... not a whole lot, it just depends on where you are accessing the functions and variables from. When you call for a MCs properties who is on the main timeline, you are using global, if you are calling for a coordinate inside of a MC (like a MC instance inside another MC) you are going on the MC's local coordinates.

    I like to try to make the center point of things like a dartboard, be the center of the MC (right on the +) so the math works out without any kind of offset. If that isn't possible, just use an offset variable whenever you call for the coordinates.

    So what should you do? 1st) Create a new MC of both the dartboard (centered on the + sign) and the Dart itself. this way, you're sort of separating them from the rest of the Movie in it's own little Movie clip and it's own set of coordinates. Make sure the dart when it is in it's finished position has it's tip on the + for it's movieclip, (It is probably at it's top corner right now)
    2nd) Create a function in the new MC --either in the timeline or using onClipEvent(load)-- as follows:
    numberOfTriangles = 16
    function checkDartHit(){
    distanceFromCenter = int(Math.sqrt(Math.abs((dartHitX*dartHitX)+(dartHi tY*dartHitY))));
    //you can use the distanceFromCenter to tell which set of scores to use. Inner Ring, 1st Ring, 2nd Ring...
    angleFromCenter = int(Math.atan2(dartHitY,dartHitX)/(Math.PI/(numberOfTriangles/2))+(numberOfTriangles/2);
    //you can use the angleFromCenter to point to which triangle you've hit
    }

    with those two calculations, you can find the how much should be added to the score with a few simple if() tests.

    Good luck!

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