设置变量一次
嗯,很难解释,所以我会尽力...
我有这个方法:
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(lastFrameChange > 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;
}
}
}
这是一种根据角色的方向和状态(站立、行走...)来改变角色框架的方法。问题是,当我的角色向上移动,然后向右移动,它穿过它们之间的所有框架。这是因为每次您以正确的方向更改方向时,currentFrame 变量都不会重置,并且会保留最后一帧。
另一件事是,resetDirections() 将 currentFrame 变量设置为角色的站立框架。
我一直在考虑何时重置此变量,但我不知道:/
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(lastFrameChange > 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 :/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要保留另一个变量“previousDirection”,该变量在方法末尾设置。
当您进入该方法时,检查方向是否发生变化并相应地设置lastFrameChange和currentFrame。
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.