目前,当我按住d-pad(sdl中的帽子)的方向时,我的代码只执行一次, 这是我的代码:
while (SDL_PollEvent( &event ))
{
switch (event.type)
{
case SDL_QUIT:
exitapp = 1;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
{
exitapp = 1;
// find other keys here: http://sdl.beuc.net/sdl.wiki/SDLKey
}
game->KeyDown( event.key.keysym.scancode );
break;
case SDL_KEYUP:
game->KeyUp( event.key.keysym.scancode );
break;
case SDL_MOUSEMOTION:
game->MouseMove( event.motion.x, event.motion.y );
break;
case SDL_MOUSEBUTTONUP:
game->MouseUp( event.button.button );
break;
case SDL_MOUSEBUTTONDOWN:
game->MouseDown( event.button.button );
break;
case SDL_JOYHATMOTION: /* Handle Hat Motion */
if ( event.jhat.value & SDL_HAT_UP )
{
game->HatUp();
}
if ( event.jhat.value & SDL_HAT_LEFT )
{
game->HatLeft();
}
if ( event.jhat.value & SDL_HAT_RIGHT )
{
game->HatRight();
}
if (event.jhat.value & SDL_HAT_DOWN )
{
game->HatDown();
}
break;
default:
// more info on events in SDL: http://sdl.beuc.net/sdl.wiki/SDL_Event
break;
}
}
理想情况下,当我按下d-pad时,它会继续调用game-> HatUp()。
我怎样才能让它发挥作用?
答案 0 :(得分:0)
在事件调查之间保存帽子状态。
这样的事情:
map< Uint8, bool > HatStatus;
while( running )
{
while (SDL_PollEvent( &event ))
{
switch (event.type)
{
...
case SDL_JOYHATMOTION: /* Handle Hat Motion */
HatStatus[ SDL_HAT_UP ] = ( true == event.jhat.value & SDL_HAT_UP );
HatStatus[ SDL_HAT_LEFT ] = ( true == event.jhat.value & SDL_HAT_LEFT );
HatStatus[ SDL_HAT_RIGHT ] = ( true == event.jhat.value & SDL_HAT_RIGHT );
HatStatus[ SDL_HAT_DOWN ] = ( true == event.jhat.value & SDL_HAT_DOWN );
break;
...
}
}
if( HatStatus[ SDL_HAT_UP ] ) game->HatUp();
if( HatStatus[ SDL_HAT_LEFT ] ) game->HatLeft();
if( HatStatus[ SDL_HAT_RIGHT ] ) game->HatRight();
if( HatStatus[ SDL_HAT_DOWN ] ) game->HatDown();
...
}