To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > General Help > Games

Reply
 
Thread Tools Display Modes
Old 11-01-2002, 12:57 PM   #1
Ed Mack
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 )
__________________
( EdMack.com )( Flash Source Files )

Last edited by Ed Mack; 11-02-2002 at 08:27 AM.
Ed Mack is offline   Reply With Quote
Old 11-02-2002, 07:33 AM   #2
strangelife2k
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 ]
__________________
(Webcamall

Last edited by Ed Mack; 11-02-2002 at 08:27 AM.
strangelife2k is offline   Reply With Quote
Old 11-03-2002, 03:36 AM   #3
BlinkOk
ism
 
BlinkOk's Avatar
 
Join Date: Aug 2001
Location: , location, location
Posts: 4,969
__________________
Graphics Attract, Motion Engages, Gameplay Addicts
XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro
BlinkOk is offline   Reply With Quote
Old 11-03-2002, 06:25 AM   #4
Ed Mack
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);
	}
}
__________________
( EdMack.com )( Flash Source Files )
Ed Mack is offline   Reply With Quote
Old 11-05-2002, 11:50 AM   #5
ViRGo_RK
Not PWD
 
ViRGo_RK's Avatar
 
Join Date: May 2001
Posts: 1,798
Quote:
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 f**k down.
Gerbick: America. Stabbing suckers since Vespucci left.
ViRGo_RK is offline   Reply With Quote
Old 11-06-2002, 02:47 PM   #6
mosterdfles_flash
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)

__________________
'How Art Is The Visual While We’re Artificial…'

Mail & MSN
My Not Finisched Page!
Nick: SaphuA

Last edited by Ed Mack; 11-06-2002 at 05:50 PM.
mosterdfles_flash is offline   Reply With Quote
Old 11-11-2002, 10:30 PM   #7
NeotiK
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
NeotiK is offline   Reply With Quote
Old 11-13-2002, 03:32 PM   #8
flashGaul
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 ^|^
flashGaul is offline   Reply With Quote
Old 11-13-2002, 05:52 PM   #9
Ed Mack
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.
__________________
( EdMack.com )( Flash Source Files )
Ed Mack is offline   Reply With Quote
Old 11-14-2002, 01:40 AM   #10
flashGaul
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 ^|^
flashGaul is offline   Reply With Quote
Old 11-14-2002, 09:08 AM   #11
Psytrix
I'm dope.
 
Psytrix's Avatar
 
Join Date: Aug 2002
Location: Slidell, LA
Posts: 309
Quote:
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...?
Psytrix is offline   Reply With Quote
Old 11-16-2002, 05:33 PM   #12
jwtheiss
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/
jwtheiss is offline   Reply With Quote
Old 11-17-2002, 04:49 AM   #13
joe_m_87
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
joe_m_87 is offline   Reply With Quote
Old 11-17-2002, 12:59 PM   #14
flashGaul
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 ^|^
flashGaul is offline   Reply With Quote
Old 11-17-2002, 02:11 PM   #15
jwtheiss
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/
jwtheiss is offline   Reply With Quote
Old 11-17-2002, 02:37 PM   #16
devnull_2k
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.
devnull_2k is offline   Reply With Quote
Old 11-17-2002, 02:50 PM   #17
mbenney
Senior Member
 
mbenney's Avatar
 
Join Date: Mar 2001
Posts: 2,741
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
mbenney is offline   Reply With Quote
Old 11-17-2002, 05:46 PM   #18
Ed Mack
Gross Pecululatarian
 
Ed Mack's Avatar
 
Join Date: Dec 2001
Location: UK
Posts: 2,954
Quote:
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
File Type: fla art_hit_rpg1.fla (18.0 KB, 1693 views)
__________________
( EdMack.com )( Flash Source Files )
Ed Mack is offline   Reply With Quote
Old 11-18-2002, 08:52 AM   #19
mosterdfles_flash
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
File Type: fla hittest example.fla (10.5 KB, 1055 views)
__________________
'How Art Is The Visual While We’re Artificial…'

Mail & MSN
My Not Finisched Page!
Nick: SaphuA
mosterdfles_flash is offline   Reply With Quote
Old 11-18-2002, 11:27 AM   #20
Ed Mack
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.
__________________
( EdMack.com )( Flash Source Files )
Ed Mack is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > General Help > Games

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:24 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.