我在C语言中使用SDL,我试图使一个字符在按下某个键时在屏幕上移动,并在释放该键时停止,但似乎在按住该键时触发了KEYUP事件。如果我在pressed()中删除KEYUP部分,字符将在屏幕上滑动,但显然不会自行停止。如果我将KEYUP部分保留在中,则必须反复按该键才能在屏幕上移动它们。
知道我做错了什么吗?
以下是我所拥有的:

...

int done = 0;
while (done==0)
{
    pressed();
    if (kenter == 1)
    {
        state = 1;
        subscreen();
    }
    if (kescape == 1)
    {
        done = 1;
    }
    if (kup == 1)
    {
        playery += -2;
    }
    if (kdown == 1)
    {
        playery += 2;
    }

    if (kright == 1)
    {
        playerx += 2;
    }
    if (kleft == 1)
    {
        playerx += - 2;
    }

...

int pressed ()
{

SDL_Event keyevent;

while (SDL_PollEvent(&keyevent))
{
    switch(keyevent.type)
    {
        case SDL_KEYDOWN:
        switch(keyevent.key.keysym.sym)
        {
            case SDLK_RETURN:
            {
                kenter = 1;
                break;
            }
            case SDLK_ESCAPE:
            {
                kescape = 1;
                break;
            }
            case SDLK_a:
            {
                ka = 1;
                break;
            }
            case SDLK_s:
            {
                ks = 1;
                break;
            }
            case SDLK_UP:
            {
                kup = 1;
                break;
            }
            case SDLK_DOWN:
            {
                kdown = 1;
                break;
            }
            case SDLK_RIGHT:
            {
                kright = 1;
                break;
            }
            case SDLK_LEFT:
            {
                kleft = 1;
                break;
            }
            default:
            break;
        }
    }
    switch(keyevent.type)
    {
        case SDL_KEYUP:
        switch(keyevent.key.keysym.sym)
        {
            case SDLK_RETURN:
            {
                kenter = 0;
                break;
            }
            case SDLK_ESCAPE:
            {
                kescape = 0;
                break;
            }
            case SDLK_a:
            {
                ka = 0;
                break;
            }
            case SDLK_s:
            {
                ks = 0;
                break;
            }
            case SDLK_UP:
            {
                kup = 0;
                break;
            }
            case SDLK_DOWN:
            {
                kdown = 0;
                break;
            }
            case SDLK_RIGHT:
            {
                kright = 0;
                break;
            }
            case SDLK_LEFT:
            {
                kleft = 0;
                break;
            }
            default:
            break;
        }
    }
}
return 0;
}

最佳答案

我想你的转换语句坏了。
用这种不那么混乱的方法

int pressed ()
{

    SDL_Event event;

    while(SDL_PollEvent(&event) )
    {
        if(event.type == SDLK_KEYDOWN)
        {
                switch(event.key.keysym.sym)
                {
                    case SDLK_RETURN:
                        doStuff = 1
                        break;
                    default:
                        break;
                }
        }
        if(event.type == SDLK_KEYUP)
        {
                switch(event.key.keysym.sym)
                {
                    case SDLK_RETURN:
                        doStuff = 0;
                        break;
                    default:
                        break;
                }
        }
    }
}

同样重要的是:
SDL Tutorials: Practical Keyboard Input
避免使用全局变量!

关于c - C SDL键盘事件SDL_KEYUP按下键时触发,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14102348/

10-10 19:34