移动位图不平滑且不稳定

时间:2012-03-14 16:01:50

标签: java android eclipse

我的持续更新更新方法。

@Override
public void update(float deltaTime) {
    Graphics g = game.getGraphics();
    List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
    game.getInput().getKeyEvents();

    redPin.moveX(deltaTime);

    int len = touchEvents.size();
    for (int i = 0; i < len; i++) {
        TouchEvent event = touchEvents.get(i);
        if (event.type == TouchEvent.TOUCH_UP) {
            if (inBounds(event, 0, 360, 800, 120)) {
                if (gameOver) {
                    // TODO game over methods
                } else {
                    // TODO game is not over methods
                    level++;
                    blackBacking.randomizeX();
                    blackBacking.setWidth(level + 1);
                }
                return;
            }
        }
    }
}

我的位图类

public class RedPin {
    private int xTracker = 400;
    private int x = 400;
    private int y = 85;
    private int height = 111;
    private long speed = 1000;
    private float deltaTime;
    private int direction = 1;

    protected int getX() {
        if (xTracker <= 86) {
            x = -10;
        } else if (xTracker >= 714) {
            x = -10;
        } else if (xTracker <= 713 && xTracker >= 87) {
            x = xTracker;
        }
        return this.x;
    }

    protected int getY() {
        if (this.xTracker == 86 || this.xTracker == 714) {
            this.y = 88;
        } else if (this.xTracker == 87 || this.xTracker == 713) {
            this.y = 87;
        } else if (this.xTracker == 88 || this.xTracker == 89 || this.xTracker == 712 || this.xTracker == 711) {
            this.y = 86;
        } else {
            this.y = 85;
        }
        return this.y;
    }

    protected int getHeight() {
        if (this.xTracker == 86 || this.xTracker == 714) {
            this.height = 104;
        } else if (this.xTracker == 87 || this.xTracker == 713) {
            this.height = 105;
        } else if (this.xTracker == 88 || this.xTracker == 712) {
            this.height = 106;
        } else if (this.xTracker == 89 || this.xTracker == 711) {
            this.height = 107;
        } else if (this.xTracker == 90 || this.xTracker == 710) {
            this.height = 109;
        } else if (this.xTracker == 91 || this.xTracker == 709) {
            this.height = 110;
        } else {
            this.height = 111;
        }
        return this.height;
    }

    protected void moveX() {
        if (this.xTracker >= 734) {
            direction = 1;
        } else if (this.xTracker <= 66) {
            direction = -1;
        }
        xTracker = (int) direction * ((xTracker + 1)*deltaTime);
        x = (int) direction * ((x + 1)*deltaTime);
    }

}

g.drawPixmap(Assets.gameRedPin,redPin.getX(),redPin.getY(),0,0,2,redPin.getHeight());

当我仅使用x + 1 * deltaTime运行代码时,会出现一点点急动。 当我将它设置为x + 2或任何更高时,抽搐运动甚至更大。有没有 我可以让运动更顺畅吗?

1 个答案:

答案 0 :(得分:0)

你能记录deltaTime的值吗?这可能是因为刷新率太低(即20 FPS),或者deltaTime值存在很大变化,两者都会导致急动。此外,如果您在模拟器上测试代码,它肯定会生涩。