设置变量一次

发布于 2024-11-19 13:13:48 字数 1538 浏览 2 评论 0原文

嗯,很难解释,所以我会尽力...

我有这个方法:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-11-26 13:13:48

您需要保留另一个变量“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文