Well you declare speed in your method and do not set it to anything. So it is always going to the else setting the speed to 10.0f. Because in both your if and else you are setting it to 10, so what is the point of the if statement?
Then you rect.origin.x += speed;
or add 10 the origin all the time, no exceptions. It is the same as doing rect.origin.x += 10;
The image will constantly move.
The speed is always going to be 10, and always add to the rect.origin.x
do you want to make speed a instance variable rather than just in the method?
EDIT
Because speed is declared in the method each time it has 0 as a value. Then it is set to 10. If you create speed as a member variable, then each time animateCharRight
is called you can do something like
speed += speedIncrementAmount;
or
speed += 1.0f;
Meaning each time the speed is increased by 1 unit or what you need for your game.
Then the you would need a couple of checks depending on your logic.
if (speed > 10.0f)
{
// This will clamp the speed at 10.0f;
speed = 10.0f;
}
if (speed < - 10.0f)
{
// This will make sure you will never go left to quickly
speed = - 10.0f;
}
The above if statements may work or might not. Depends on the use. If the game is a side scooler and you only want to move to the right then you might want to make sure the speed is never less than 0.
Think about the design you want.
Since you are new to game programming I would start with a Tutorial and build upon the skills learned in the tutorial. Here is a link to one I found in a quick Google search.
Remember nobody makes Doom as there first game. Everybody starts with a Brick Breaker rip off or Tetris, it is the 'Hello World' of game programming.