00:00
00:00
Newgrounds Background Image Theme

tippy104 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

hitTests -- WTF

853 Views | 12 Replies
New Topic Respond to this Topic

hitTests -- WTF 2009-07-18 10:47:28


How come this code "works":

onEnterFrame = function(){
  if(player.hitTest(_root.ground)){
    trace("hitting");
  }
}

and these don't:

onEnterFrame = function(){
  if(player.hitTest(_root.ground._x, _root.ground._y, true)){
    trace("hitting");
  }
}
onEnterFrame = function(){
  if(player.hitTest(_root.ground._x, _root.ground._y, false)){
    trace("hitting");
  }
}

WTF? I'd use the first one, but that registers hitTests with the bounding box, not the actual shape.

What's wrong?

Response to hitTests -- WTF 2009-07-18 10:53:39


You have to do it the other way around. The ground's x and y (it's registration point) are at one small point on the ground movieclip (Look for the little 'cross')... so it will only check if the player is hitting that tiny point, and not anywhere on the ground.

What you want to do is put the player's registration point at his feet, and then check the ground AGAINST that.

if(ground.hitTest(player._x, player._y, true))


Writer @ www.Johnrickett.com

Response to hitTests -- WTF 2009-07-18 10:53:43


because you're checking the ground's _x, _y not the players, so the player will have to hit the one spot on the ground for the hittest to work

onEnterFrame = function(){
if(player.hitTest(_root.ground._x, _root.ground._y, true)){
trace("hitting");
}
}

try:

onEnterFrame = function(){
if(_root.ground.hitTest(player_x, player._y, true)){
trace("HITTING");
}
}

Response to hitTests -- WTF 2009-07-18 10:59:06


Thanks guys... hitTests are messed up IMO.

Response to hitTests -- WTF 2009-07-18 11:03:03


At 7/18/09 10:59 AM, Archon68 wrote: Thanks guys... hitTests are messed up IMO.

No, you just don't know how to use them.

Response to hitTests -- WTF 2009-07-18 11:08:16


At 7/18/09 11:03 AM, CHINESEMANZOMG wrote:
At 7/18/09 10:59 AM, Archon68 wrote: Thanks guys... hitTests are messed up IMO.
No, you just don't know how to use them.

No, I know how to use them... and I agree with him. It's backwards.

It is more intuitive to check if the player is hitting the ground, not if the ground is hitting the player, so for someone new to programming, that's what they're going to check. If the player is hitting the ground.

In real life, you'd say "Am I standing on the ground?" not "Is the ground keeping me from falling through it"

Blah.. blah.. newton's law, equal/opposite reaction, they're hitting each other I know... but from a basic view, it's more natural to check if the player is hitting the ground.


Writer @ www.Johnrickett.com

Response to hitTests -- WTF 2009-07-18 11:35:25


At 7/18/09 11:08 AM, Johnny wrote: It is more intuitive to check if the player is hitting the ground, not if the ground is hitting the player, so for someone new to programming, that's what they're going to check. If the player is hitting the ground.

I'm not new to programming, just annoyed with hitTests, I can never remember which way to use them.

Other than that, I agree with you completely.

Response to hitTests -- WTF 2009-07-18 11:43:50


At 7/18/09 11:35 AM, Archon68 wrote: I'm not new to programming, just annoyed with hitTests, I can never remember which way to use them.

Please don't argue that.

If you had trouble remembering how to properly use a hitTest, to the point where you had to ask for advice on one of the most basic methods and to not be able to easily figure out that the position of a registration point was the culprit, you're still new enough to actionscript to warrant being called so. It's not an attack at you. It's a reality is all.

If you argue against it, then instead of being 'new'... you're just a little 'slow.'

So you can take 'slow' or 'new'

Your choice.


Writer @ www.Johnrickett.com

Response to hitTests -- WTF 2009-07-18 11:50:47


Fine, I'm relatively new to hitTests, just not AS in General.

Response to hitTests -- WTF 2009-07-18 12:16:10


the entire issue is that you cannot check for contact between the true shapeflags of both objects. You can test a true shapeflag against a bounding box or a true shapeflag against a point. Not a true shapeflag against another true shapeflag, which we game designers need oh so badly.

Very stupid imo, could probably be done by adobe but meh, what are you gonna do?

Response to hitTests -- WTF 2009-07-18 12:25:22


At 7/18/09 12:16 PM, zedd56 wrote: Very stupid imo, could probably be done by adobe but meh, what are you gonna do?

Implement it yourself. There isn't a mega quick way of just testing any two shapes, it depends on your set up.

Response to hitTests -- WTF 2009-07-18 12:30:28


Once you start going into vector collision detections, all your hitTest troubles will be gone. Otherwise, just learn how to properly use hitTest and it'll work great 95% of the time.

Response to hitTests -- WTF 2009-07-18 14:44:06


At 7/18/09 12:30 PM, Coolio-Niato wrote: Once you start going into vector collision detections, all your hitTest troubles will be gone. Otherwise, just learn how to properly use hitTest and it'll work great 95% of the time.

I know bitmap collision detection, but I never heard of vector collision detection..