2010-05-19 6 views
2

donc je travaille sur une mise à niveau de mon projet précédent (que j'ai posté ici pour la révision du code) cette fois la mise en œuvre d'un arrière-plan de répétition (comme ce qui est utilisé sur les dessins animés) de sorte que SDL ne avoir à charger de très grandes images pour un niveau. Il y a une étrange inconséquence dans le programme, cependant: la première fois l'utilisateur fait défiler tout le chemin à droite 2 panneaux moins sont représentés que ce qui est indiqué. En retournant (à gauche), le nombre correct de panneaux est affiché (c'est-à-dire que les panneaux répètent le nombre de fois spécifié dans le code). Après cela, il semble que le bon nombre de panneaux soit affiché et que ce soit à reculons (une fois tout le chemin à gauche). Voici un code sélectionné et est ici un constructeur .zip of all my codedéfile à défilement horizontal SDL inconsistantly

:

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; 
} 

et voici la méthode de déplacement qui est probablement à l'origine tous les problèmes:

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; 
     } 
    } 
} 
} 

sans que le reste du code ce n » Je fais trop de sens. Comme il y a trop de ce que je vais le télécharger here pour les tests. Quoi qu'il en soit, est-ce que quelqu'un sait comment je pourrais corriger cette incohérence?

+0

Je me souviens d'avoir un problème similaire. Cependant, le lien vers le code complet semble cassé, pourriez-vous le mettre à jour? – raven

Répondre

0

Imprimer les valeurs qui représentent les données que vous pensez peut-être suspect, et assurez-vous qu'ils changer la façon dont vous pensez qu'ils devraient être. Cela ressemble à un fencepost error assez typique.

Questions connexes