Be a Supporter!
Archawn
Archawn
  • Member since: Sep. 9, 2007
  • Offline.
Forum Stats
Member
Level 27
Game Developer
hitTests -- WTF Jul. 18th, 2009 @ 10:47 AM Reply

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?

Johnny
Johnny
  • Member since: Apr. 17, 2004
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to hitTests -- WTF Jul. 18th, 2009 @ 10:53 AM Reply

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))


Perpetually looking for time to return to the arts.

BBS Signature
51lver
51lver
  • Member since: Jan. 14, 2008
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to hitTests -- WTF Jul. 18th, 2009 @ 10:53 AM Reply

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");
}
}
Archawn
Archawn
  • Member since: Sep. 9, 2007
  • Offline.
Forum Stats
Member
Level 27
Game Developer
Response to hitTests -- WTF Jul. 18th, 2009 @ 10:59 AM Reply

Thanks guys... hitTests are messed up IMO.

CHINESEMANZOMG
CHINESEMANZOMG
  • Member since: Apr. 1, 2009
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to hitTests -- WTF Jul. 18th, 2009 @ 11:03 AM Reply

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.

Johnny
Johnny
  • Member since: Apr. 17, 2004
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to hitTests -- WTF Jul. 18th, 2009 @ 11:08 AM Reply

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.


Perpetually looking for time to return to the arts.

BBS Signature
Archawn
Archawn
  • Member since: Sep. 9, 2007
  • Offline.
Forum Stats
Member
Level 27
Game Developer
Response to hitTests -- WTF Jul. 18th, 2009 @ 11:35 AM Reply

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.

Johnny
Johnny
  • Member since: Apr. 17, 2004
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to hitTests -- WTF Jul. 18th, 2009 @ 11:43 AM Reply

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.


Perpetually looking for time to return to the arts.

BBS Signature
Archawn
Archawn
  • Member since: Sep. 9, 2007
  • Offline.
Forum Stats
Member
Level 27
Game Developer
Response to hitTests -- WTF Jul. 18th, 2009 @ 11:50 AM Reply

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

zedd56
zedd56
  • Member since: Aug. 31, 2007
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to hitTests -- WTF Jul. 18th, 2009 @ 12:16 PM Reply

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?

UnknownFury
UnknownFury
  • Member since: Aug. 10, 2005
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to hitTests -- WTF Jul. 18th, 2009 @ 12:25 PM Reply

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.

Coolio-Niato
Coolio-Niato
  • Member since: Jun. 30, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to hitTests -- WTF Jul. 18th, 2009 @ 12:30 PM Reply

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.

4urentertainment
4urentertainment
  • Member since: Aug. 1, 2008
  • Offline.
Forum Stats
Moderator
Level 13
Game Developer
Response to hitTests -- WTF Jul. 18th, 2009 @ 02:44 PM Reply

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..