2010-05-19 100 views
2

因此,我正在從之前的項目(我在此處發佈代碼審閱)升級這一次,實施重複背景(如漫畫上使用的內容),以便SDL不會必須爲一個關卡加載真正大的圖像。 但是,程序中存在奇怪的不一致:用戶第一次向右滾動時,顯示的面板數量少於指定的數量。向後(向左)顯示正確數量的面板(即面板重複代碼中指定的次數)。之後,看起來再次右轉(一直在左側)顯示正確數量的面板,並且相同地向後。下面是一些選擇的代碼,這裏是一個.zip of all my codeSDL橫向卷軸不一致滾動

構造:

Game::Game(SDL_Event* event, SDL_Surface* scr, int level_w, int w, int h, int bpp) { 
    this->event = event; 
    this->bpp = bpp; 
    level_width = level_w; 
    screen = scr; 
    w_width = w; 
    w_height = h; 

    //load images and set rects 
    background = format_surface("background.jpg"); 
    person = format_surface("person.png"); 

    background_rect_left = background->clip_rect; 
    background_rect_right = background->clip_rect; 
    current_background_piece = 1; //we are displaying the first clip 
    rect_in_view = &background_rect_right; 
    other_rect = &background_rect_left; 

    person_rect = person->clip_rect; 

    background_rect_left.x = 0; background_rect_left.y = 0; 
    background_rect_right.x = background->w; background_rect_right.y = 0; 
    person_rect.y = background_rect_left.h - person_rect.h; 
    person_rect.x = 0; 
} 

和這裏的移動方法,它可能是導致所有麻煩:

void Game::move(SDLKey direction) { 
if(direction == SDLK_RIGHT) { 
    if(move_screen(direction)) { 
     if(!background_reached_right()) { 
      //move background right 

      background_rect_left.x += movement_increment; 
      background_rect_right.x += movement_increment; 

      if(rect_in_view->x >= 0) { 
       //move the other rect in to fill the empty space 
       SDL_Rect* temp; 

       other_rect->x = -w_width + rect_in_view->x; 

       temp = rect_in_view; 
       rect_in_view = other_rect; 
       other_rect = temp; 

       current_background_piece++; 
       std::cout << current_background_piece << std::endl; 
      } 

      if(background_overshoots_right()) { 
       //sees if this next blit is past the surface 
       //this is used only for re-aligning the rects when 
       //the end of the screen is reached 

       background_rect_left.x = 0; 
       background_rect_right.x = w_width; 
      } 
     } 
    } 
    else { 
     //move the person instead 

     person_rect.x += movement_increment; 
     if(get_person_right_side() > w_width) { 
      //person went too far right 

      person_rect.x = w_width - person_rect.w; 
     } 
    } 
} 

else if(direction == SDLK_LEFT) { 
    if(move_screen(direction)) { 
     if(!background_reached_left()) { 
      //moves background left 

      background_rect_left.x -= movement_increment; 
      background_rect_right.x -= movement_increment; 

      if(rect_in_view->x <= -w_width) { 
       //swap the rect in view 
       SDL_Rect* temp; 

       rect_in_view->x = w_width; 

       temp = rect_in_view; 
       rect_in_view = other_rect; 
       other_rect = temp; 

       current_background_piece--; 
       std::cout << current_background_piece << std::endl; 
      } 

      if(background_overshoots_left()) { 
       background_rect_left.x = 0; 
       background_rect_right.x = w_width; 
      } 
     } 
    } 
    else { 
     //move the person instead 

     person_rect.x -= movement_increment; 
     if(person_rect.x < 0) { 
      //person went too far left 

      person_rect.x = 0; 
     } 
    } 
} 
} 

沒有代碼的其餘部分這並未」沒有太大意義。由於它太多了,我會上傳它here進行測試。無論如何,有誰知道我可以如何解決這種不一致?

+0

我記得有一個類似的問題。但是,完整代碼的鏈接似乎已破解,您可以更新它嗎? – raven 2010-09-18 10:17:32

回答

0

打印出代表您認爲可能會懷疑的數據的值,並確保它們正在改變您認爲應該的方式。這聽起來像是一個非常典型的fencepost error