所以我只是試圖讓一個紅色的10 x 10盒子來回垂直移動。我編譯並運行我的程序,紅框出現開始向下移動,然後在碰到屏幕邊緣後消失。我使用了一些cout <<
語句,它們告訴我什麼時候函數被調用,並且它們在應該被調用時都被調用。即使在看不見盒子的情況下,函數也能正確地被調用。矩形與SDL一起消失
我的主循環
while(running)
{
myScreen->Clear();
boxes.Move();
boxes.Draw();
myScreen->Flip();
........
我draw()函數
SDL_Color red;
red.r = 255;
red.g = 0;
red.b = 0;
if(SDL_FillRect(my_screen->Get_screen(), &start_dest, SDL_MapRGB(
my_screen->Get_pixel_format(), red.r, red.g, red.b)) == -1)`
cout << "Fill rect in Draw(); failed\n";
我的移動()函數
start_dest.y += y_step;
if (start_dest.y >= my_screen->Get_height())
{
cout << "start_dest.y >= screen height\n";
start_dest.y = my_screen->Get_height();
y_step = -y_step;
}
if (start_dest.y <= 0)
{
cout << "start_dest.y <= 0\n";
start_dest.y = 0;
y_step = -y_step;
}
我一直在試圖找到永遠這個bug。只要有人想看更多的代碼就留下評論。由於
只是一個小竅門:如果你使用真正的調試器,你會發現bug比使用'cout'來填充你的程序要快得多,並且更加簡潔。 – 2009-12-24 19:58:51
也許這個錯誤是由源代碼中多餘的感嘆號造成的。 – avakar 2009-12-24 20:00:44
請告訴我:Get_height()返回什麼值,以及y_step的初始值是什麼。在我看來,他們喜歡以不同的單位說話,然後永遠不會達到高潮,方向永遠不會顛倒。 – 2009-12-24 20:10:26