2011-09-27 101 views
1

我的源代碼出現問題。基本上,我試圖在2D地圖SDL移動我的角色

,這裏是我的武士階層移動精靈性格:

class Warrior 
{ 
     private : 
       int HP, SP; 
       int xPos, yPos; 
       int status; 
       char *name; 
       SDL_Surface *warrior; 
       Map m; 
     public : 
      int atk, def, matk, mdef; 
      Warrior(int, int, int, Map); 
      int getHP(); 
      bool isAlive(); 
      int getSP(); 
      int getX(); 
      int getY(); 
      void setX(int); 
      void setY(int); 
      void changeStatus(int); 
      void drawChar(); 
      void move(); 
      void deleteChar(); 
}; 
//constructor buat warrior 
Warrior::Warrior(int pos, int x, int y, Map _m) 
{ 
     HP = 250; 
     SP = 50; 
     atk = 75; 
     def = 20; 
     matk = 15; 
     mdef = 5; 

     warrior = NULL; 
     m = _m; 
     status = pos; 
     xPos = x; 
     yPos = y; 

} 

bool Warrior::isAlive() 
{ 
    if (HP > 0) return true; 
    return false; 
} 
//buat ngilangin character warrior-nya... 
//timpa ama sprite rumput di coord char-nya 
void Warrior::deleteChar() 
{ 
    char path = '0'; 
    m.drawTile(xPos, yPos, path); 
    m.drawTile(xPos, yPos+20, path); 

    SDL_Flip (SDL_SCREEN); 

} 
//buat gambar character 
void Warrior::drawChar() 
{  
     switch (status) 
     { 
      case WARRIOR_LEFT : 
        warrior = IMG_Load ("Sprites/Warrior/WARRIOR_LEFT.png"); 
        applySurface (xPos, yPos, warrior, SDL_SCREEN); 
        break; 
      case WARRIOR_RIGHT : 
        warrior = IMG_Load ("Sprites/Warrior/WARRIOR_RIGHT.png"); 
        applySurface (xPos, yPos, warrior, SDL_SCREEN); 
        break; 
      case WARRIOR_UP : 
        warrior = IMG_Load ("Sprites/Warrior/WARRIOR_UP.png"); 
        applySurface (xPos, yPos, warrior, SDL_SCREEN); 
        break; 
      case WARRIOR_DOWN : 
        warrior = IMG_Load ("Sprites/Warrior/WARRIOR_DOWN.png"); 
        applySurface (xPos, yPos, warrior, SDL_SCREEN); 
        break; 

      case WARRIOR_MOVE_LEFT : 
        warrior = IMG_Load ("Sprites/Warrior/WARRIOR_MOVE_LEFT.png"); 
        applySurface (xPos, yPos, warrior, SDL_SCREEN); 
        SDL_Flip(SDL_SCREEN); 

        SDL_Delay (100); 
        deleteChar(); 
        status = WARRIOR_LEFT; 
        warrior = IMG_Load ("Sprites/Warrior/WARRIOR_LEFT.png"); 
        applySurface (xPos, yPos, warrior, SDL_SCREEN); 
        break; 
      case WARRIOR_MOVE_RIGHT : 
        warrior = IMG_Load ("Sprites/Warrior/WARRIOR_MOVE_RIGHT.png"); 
        applySurface (xPos, yPos, warrior, SDL_SCREEN); 
        SDL_Flip(SDL_SCREEN); 

        SDL_Delay (100); 
        deleteChar(); 
        status = WARRIOR_RIGHT; 
        warrior = IMG_Load ("Sprites/Warrior/WARRIOR_RIGHT.png"); 
        applySurface (xPos, yPos, warrior, SDL_SCREEN); 
        break; 
      case WARRIOR_MOVE_UP : 
        warrior = IMG_Load ("Sprites/Warrior/WARRIOR_MOVE_UP.png"); 
        applySurface (xPos, yPos, warrior, SDL_SCREEN); 
        SDL_Flip(SDL_SCREEN); 

        SDL_Delay (100); 
        deleteChar(); 
        status = WARRIOR_UP; 
        warrior = IMG_Load ("Sprites/Warrior/WARRIOR_UP.png"); 
        applySurface (xPos, yPos, warrior, SDL_SCREEN); 
        break; 
      case WARRIOR_MOVE_DOWN : 
        warrior = IMG_Load ("Sprites/Warrior/WARRIOR_MOVE_DOWN.png"); 
        applySurface (xPos, yPos, warrior, SDL_SCREEN); 
        SDL_Flip(SDL_SCREEN); 

        SDL_Delay (100); 
        deleteChar(); 
        status = WARRIOR_DOWN; 
        warrior = IMG_Load ("Sprites/Warrior/WARRIOR_DOWN.png"); 
        applySurface (xPos, yPos, warrior, SDL_SCREEN); 
        break; 
     } 

     SDL_Flip(SDL_SCREEN); 
} 

int Warrior::getX() 
{ 
    return xPos; 
} 

int Warrior::getY() 
{ 
    return yPos; 
} 

void Warrior::setX(int x) 
{ 
    xPos = x; 
} 

void Warrior::setY(int y) 
{ 
    yPos = y; 
} 

int Warrior::getHP() 
{ 
    return HP; 
} 

int Warrior::getSP() 
{ 
    return SP; 
} 

void Warrior::changeStatus(int _status) 
{ 
    status = _status; 
} 
//jalannya karakter 
void Warrior::move() 
{ 
     if (event.type == SDL_KEYDOWN) 
     { 
      //hilangkan char-nya dolo 
      deleteChar(); 
      switch (event.key.keysym.sym) 
      { 
       case SDLK_UP : 
        // nge-cek collision dari coord peta-nya (Map m) 
        if (m.map[m.getLevel()][(yPos/20)-1][xPos] == '0') 
        { 
         yPos -= 20; 
         status = WARRIOR_MOVE_UP; 
        } 
        break; 
       case SDLK_DOWN : 
        if (m.map[m.getLevel()][(yPos/20)+1][xPos/20] == '0') 
        { 
         yPos += 20; 
         status = WARRIOR_MOVE_DOWN; 
        } 
       case SDLK_LEFT : 
        if (m.map[m.getLevel()][yPos/20][(xPos/20)-1] == '0') 
        { 
         xPos -= 20; 
         status = WARRIOR_MOVE_LEFT; 
        } 
       case SDLK_RIGHT : 
        if (m.map[m.getLevel()][yPos/20][(xPos/20)+1] == '0') 
        { 
         xPos += 20; 
         status = WARRIOR_MOVE_RIGHT; 
        } 
       } 
      //bru di-gambar 
      drawChar(); 
      SDL_Delay (100);  
     }     
} 

的問題是我不能移動它,程序完全沒有響應......我已經檢查過所有的精靈圖像,它工作正常。

回答

4
  1. 發佈您的主循環。
  2. 在您的代碼的各個位置添加打印件,以便您可以看到它掛起的位置。
  3. 每幀不要調用IMG_Load。在啓動時加載你的圖像。
  4. SDL_Surfaces裝有IMG_Load必須在不再需要時用SDL_FreeSurface釋放。特別是如果你加載了很多。
  5. 您不必每次更改內容時都請致電SDL_Flip。只要確保在每幀結束時調用它。
0
  1. 您可以將您的雪碧圖像添加到地圖一樣textureMap(stringID, pTexture)之前進入播放狀態: SDL_Surface* pTempSurface = IMG_Load(fileName.c_str()); SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface); SDL_FreeSurface(pTempSurface); m_textureMap[id] = pTexture;
  2. 當繪製英雄,你可以使用的StringID獲得質地: SDL_RenderCopyEx(pRenderer, m_textureMap[stringID], &srcRect, &destRect, 0, 0, flip);
  3. 對於你不會發布你的主循環,你可能應該在主循環中捕獲輸入,例如: SDL_Event event; if (SDL_PollEvent(&event)) { switch (event.type) {....} } 確保你先輸入事件,然後檢查每幀的繪製位置。