Way to increment objects inside an array for 2 hit detection c+

♀尐吖头ヾ 提交于 2019-12-03 01:19:20

问题


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.



来源:https://stackoverflow.com/questions/22657779/way-to-increment-objects-inside-an-array-for-2-hit-detection-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!