I'm making a game in which my player is a sprite which changes as i press the right key or up key ( to jump and run ) for that i wrote " GlutPostRedisplay " in pressKeySpecial and releaseKeySpecial. after that i have done some animations for which i have made a timer function (glutTimerFunc) that uses GlutPostRedisplay as well. now the problem is when i press the right key my player keep changing its sprite images for 'run' at a place. i know its because i have set GlutPostRedisplay in timer so it keep redisplaying the sprite but if i remove the GlutPostRedisplay from timer function then the rest of the animations stop and works only when the key is pressed.
i'm confused where to actually set the GlutPostRedisplay call so that all my animations and player works fine? i have tried glutIdleFunc as well but result stays the same.
void pressKeySpecial(int key,int x,int y)
{
switch (key)
{
case GLUT_KEY_LEFT:
break;
case GLUT_KEY_RIGHT:
m.changePosition(10.0,0);
m.changeMode(Mario::RUN);
break;
case GLUT_KEY_UP:
if(m.pos_Y<12)
{
m.changePosition(0,9);
m.changeMode(Mario::JUMP);
}
break;
}
glutPostRedisplay();
}
// TIMER
void Timer(int value)
{
t +=205;
t1 +=705;
down -=5;
angle +=5;
SplashTimer +=1;
glutTimerFunc(30, Timer, 1);
//glutIdleFunc(Timer);
glutPostRedisplay();
}
If there is no input you have to reset your mode. You need somthing like:
void pressKeySpecial(int key,int x,int y)
{
switch (key)
{
case GLUT_KEY_LEFT:
break;
...
default:
m.changeMode(Mario::STOP); // <- I used STOP because I don't know your default
break;
}
glutPostRedisplay();
}
or
void releaseKeySpecial( int key, int x, int y )
{
m.changeMode(Mario::STOP); // <- I used STOP because I don't know your default
}