Forums

ForumsProgramming Forum

How to make a perfect hitTest in as2?

Posted May 22, '11 at 10:59am

Oseb

Oseb

29 posts

Hi everybody!
Im working on a game, but when i make a hitTest its not work perfectly (stops before reach the object). I use this code:

onClipEvent (enterFrame) {
if (_root.MC1.hitTest(this)) {
_root.MC1._y-= 5;
}
}

Any idea how to make it? Please explain it please.

 

Posted May 23, '11 at 12:13am

arobegamr

arobegamr

130 posts

The reason is that in flash, the hitTest function only detects collisions between bounding boxes, which are perfect rectangles which encompass the entire clips.
The only way to create perfect collisions between objects without going through an in-depth math-based engine is to check one specific point with a full movie clip.

Example:
onClipEvent(enterFrame){
if(this.hitTest(_root._xmouse,_root._ymouse,true)){
trace("hit&quot
}
}


The function hitTest can be used in two ways:
mc1.hitTets(mc2)
or
mc1.hitTest(pointx,pointy,true)
The third parameter in the second method tells flash whether to use just the bounding box, or evaluate the exact shape of the clip, which only works if flash is working with a point rather than a second full movie clip.

 

Posted May 23, '11 at 7:38am

gaboloth

gaboloth

1,629 posts

Yeah, but remember that you can find loads of nice perfect hit test classes for free with google. For now, it's not fundamental that you understand how they work before using them.

 

Posted May 23, '11 at 10:25am

Oseb

Oseb

29 posts

@arobegamr

onClipEvent(enterFrame){
if(this.hitTest(_root._xmouse,_root._ymouse,true)){
trace("hit&quot
}
}

You mean in the example that: mouse is an object which you give the intance name: mouse?
and in the code you dont need "trace("hit&quot" right? You need to add the function there?

 

Posted May 23, '11 at 12:00pm

gaboloth

gaboloth

1,629 posts

no, _xmouse e _ymouse are the two built-in flash variables for the mouse coordinates, but you can replace them with the coordinates of the point you want to hitTest.
and yes, you have to replace trace("hit&quot with all the stuff that you want to happen if the collision is happening.

 

Posted May 23, '11 at 1:47pm

Oseb

Oseb

29 posts

but if a write "_xmouse" then which object will my hero hits?
can you write me the code: my character (which i move with tehe arrows) called hero, the object that my hero hits called wall? I would be really glad if you would do that for me.

 

Posted May 23, '11 at 7:13pm

arobegamr

arobegamr

130 posts

Perfect hitTests cannot refer to a second movieclip, only a point. In the second example, _xmouse is not a clip, but a coordinate. By specifying _xmouse,_ymouse, you are telling flash to detect a collision with the mouse cursor.

It is, however, possible to check all corners of the hero's bounding box, while using the full second movie clip. For an example of how to do this, check out tonypa's tutorial: [url=http://www.tonypa.pri.ee/tbw/start.html]

(The tutorial covers much more than just that, and there is a lot more useful information)

 

Posted May 24, '11 at 10:44am

Oseb

Oseb

29 posts

Thanks now i get it, but still dont understand xmouse and ymouse.
BTW is this code ok:

onClipEvent(enterFrame){
if(this.hitTest(_root._x=200,_root._y=200,true)){
this._y-= 5;
}
}

?

 

Posted May 29, '11 at 11:50am

stijnhero

stijnhero

24 posts

well i wil do onClipEvent(enterFrame) {
if(this.hit!test.'objectname{
your code
}
}

 

Posted May 29, '11 at 11:51am

stijnhero

stijnhero

24 posts

o w8 nClipEvent(enterFrame) {
if(_root.this.hit!test.'_root.objectname{
your code
}
}

 
Reply to How to make a perfect hitTest in as2?

You must be logged in to post a reply!