Way to increment objects inside an array for 2 hit

2019-03-07 07:55发布

问题:

Im creating a basic game using SDL/C++. I need a way to implement 2 hit detection. When just trying one hit it works fine. Here is what i have for the two hit detection:

  int maxHit = 2;
  int hitCount = 0;

  // Detect collisions
  for(auto p : projectiles) {
    for(auto a : aliens) {

    if(p->CollidesWith(a) && hitCount == maxHit)
    {
        p->HandleCollision();
        a->HandleCollision();       
    }  
        if(p->CollidesWith(a) && hitCount != maxHit) {  
        ++hitCount; 
      }
    }

  }

For some reason it works on a select few of the enemies on the screen and doesn't for others.

EDITED TO MAKE IT CLEARER

回答1:

Yes, this identifies the object on which the method was called. In C++ if you use the this keyword explicitly when accessing a member, you need the -> access operator instead of . because it is a pointer, not a reference.

But, of course, you would usually just write ++ hits with no this.



标签: c++ sdl this