Forums

How to make a perfect hitTest in as2?

Oseb: Your code isn't ok, what you're effectively doing is setting variables in your hittest coding - _root._x = 200. Depending on the outcome of setting this it will return either true or false I believe, but not any usable Number. Either remove the =200, or try getting your x and y values from somewhere else

Oseb: Your code isn't ok, what you're effectively doing is setting variables in your hittest coding - _root._x = 200. Depending on the outcome of setting this it will return either true or false I believe, but not any usable Number. Either remove the =200, or try getting your x and y values from somewhere else :)

I think I know what you're trying to do, Oseb, but if you want to detect collision between the object and the coordinate (200,200), then you need to remove the _root._x=

Just have this as you're code:onClipEvent(enterFrame){if(this.hitTest(200,200,true)){this._y-=5}

I think I know what you're trying to do, Oseb, but if you want to detect collision between the object and the coordinate (200,200), then you need to remove the _root._x=\r\n\r\nJust have this as you're code:\r\n[i]onClipEvent(enterFrame){\r\nif(this.hitTest(200,200,true)){\r\nthis._y-=5\r\n}[/i]

Thanks arobegamr!But i have problem with the tird line: this._y-=5 if i add this then when my character reach the point he will be bounce back and if i hold the button then he will hit the wall perfectly, now i want to do the same. I mean don't dounce back just hit it.

Thanks arobegamr!\r\nBut i have problem with the tird line: this._y-=5 if i add this then when my character reach the point he will be bounce back and if i hold the button then he will hit the wall perfectly, now i want to do the same. I mean don't dounce back just hit it.

Now i want this code with only y cordinate in. Then how is it writen?Because i deleted the first 200 but then the second one became the firts one (its became from Y to X coordinate). I want that my char wont go under Y 200 (its like a floor).

onClipEvent(enterFrame){\r\nif(this.hitTest(200,200,true)){\r\nthis._y-=5\r\n}\r\n\r\nNow i want this code with only y cordinate in. Then how is it writen?\r\nBecause i deleted the first 200 but then the second one became the firts one (its became from Y to X coordinate). I want that my char wont go under Y 200 (its like a floor).

Okay, firstly, _root is simply a Movie Clip. It refers to the stage itself. You know how you can place symbols within symbols? Well, all of the flash game's symbols are being placed inside _root. So if you say _root._x = 200, you're going to move EVERYTHING over by 200, because you're changing the origin (anchor point) of the stage.

I think you may have meant "this._x", or just "_x"; in most cases both mean the same thing, and refer to the x-coordinate of which movie clip your function is written for. Or better yet, just use numbers:

this.hitTest(200,200,true);

That will check against the point at x=200, y=200.

Also, what specifically are you trying to collide? Sometimes, a bounding circle (a circle around the object that collides with other objects) can be MUCH better than a bounding box. It's also much easier to write yourself and much more efficient:

Say that every object has a radius r that you assign them. This is the radius of their bounding circle. To check whether two bodies overlap, all you need to do is check the distance between them against the sum of their radii; if the radii add up to more than the distance between them, then there's no way for them not to overlap. Sorry, this diagram should make it clearer:

You could also set the radius up arbitrarily as half of the width of the MovieClip, so that you don't have to define variables. If you had one MovieClip named my_mc (I should say "if you had one reference to a MovieClip", but that's more technical Object-Oriented talk) and another named enemy_mc, the collision code within the my_mc object would look like:

This is pretty sloppy code; some easy optimizations would be to use ( )*( ) instead of ( )^2, because the exponent operator is slower than the multiplication one. Also, instead of ( )/2 + ( )/2 it could have been (( ) + ( ))/2. I just wrote it out so it would be easier to read.

So yeah, this uses a simple Pythagorean distance equation and checks it against the sum of their "radii". Circular bounding-boxes can provide some of the easiest and smoothest collisions you'd need, although they don't work in all occasions. If you want me to continue expounding on their use, let me know!

[quote]onClipEvent(enterFrame){\r\nif(this.hitTest(_root._x=200,_root._y=200,true)){\r\nthis._y-= 5;\r\n}\r\n}[/quote]\r\n\r\nOkay, firstly, _root is simply a Movie Clip. It refers to the stage itself. You know how you can place symbols within symbols? Well, all of the flash game's symbols are being placed inside _root. So if you say _root._x = 200, you're going to move EVERYTHING over by 200, because you're changing the origin (anchor point) of the stage.\r\n\r\nI think you may have meant "this._x", or just "_x"; in most cases both mean the same thing, and refer to the x-coordinate of which movie clip your function is written for. Or better yet, just use numbers:\r\n\r\nthis.hitTest(200,200,true);\r\n\r\nThat will check against the point at x=200, y=200.\r\n\r\nAlso, what specifically are you trying to collide? Sometimes, a bounding circle (a circle around the object that collides with other objects) can be MUCH better than a bounding box. It's also much easier to write yourself and much more efficient:\r\n\r\nSay that every object has a radius r that you assign them. This is the radius of their bounding circle. To check whether two bodies overlap, all you need to do is check the distance between them against the sum of their radii; if the radii add up to more than the distance between them, then there's no way for them not to overlap. Sorry, this diagram should make it clearer:\r\n\r\n[img]https://lh6.googleusercontent.com/-2o3uJx1a8U0/TeO7mUWdpCI/AAAAAAAAAA8/DWpFsTksgH4/BoundingCircle_Diagram.jpg[/img]\r\n\r\nYou could also set the radius up arbitrarily as half of the width of the MovieClip, so that you don't have to define variables. If you had one MovieClip named my_mc (I should say "if you had one reference to a MovieClip", but that's more technical Object-Oriented talk) and another named enemy_mc, the collision code within the my_mc object would look like:\r\n\r\n[quote]onClipEvent(enterFrame) {\r\n\r\nif(Math.sqrt((_x-_root.enemy_mc._x)^2 + (_y-_root.enemy_mc._y)^2) > (_width/2 + _root.enemy_mc._width/2)) {\r\n//do this stuff if we overlap\r\n}\r\n\r\n}[/quote]\r\n\r\nThis is pretty sloppy code; some easy optimizations would be to use ( )*( ) instead of ( )^2, because the exponent operator is slower than the multiplication one. Also, instead of ( )/2 + ( )/2 it could have been (( ) + ( ))/2. I just wrote it out so it would be easier to read.\r\n\r\nSo yeah, this uses a simple Pythagorean distance equation and checks it against the sum of their "radii". Circular bounding-boxes can provide some of the easiest and smoothest collisions you'd need, although they don't work in all occasions. If you want me to continue expounding on their use, let me know! :)

[img]http://kepfeltoltes.hu/view/110530/ma_www.kepfeltoltes.hu_.jpg[/img]\r\n\r\n\r\nI want that my blue circle stops on the black wall. If my wall intance name is wall and my character's is char. Then with this code how is it sounds like?\r\n\r\nonClipEvent(enterFrame) {\r\nif(Math.sqrt((_x-_root.wall_char._x)^2 + (_y-_root.wall_char._y)^2) > (_width/2 + _root.wall_char._width/2)) {\r\n_root.char._y-= 5;\r\n}\r\n}\r\n\r\nthis?

Sorry, sorry, I was unclear. My code works only for circle-on-circle collisions.

For circle-rectangle or rectangle-rectangle something more will be needed. Hmm...

Could you describe what you want in more detail? I can't see the picture

[quote]Then with this code how is it sounds like?\r\n\r\nonClipEvent(enterFrame) {\r\nif(Math.sqrt((_x-_root.wall_char._x)^2 + (_y-_root.wall_char._y)^2) > (_width/2 + _root.wall_char._width/2)) {\r\n_root.char._y-= 5;\r\n}\r\n}\r\n\r\nthis?[/quote]\r\n\r\nSorry, sorry, I was unclear. My code works only for circle-on-circle collisions.\r\n\r\nFor circle-rectangle or rectangle-rectangle something more will be needed. Hmm...\r\n\r\nCould you describe what you want in more detail? I can't see the picture :(

Rectangle-rectangle collisions are performed by cross-checking each line segment algebraically, (the way you would solve linear systems in your math class), with the extra step of making sure that the intersection of the two lines lies on the segment.

Rectangle-circle collisions are more difficult, and done by finding the intersection between each segment on the rectangle with the line perpendicular to that segment and passing through the center of the circle, then checking if the point lies within the circle. (If the distance between the point and the center of the circle is less than the circle's radius)

Remember that collision detection and collision reaction are two completely different operations, the latter being the more complicated of the two.

Rectangle-rectangle collisions are performed by cross-checking each line segment algebraically, (the way you would solve linear systems in your math class), with the extra step of making sure that the intersection of the two lines lies on the segment.\r\n\r\nRectangle-circle collisions are more difficult, and done by finding the intersection between each segment on the rectangle with the line perpendicular to that segment and passing through the center of the circle, then checking if the point lies within the circle. (If the distance between the point and the center of the circle is less than the circle's radius)\r\n\r\nRemember that collision detection and collision reaction are two completely different operations, the latter being the more complicated of the two.

Flip a Coin...

Play free online games at Armor Games!

We strive to deliver the best online games experience on the internet, with thousands of free online games for kids, access to free mmorpg games, free online games for girls, online rpg games, fun online flash games, and more. We offer free flash games in many different genres: online shooting games, online puzzle games, online war games, free online car games, free online hidden object games and dozens more. This is the best place on the web to play online games for free... play on Armor Games! No matter what game style you prefer, we've got it here.

As one of the biggest free gaming websites, we offer thousands of new game reviews and ratings, making it easy for gamers to find new games every day. We offer only the best games from developers Jmtb02, Krin, ConArtist, Joey Betz, Tony Lavell, Louissi, Gameinabottle and of course Armor Games develops some of the best games around.

About Armor Games (ArmorGames.com, Armour Games, or Armored Games)

We are dedicated to providing our players the best online gaming experience on the internet! Visit every day to discover new games, achieve high scores, and participate in one of the most active gaming communities.

Submit a Game

Have your game played by millions of gamers! Armor Games welcomes game submissions from talented game developers. Not every game makes the cut, but if it does, we'll promote it on Armor Games and give you all the credit and link back to your site. Submit your game here and good luck!