Problem with inheritance

edited January 2018 in Questions about Code

Hi this is my first post here. Im doing a game, i got the superclass object Living_Body, that should manage collissions, health, etc. Then i got the object Enemy_Swordman that manage the other stuff. This is a sub-class that inherites from living_body

The problem is that i can't get working the method overlaps() from the living_body parent, but if i put the overlaps in the Enemy class then it works, also i can get the health variable from the parent, so the inheritance it is working.

So i read a lot about inheritance, but it seems to bee all ok, but maybe you can help me with this.

When i run the game, the enemys in the map are moving but they dont collission each other, like if the overlaps method dosn't exist.

this is the overlaps method:

void overlaps(Enemy_Swordman other){
  float d = dist(posx, posy, other.posx, other.posy);
  if (d < diameter / 4 + diameter / 4)
  {
    if (posx > other.posx) {
      posx += 3; other.posx -= 3;
    } else {
      posy -= 3; other.posy += 3;
    }
    if (posy > other.posy) {
      posy += 3; other.posy -= 3;
    } else {
      posy -= 3; other.posy += 3;
    }     
  }
}

Thanks !

Answers

  • You're better off not mixing collision check & movement code in the same method. :-\"

  • do you mean posy in line 8?

  • But that wouldn't fix anything, is the same problem, the inherited method is not checking the collission

  • That's the only code you posted. Can't fix code we can't see.

Sign In or Register to comment.