I'm writing the Contra Game by Directx9 and c++ please help me about list of bullets
i'm trying below code but it's error: vector intertor incompatible
std::vector<Bullet*> bullets
if (mKeyboard->IsKeyPress(DIK_X))
{
Bullet* toShoot = new Bullet(noneType, _position.x, _position.y, RIGHT);
toShoot->Init();
bullets.push_back(toShoot);
}
Update Funtion:
std::vector<Bullet*>::iterator it = bullets.begin();
while ((it) != bullets.end())
{
(*it)->Update(gameTime, c);
if ((*it)->IsLive() == false)
{
bullets.erase(it++);
}
}
Render funtion
std::vector<Bullet*>::iterator it = bullets.begin();
while (it != bullets.end())
{
if ((*it)->IsLive())
{
(*it++)->Render(gr, cx, cy);
}
}
You can't just increment an iterator passed to
erase(…)
. Do this instead:Your Render function has a different bug. It gets stuck on the first non-live bullet, since the increment is inside the if-block. This is one reason
for(…)
is usually preferable towhile(…)
:In fact, the Update function should be likewise changed, but omit the
++it
.