开发者

SDL Moving my character

I've a problem with my source code. basically, I tried to move a sprite character in 2D map

here's my warrior class:

    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... 
//tim开发者_如何学Pythonpa 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);    
        }                  
}

The problem is I can't move it, and the program wasn't responding at all... I've checked all the sprite images and it works fine.


  1. Post your main loop.
  2. Add prints in various places in your code, so you can see where it hangs.
  3. Don't call IMG_Load every frame. Load your images at start-up.
  4. SDL_Surfaces loaded with IMG_Load have to be freed with SDL_FreeSurface when no longer needed. Especially if you are loading a lot of them.
  5. You don't have to call SDL_Flip every time you change something. Just make sure you call it at end of every frame.


  1. You can add your Sprite image to a map like textureMap(stringID, pTexture) before enter play state: SDL_Surface* pTempSurface = IMG_Load(fileName.c_str()); SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface); SDL_FreeSurface(pTempSurface); m_textureMap[id] = pTexture;
  2. When draw the hero, you can use the stringID to get the texture: SDL_RenderCopyEx(pRenderer, m_textureMap[stringID], &srcRect, &destRect, 0, 0, flip);
  3. For you do not post your main loop, you may should catch input in main loop like: SDL_Event event; if (SDL_PollEvent(&event)) { switch (event.type) {....} } Make sure you get the input event first and then check the draw position every frame.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜