Setting a variable once
Mmm it's difficult to explain it so I will do my best...
I have this method:
private void manageActions(long delta) {
lastFrameChange += delta;
if(currentAction == States.Action.STANDING) {
lastFrameChange = 0;
resetDirections();
}
if(currentAction == States.Action.WALKING) {
switch(currentDirection) {
case UP:
if(lastFram开发者_StackOverfloweChange > 75.0f) {
lastFrameChange = 0;
if(++currentFrame > 2)
currentFrame = 1;
}
break;
case DOWN:
if(lastFrameChange > 75.0f) {
lastFrameChange = 0;
if(++currentFrame > 5)
currentFrame = 4;
}
break;
case LEFT:
if(lastFrameChange > 75.0f) {
lastFrameChange = 0;
if(++currentFrame > 7)
currentFrame = 6;
}
break;
case RIGHT:
if(lastFrameChange > 75.0f) {
lastFrameChange = 0;
if(++currentFrame > 9)
currentFrame = 8;
}
break;
}
}
}
It is a method to change the frames of a character based on his orientation and status (stand, walking ...) The problem is that when my character moves upward and then right, it pass through all the frames that between them. This is because the currentFrame variable is not resetted each time you change orientation with the right direction, and the last frame is keeped.
One more thing, resetDirections() set the currentFrame variable to the standing frames of the character.
I have been thinking when to reset this variable but I don't have any idea :/
You need to keep another variable, "previousDirection", that's set at the end of the method. When you enter the method, check if there's been a change of direction and set lastFrameChange and currentFrame accordingly.
精彩评论