dcsimg
A Flash Developer Resource Site

Page 1 of 11 12345 ... LastLast
Results 1 to 20 of 215

Thread: The elusive hitTest explained

  1. #1
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954

    The elusive hitTest explained

    Back by popular demand!

    HitTest is a very useful, and can be used for just about anything (like ductape really). There are two main forms that it takes:
    Code:
    if(this.hitTest(another_clip)){
     // Something
    }
    This just tests the bounding boxes of this, and another_clip. If they hit, then the contents of the if will run.
    Code:
    if(this.hitTest(x, y, true)){
     // Something
    }
    This tests the point x,y against the contents of this, the actual shape and not the bounding box. The true is what makes it test against the shape and not the bounding box.

    Anyone else want to continue?

    (if it's an irrelevant post, it'll be edited out as this is a guide sticky, not just a general bantering one )
    Last edited by Ed Mack; 11-02-2002 at 09:27 AM.

  2. #2
    Strange Life strangelife2k's Avatar
    Join Date
    Jun 2002
    Location
    UK
    Posts
    429
    I am going to show you how to have a hitTest on your badguy so that if your hero touches it, it will decrease your life.

    1.Goto your hero's instance box and name it "player"

    2.Add a dynamic text box on your scene, and set it's variable to "life".

    3.Add this code to Frame 1
    Code:
    life = 100;
    Edit the life if you wish

    4.Bring up the object actions panel for your enemy and add these actions:
    Code:
    onClipEvent (enterFrame) {
        if (this.hitTest(_root.player)) {
            _root.life -= 5;
            if (_root.life == 0) {
                _root.gotoAndStop(2);
            }
        }
    }
    These actions cause the variable "life" on _root (the one you set the dynamic text field to display) to go down by 5 each time the player and the enemy collide. It also makes the main timeline goto frame two, if the player runs out of life. You should put a gameover screen on frame two of _root.

    There you go, test it out

    [ed: Just tweaking a few readability things, thanks very much for this ]
    Last edited by Ed Mack; 11-02-2002 at 09:27 AM.

  3. #3
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  4. #4
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    Thanks! Is there much more that we can do?

    Here's code I'm quite fond of using for moving the player. It requires a clip on _root, called map containing the areas you're not allowed to walk on (It basically tests the bounding box of the object against an actual shape):

    Code:
    onClipEvent(load){
    	s = 5;
    	b = this.getBounds(this);
    
    	function move(x,y){
    		if(!_root.hit.hitTest(_x+x+b.xmin+1, _y+y+b.ymin+1, true)){
    			if(!_root.hit.hitTest(_x+x+b.xmax-1, _y+y+b.ymin+1, true)){
    				if(!_root.hit.hitTest(_x+x+b.xmin+1, _y+y+b.ymax-1, true)){
    					if(!_root.hit.hitTest(_x+x+b.xmax-1, _y+y+b.ymax-1, true)){
    						_x += x;
    						_y += y;
    					}
    				}
    			}
    		}
    	}
    }
    
    onClipEvent(enterFrame){
    	if(Key.isDown(Key.UP)){
    		move(0,-s);
    	}
    	if(Key.isDown(Key.DOWN)){
    		move(0,s);
    	}
    	if(Key.isDown(Key.LEFT)){
    		move(-s,0);
    	}
    	if(Key.isDown(Key.RIGHT)){
    		move(s,0);
    	}
    }

  5. #5
    Not PWD ViRGo_RK's Avatar
    Join Date
    May 2001
    Posts
    1,799
    Code:
    if(this.hitTest(x, y, true)){
     // Something
    }

    This tests the point x,y against the contents of this, the actual shape and not the bounding box. The true is what makes it test against the shape and not the bounding box.

    This would be useful for, say, when leaving a level. Like your hero. once he hits a door or something, (this is for art-based), you would just set the right x and y coords and poof! Perfect for art based...


    PAlexC: That's just Chuck Norris's way of saying sometimes corn needs to lay the heck down.
    Gerbick: America. Stabbing suckers since Vespucci left.

  6. #6
    SaphuA mosterdfles_flash's Avatar
    Join Date
    Jan 2002
    Location
    Tha Couch
    Posts
    935

    bla bla bla

    Ah well i'll gonna be a part of this to

    This _x, _y, true thingy can also be used for games like this:
    http://members.lycos.nl/saphua/games/newBouncy.swf

    It will walk on the hill instead of on the bounding box

    There are loads of files here on flashkit wich explain this very well (atleast bether than my [poor] english)

    Last edited by Ed Mack; 11-06-2002 at 06:50 PM.
    'How Art Is The Visual While We’re Artificial…'

    Mail & MSN
    My Not Finisched Page!
    Nick: SaphuA

  7. #7
    Junior Member
    Join Date
    Nov 2002
    Posts
    13

    Will this work with...

    this works really nicely for art-based RPG's where you go from room to room, but is there any way to modify the code to make a large outdoor map that will start to scroll up/down/left/right as soon as the "hero" gets to the midpoint of the screen? If you still don't know what I'm talking about here's an example someone did that I'm trying to implement into my RPG engine, but i'm stumped on how to do it using the current script setup: http://www.geocities.com/mclelun/rpg.html

    Any help would be appreciated. Thanx.
    -NeotiK

  8. #8
    Junior Member
    Join Date
    Nov 2002
    Posts
    18
    HI,
    I just want to detect collision between two circles not with there bounding boxes but there actual shapes using shapeFlag property of flash.

    I came acros this thread actual wherein the discussion is alrady on.
    Anyway
    we all know this is right
    onCliEvent(enterFrame){
    if(hitTest(_root._xmouse,_root._ymouse,true)){
    trace(Hit is on);
    }
    }

    This works fine with the _xmouse and _ymouse property.

    But what If I want to do this witha movie clip but checking the actual shape of the movieclip not the bounding box.

    something like :
    onCliEvent(enterFrame){
    if(hitTest(_root.mov._x,_root.mov._y,true)){
    trace(Hit is on);
    }
    }

    This also works but it works only when the center of the "mov" clip which is basically a circle is hit by the host movie clip i:e _x and _y of mov.

    I want it to happen when the mov actaully collides iwth the host movielcip

    So I treid

    onCliEvent(enterFrame){
    if(hitTest(_root.mov._x-_root.mov._width/2,_root.mov._x-
    _root.mov._width/2,_root.mov._y-_root.mov._height/2,_root.mov._y+
    _root.mov._height/2){
    trace(Hit is on);
    }
    }


    This is to detect the actual collison of the mov movie clip.
    But this isnt happening.

    Also I have heard that this type of hitTest method in flash consumes quiet an amount of memory resulting in lower fps.

    Pls guide me if I am wrong somewhere.

    thx
    Where is this Flash Rocket Taking us ^|^

  9. #9
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    The easiest way to check if two circles hit, is to work out the distance between the two circle's centers, and check if it less than both circle's radias (?) added together.

  10. #10
    Junior Member
    Join Date
    Nov 2002
    Posts
    18
    that worked, that really worked.

    thx edmack that was gr8 help.

    this is what I tried and got it. The important thing is that I have totally eluded the hitTest function of flash.

    onClipEvent (enterFrame) {
    x = _x-_root.two._x;
    y = _y-_root.two._y;
    h = Math.sqrt((x*x)+(y*y));
    rad = _width/2;
    radTwo = _root.two._width/2;
    sumRad = rad+radTwo;
    if (sumRad>=h) {
    trace("yes");
    }
    }
    onClipEvent(load){
    startDrag("", true);
    }

    two is the other movie clip on the stage.

    Infact I am working on a pool game with this help I can really move ahead. I will start posting threads based on this.

    thx again.
    Where is this Flash Rocket Taking us ^|^

  11. #11
    I'm dope. Psytrix's Avatar
    Join Date
    Aug 2002
    Location
    Slidell, LA
    Posts
    309
    Originally posted by Ed Mack

    It requires a clip on _root, called map containing the areas you're not allowed to walk on (It basically tests the bounding box of the object against an actual shape):

    Could you explain that code a lil better, im not sure exactly how it works...?

  12. #12
    Actionscript Nerd
    Join Date
    Apr 2002
    Location
    USA
    Posts
    47
    NeotiK, your engine is looking sweet.
    Ed Mack, I think someone got an 'A' in geometry.
    ••••••••••••••••••••••••••••••••••••••••••••••••••
    Try out the all new Ball Drop 2.0!
    http://www.mtsu.edu/~jwt2h/balldrop/

  13. #13
    Junior Member
    Join Date
    Oct 2002
    Posts
    23

    general question...

    is there a proper way to do a hitTest that checks if some one is clicking on the object? if not are you supposed to use buttons to do this? i have seen many games where a click alters a MC... (shoot-em-up types obviously)

    -joe

  14. #14
    Junior Member
    Join Date
    Nov 2002
    Posts
    18
    try this

    PHP Code:
    onClipEvent(mouseUp){

    if(
    hitTest(_root._xmouse,_root._ymouse)){
    //Your statements here;
    }

    Copy Paste the above code in movieclip.
    Where is this Flash Rocket Taking us ^|^

  15. #15
    Actionscript Nerd
    Join Date
    Apr 2002
    Location
    USA
    Posts
    47
    don't need a "this." before the hitTest?
    that's something good to know. just implied when inside a movieClip script?
    ••••••••••••••••••••••••••••••••••••••••••••••••••
    Try out the all new Ball Drop 2.0!
    http://www.mtsu.edu/~jwt2h/balldrop/

  16. #16
    Senior Member devnull_2k's Avatar
    Join Date
    Oct 2001
    Location
    Limehouse, Ontario - Never heard of it? Not surprised.
    Posts
    785
    If you do not write "this", ActionScript assumes you mean "this". I for one write it anyways, as it is just good coding practice. You can go back to it, and glide right over code and know whats happening because the "this" is highlighted.
    The future belongs to those who prepare for it today.

  17. #17
    Senior Member mbenney's Avatar
    Join Date
    Mar 2001
    Posts
    2,744
    a simple way of checking circles against all other shapes is to put an invisible edge round your shapes, that is the radius of the circle) and test the mid point of the circle, giving the impression of seamless collision detection

    nb only works if one shape is a circle

  18. #18
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    Originally posted by Psytrix
    Could you explain that code a lil better, im not sure exactly how it works...?
    Have a look at the fla (one of my faves):
    Attached Files Attached Files

  19. #19
    SaphuA mosterdfles_flash's Avatar
    Join Date
    Jan 2002
    Location
    Tha Couch
    Posts
    935

    ah well

    The only problem is that the movement in computer is not the same as in realLife...

    In realLife when an object goes for about 5 meter per second and then hits a very strong wall... a hit has been done...

    But with the computer, when an object moevs for 50 pixels and between that 'jump' is a small wall... theres no hit

    could someone please fix this? (I.m trying to but with no succes :S

    heres a small .fla with my problem (if you didn;t understand hat I ment ) (ITs MX!!!)
    Attached Files Attached Files
    'How Art Is The Visual While We’re Artificial…'

    Mail & MSN
    My Not Finisched Page!
    Nick: SaphuA

  20. #20
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    For this there are two fixes:

    Define the walls as sets of points (a bent line) and the balls movement as a vector. Then just test the vector against the line(s). This requires a good bit of algebra knowledge, but can be a better system in the long run for collisions (just find the gradient of the line) and mass hitTesting.

    OR

    Do a set of checks, ie. one for every 10px the ball will travel that frame.

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