2
\$\begingroup\$

So I have an issue that I'm laughing at myself about, because it really seems like it should be something that I should be able to figure out pretty quickly.

I am designing a 2D action platformer; I have a playable character, and a dummy 'punching bag' character for testing purposes that I've created. I've just gotten enough of both of them done that I can start prototyping and testing them in runtime.

Then I realized- neither of them have references of each other (intentionally so), so how do I check for hitboxes stored within my playable character from my dummy character?

Long story short, how do I make my dummy know when he's been punched by my hero?

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I won't write a full answer, because really "it depends". However I think this answer stackoverflow.com/a/5459955/165500 might give you some inspiration. \$\endgroup\$ Commented Jun 20, 2012 at 4:42
  • \$\begingroup\$ "it depends" could be true of almost every situation in programming. Regardless, thanks for the link! \$\endgroup\$ Commented Jun 20, 2012 at 4:45

1 Answer 1

2
\$\begingroup\$

You don't need them to have references to each other.

Have a central hitbox system for your stage that does the work for them

Your hitbox system must have a list of all the hitboxes relevant to it - you'd generally have one hitbox system per world. How it finds out about its hitboxes is up to you, but the approach I take is (skipping all the detail) that every hitbox knows about the stage it's on, so it registers itself by calling: this.Stage.HitboxSystem.Add(this)

Once per update, this hitbox system goes through all the hitboxes known to it and lets them know if they're colliding with any other hitboxes (giving them whatever information is necessary, e.g. a reference to the other hitbox).

Next time your playable character or your "punching bag" need to know if they're touching anything, they've already been informed by the hitbox system and don't need to go check for themselves first.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for your input, this makes a lot of sense to me. I'll try it! \$\endgroup\$ Commented Jun 20, 2012 at 21:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .