A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: hit test barrier

  1. #1
    Video Game Addict
    Join Date
    Apr 2004
    Location
    Oregon
    Posts
    128

    hit test barrier

    I know how to get an object when hit to move to a random spot on the stage but how would i make it so that when something its it it stops?

    Like this:
    I have a movie clip of a wall, and when the user controlled player hits the wall it wont let them past it, like a boundry i guess, How would i do that?

  2. #2
    Video Game Addict
    Join Date
    Apr 2004
    Location
    Oregon
    Posts
    128
    anyone please, cant be that hard...

  3. #3
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    What you're doing is called collision testing or hit testing. There are simple ways to do it, and more complicated ways.

    A simple way to do it in flash is to use the hitTest() function, which comes in a few different forms. There is one form, where you can test two movieclips for collision:

    code:

    if (movie1.hitTest(movie2))
    {
    trace('kaboom!');
    }



    However this form tests if the bounding-boxes of each movieclip intersect, and sometimes, that isn't what you want, especially if your movieclips are not square in shape. It's particularly bad for long skinny movieclips which are rotated, because these have a big giant bounding-box.

    There is another version which tests if a particular set of global x,y coordinates collides with any of the visible parts of a movieclip:

    code:

    if (movie1.hitTest(x,y,true))
    {
    trace('kaboom!');
    }

    OR

    if (movie1.hitTest(movie2._x,movie2._y,true))
    {
    trace('kaboom!');
    }





    ...and this tends to get used more often for games, as it is more precise. But since it only tests a single point, you have to account for that.

    One way to account for that is to design your game so that objects move about a grid in discrete jumps. For example, in a frogger style game, if the frog always moves += 50 pixels, and the cars are centered on each 50x50 cell, then you only need to test the center of the frog with the cars to see if there is a hit.

    Another way to deal with it is to test a few different points which represent different parts of the object (e.g. the back and front of a bumper car).

    Finally, if the wall stretches all the way across the stage, or you are trying to restrain an object to a rectangular area of movement, it's easier to just do the math and check if _x is greater or less than some minimum or maximum value.

    code:

    if (this._x > 20 or this._x > 500)
    {
    trace('kaboom!');
    }




    In more advanced arcade games, collision testing can get pretty hairy - if an object is moving really fast, it may jump over an object it is supposed to collide with, which makes hitTest() style collision testing fail. In this case, you have to do a line/object collision test and see if the line of movement intersects with any objects. Ugh.
    Last edited by jbum; 10-21-2004 at 05:28 PM.

  4. #4
    Video Game Addict
    Join Date
    Apr 2004
    Location
    Oregon
    Posts
    128

    Thanks

    thank you for the help

  5. #5
    Banned
    Join Date
    Dec 2007
    Location
    In planet earth...florida
    Posts
    11
    Hello! I am flashkay!
    I would just like to say...I love hiTest!!!

    Ok, here is one kind of HitTest that see's if a blue box that goes around the movie clip is touching another movie clip's blue box:

    NOTE: the code goes inside movieclip 1

    CODE:

    onClipEvent (load) {
    speed = 2;
    }

    onClipEvent (enterFrame) {
    if (this.hitTest(_parent.MOIECLIP2)
    this.speed = 0;
    }
    }

    And this is to see if it is touching the actual movieclip 1, but, you can't make it slow down or anything...you can only make it stop:

    NOTE: this code goes inside movieclip 1

    CODE:

    if (_root.MOVIECLIP1.hitTest(_x+(_width/2), _y, true)) {
    this._x -= 2;
    }
    if (_root.MOVIECLIP1.hitTest(_x-(_width/2), _y, true)) {
    this._x += 2;
    }
    if (_root.MOVIECLIP1.hitTest(_x, _y+(_height/2), true)) {
    this._y -= 2;
    }
    if (_root.MOVIECLIP1.hitTest(_x, _y-(_height/2), true)) {
    this._y += 2;
    }
    }



    This might help!


    Bye-bye!

    ~kayla

  6. #6
    Banned
    Join Date
    Dec 2007
    Location
    In planet earth...florida
    Posts
    11
    Hello! I am flashkay!
    I would just like to say...I love hiTest!!!

    Ok, here is one kind of HitTest that see's if a blue box that goes around the movie clip is touching another movie clip's blue box:

    NOTE: the code goes inside movieclip 1

    CODE:

    onClipEvent (load) {
    speed = 2;
    }

    onClipEvent (enterFrame) {
    if (this.hitTest(_parent.MOIECLIP2)
    this.speed = 0;
    }
    }

    And this is to see if it is touching the actual movieclip 1, but, you can't make it slow down or anything...you can only make it stop:

    NOTE: this code goes inside movieclip 1

    CODE:

    if (_root.MOVIECLIP1.hitTest(_x+(_width/2), _y, true)) {
    this._x -= 2;
    }
    if (_root.MOVIECLIP1.hitTest(_x-(_width/2), _y, true)) {
    this._x += 2;
    }
    if (_root.MOVIECLIP1.hitTest(_x, _y+(_height/2), true)) {
    this._y -= 2;
    }
    if (_root.MOVIECLIP1.hitTest(_x, _y-(_height/2), true)) {
    this._y += 2;
    }
    }



    This might help!


    Bye-bye!

    ~kayla

  7. #7
    Banned
    Join Date
    Dec 2007
    Location
    In planet earth...florida
    Posts
    11
    Hello! I am flashkay!
    I would just like to say...I love hiTest!!!

    Ok, here is one kind of HitTest that see's if a blue box that goes around the movie clip is touching another movie clip's blue box:

    NOTE: the code goes inside movieclip 1

    CODE:

    onClipEvent (load) {
    speed = 2;
    }

    onClipEvent (enterFrame) {
    if (this.hitTest(_parent.MOIECLIP2)
    this.speed = 0;
    }
    }

    And this is to see if it is touching the actual movieclip 1, but, you can't make it slow down or anything...you can only make it stop:

    NOTE: this code goes inside movieclip 1

    CODE:

    if (_root.MOVIECLIP1.hitTest(_x+(_width/2), _y, true)) {
    this._x -= 2;
    }
    if (_root.MOVIECLIP1.hitTest(_x-(_width/2), _y, true)) {
    this._x += 2;
    }
    if (_root.MOVIECLIP1.hitTest(_x, _y+(_height/2), true)) {
    this._y -= 2;
    }
    if (_root.MOVIECLIP1.hitTest(_x, _y-(_height/2), true)) {
    this._y += 2;
    }
    }



    This might help!


    Bye-bye!

    ~kayla

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