This could be any number of problems. The first question is does he always have the ability to go off the screen or only sometimes. If sometimes, what is it you have to do to make him go off screen. You say your character is sometimes faster than he is supposed to be. What are you doing that makes him speed up? Is it temporary once he speeds up? Does he also sometimes go slower? Is it when you walk in a certain direction, etc. Also, we see where you set the velocity, but when do you actually move the character. Maybe movement is tied to your framerate and therefore he will move faster during faster framerates and slower during slower framerates.
If your player speeds up sometimes it is probably because you have key presses in the buffer and more than one key_held routine is being executed per frame when things get laggy.
"It's like naming him Asskicker Monstertrucktits O'Ninja" -Khaiy
@Quasi
1. He should never be offscreen
2. he is not actualyl speeding up, it just looks like that because once he gets to a certain x value scrolling kicks in and when it scrolls he appears to be moving at the correct speed.
3. direction doesnt matter
4. framerate is capped at 20 fps
I figured out why he was going fast. MAX_PLAYER_SPEED was set to 10 from a while ago and when i changed some other code i forgot to change MAX_PLAYER_SPEED down to 5.
//Collisions detection with sides of scene
if( player->xPos < 1 || player->xPos + PLAYER_WIDTH > SCREEN_WIDTH )
{
player->xPos -= player->xVel;
}
You need to actually physically set his position here.
if (player->xPos < 1)
player->xPos = 1;
else if (player->xPos +PLAYER_WIDTH > SCREEN_WIDTH)
player->xPos = SCREEN_WIDTH - PLAYER_WIDTH;
else
player->xPos += player->xVel;
//Collisions detection with sides of scene
if( player->xPos < 1 || player->xPos + PLAYER_WIDTH > SCREEN_WIDTH )
{
player->xPos -= player->xVel;
}
You need to actually physically set his position here.
if (player->xPos < 1)
player->xPos = 1;
else if (player->xPos +PLAYER_WIDTH > SCREEN_WIDTH)
player->xPos = SCREEN_WIDTH - PLAYER_WIDTH;
else
player->xPos += player->xVel;
That's wrong, you need to adjust your players location, then check if it's out of bounds. You need to do something like this (assuming position and velocity are integers):
int newXpos;