SDL如何在按下键时为Sprite设置动画

时间:2014-04-25 11:01:00

标签: c++ animation sdl sprite

我要直截了当地说: 我有一个精灵表,有8个运动精灵(http://i.imgur.com/hLpo2Qn.png不能发布图像:\),所以它创造了我走路的幻觉) 我的问题是,当我按住一个键时,我希望精灵有点动画散步。例如,当我按向上箭头时,前两个精灵在循环播放,直到我松开键。到目前为止,我所能做到的只是每个方向只显示一个精灵,所以它有点不切实际。 代码:

#include <SDL/SDL.h>

int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface* screen, *image;
screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
bool running = true;
const int FPS = 30;
Uint32 start;
bool direction[4] = {0, 0, 0, 0};
SDL_Rect pos;
    pos.x = 0;
    pos.y = 0;
    SDL_Rect sprite;
    sprite.x = 160;
    sprite.y = 0;
    sprite.w = 32;
    sprite.h = 32;;
Uint32 color = SDL_MapRGB(screen -> format, 0xff, 0xff, 0xff);
image = SDL_DisplayFormat(SDL_LoadBMP("sprite.bmp"));
SDL_SetColorKey(image, SDL_SRCCOLORKEY, SDL_MapRGB(screen -> format, 255, 0, 255));
while (running == true)
{
    start = SDL_GetTicks();
    SDL_Event event;
    while(SDL_PollEvent(&event))
    {
        switch(event.type)
        {
        case SDL_QUIT:
            running = false;
            break;
        case SDL_KEYDOWN:
            switch(event.key.keysym.sym)
            {
            case SDLK_UP:
                direction[0] = 1;
                sprite.x = 0;
                break;
            case SDLK_LEFT:
                direction[1] = 1;
                sprite.x = 224;
                break;
            case SDLK_DOWN:
                direction[2] = 1;
                sprite.x = 160;
                break;
            case SDLK_RIGHT:
                direction[3] = 1;
                sprite.x = 96;
                break;
            }
            break;
        case SDL_KEYUP:
            switch(event.key.keysym.sym)
            {
            case SDLK_UP:
                direction[0] = 0;
                sprite.x = 32;
                break;
            case SDLK_LEFT:
                direction[1] = 0;
                break;
            case SDLK_DOWN:
                direction[2] = 0;
                break;
            case SDLK_RIGHT:
                direction[3] = 0;
                break;
            }
            break;
        }
    }
    if(direction[0])
        pos.y-- ;
    if(direction[1])
        pos.x--;
    if(direction[2])
        pos.y++;
    if(direction[3])
        pos.x++;

    SDL_FillRect(screen, &screen -> clip_rect, color);
    SDL_BlitSurface(image, &sprite, screen, &pos);
    SDL_Flip(screen);

        if(1000 / FPS > SDL_GetTicks() - start)
        SDL_Delay(1000 / FPS - (SDL_GetTicks() - start));

}
SDL_FreeSurface(image);
SDL_Quit();
return 0;

}

1 个答案:

答案 0 :(得分:1)

免责声明:这不是实现您想要的唯一或最佳方式。这只是一种可能的解决方案

您需要实现一个基本的计时器/计数器,以在两个适当的图像之间切换。

对于这个例子,我将精灵偏移存储在一个二维数组int sprite_x[4][2];中 - 这将代表四个方向和每个方向的两个图像偏移。然后在按键而不是使用bool数组时,只需要一个整数索引int sprite_index;来索引精灵偏移量。那么您需要做的就是使用另一个称为int current_key;的整数值,您可以在0和1之间切换。

// Used for indexing the 2D array
enum
{
    WALK_LEFT,
    WALK_RIGHT,
    WALK_UP,
    WALK_DOWN

    WALK_MAX
};

// How many sprites per animation
const int NUM_KEYFRAMES = 2;

// 2D array to hold the x value offsets for sprites
int sprite_x[WALK_MAX][NUM_KEYFRAMES];

int sprite_index = WALK_LEFT;  // Current animation
int current_key = 0;           // Current keyframe for the animation

// Set up all the x value offsets in the array
sprite_x[WALK_LEFT][0] = 0;
sprite_x[WALK_LEFT][1] = 32;
sprite_x[WALK_RIGHT][0] = 64;
sprite_x[WALK_RIGHT][1] = 96;
.
.
.

然后,您只需相应地更新精灵x值sprite.x = sprite_x[sprite_index][current_key];