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 )
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 ]
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):
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.
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
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.
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.
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...?
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)
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.
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
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.