我很困難。過去兩天我一直在嘗試Google搜索,但我無法弄清楚。我有一個叫做Player的類,另一個叫做從Ellemy繼承的Player。我有一個列表,它存儲我的子彈的座標並循環播放它們在播放器中。我試圖訪問和循環通過相同的列表來檢查在Enemy中建立在播放器上的碰撞,但它甚至不會進入循環。我猜不知它是空的,但爲什麼?C++列表循環
struct structShoot
{
float x;
float y;
};
class Player
{
private:
blablabla
protected:
list<structShoot>::iterator it;
list<structShoot> shoot_list;
structShoot test;
public:
void render(SDL_Surface* dest);
};
void Player::render(SDL_Surface* dest)
{
//Works fine, see the other loop down below
for(it = shoot_list.begin(); it != shoot_list.end();)
{
shoot.moveSet(it->x, it->y);
shoot.draw(dest);
it->y--;
if((it->y) < -25)
{
it = shoot_list.erase(it);
}
else
{
it++;
}
}
}
class Enemy : protected Player
{
public:
void render(SDL_Surface* dest);
};
void Enemy::render(SDL_Surface* dest)
{
SDL_Rect a, b;
//Does not enter loop!? Ever. Why?
for(it = shoot_list.begin(); it != shoot_list.end();)
{
SDL_Quit();
a.x = enemy.getX();
a.y = enemy.getY();
a.w = enemy.getWidth();
a.h = enemy.getHeight();
b.x = it->x;
b.y = it->y;
b.w = 10;
b.h = 19;
it->y--;
if (collision(a, b) == true)
{
SDL_Quit();
}
if(it->y < -25)
{
it = shoot_list.erase(it);
}
else
{
it++;
}
}
}
也許你在第一個循環中刪除了整個列表,所以第二個循環無關? – 2011-04-11 14:21:58
您是否曾嘗試在調試器中運行此操作或添加打印語句?例如,你有沒有檢查過'!shoot_list.empty()'? – 2011-04-11 14:22:24
你並沒有將'Enemy'對象的某種類型多態轉換爲'Player'指針,是嗎?如果是這樣的話,那麼因爲render()不是一個虛函數,任何對某個指針(* my_player).render()的調用只會調用'Player :: render()'而不是'Enemy ::渲染()'。 – Jason 2011-04-11 14:35:26