|
|
|
#1 |
|
Gross Pecululatarian
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
}
Code:
if(this.hitTest(x, y, true)){
// Something
}
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 08:27 AM. |
|
|
|
|
|
#2 |
|
Strange Life
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; 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);
}
}
}
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. |
|
|
|
|
|
#3 |
|
ism
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 |
|
|
|
|
|
#4 |
|
Gross Pecululatarian
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 | |
|
Not PWD
Join Date: May 2001
Posts: 1,798
|
Quote:
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. |
|
|
|
|
|
|
#6 |
|
SaphuA
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 Were Artificial ' Mail & MSN My Not Finisched Page! Nick: SaphuA Last edited by Ed Mack; 11-06-2002 at 05:50 PM. |
|
|
|
|
|
#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 |
|
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 |
|
Gross Pecululatarian
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 |
|
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 | |
|
I'm dope.
Join Date: Aug 2002
Location: Slidell, LA
Posts: 309
|
Quote:
|
|
|
|
|
|
|
#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 |
|
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 |
|
Junior Member
Join Date: Nov 2002
Posts: 18
|
try this
PHP Code:
__________________
Where is this Flash Rocket Taking us ^|^ |
|
|
|
|
|
#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 |
|
Senior Member
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 |
|
Senior Member
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 |
|
|
|
|
|
#18 | |
|
Gross Pecululatarian
Join Date: Dec 2001
Location: UK
Posts: 2,954
|
Quote:
|
|
|
|
|
|
|
#19 |
|
SaphuA
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!!!)
__________________
'How Art Is The Visual While Were Artificial ' Mail & MSN My Not Finisched Page! Nick: SaphuA |
|
|
|
|
|
#20 |
|
Gross Pecululatarian
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. |
|
|
|
![]() |
|
||||||
| Thread Tools | |
| Display Modes | |
|
|