5

我一直在研究来自http://obviam.net/index.php/a-very-basic-the-game-loop-for-android/的示例在此我想做一些更改。

速度.java

public class Speed {

    public static final int DIRECTION_RIGHT = 1;
    public static final int DIRECTION_LEFT  = -1;
    public static final int DIRECTION_UP    = -1;
    public static final int DIRECTION_DOWN  = 1;

    private float xv = 1;   // velocity value on the X axis
    private float yv = 1;   // velocity value on the Y axis

    private int xDirection = DIRECTION_RIGHT;
    private int yDirection = DIRECTION_DOWN;

    public Speed() {
        this.xv = 1;
        this.yv = 1;
    }

    public Speed(float xv, float yv) {
        this.xv = xv;
        this.yv = yv;
    }

    public float getXv() {
        return xv;
    }
    public void setXv(float xv) {
        this.xv = xv;
    }
    public float getYv() {
        return yv;
    }
    public void setYv(float yv) {
        this.yv = yv;
    }

    public int getxDirection() {
        return xDirection;
    }
    public void setxDirection(int xDirection) {
        this.xDirection = xDirection;
    }
    public int getyDirection() {
        return yDirection;
    }
    public void setyDirection(int yDirection) {
        this.yDirection = yDirection;
    }

    // changes the direction on the X axis
    public void toggleXDirection() {
        xDirection = xDirection * -1;
    }

    // changes the direction on the Y axis
    public void toggleYDirection() {
        yDirection = yDirection * -1;
    }

}

使用它,图像可以向各个方向移动。现在我只想从下到上限制这种运动。onclick 的功能是,我们可以单击并拖动图像到所需的位置。我想用让图像消失或转到另一个活动来替换它。请帮助我更改此代码。提前致谢。

4

1 回答 1

0

如果您使用过 SurfaceView,那么在onDraw(Canvas canvas)方法中,用于从下到上移动图像的代码是这样的。

canvas.drawBitmap(bitmap,xPoint,yPoint,paint);

其中位图是一个图像(您要移动的位图),xPoint 是 x 坐标,yPoint 是 y 坐标,paint 是一个 Paint,它也可以为空。

对于自下而上的移动,只需更新

yPoint = yPoint - 1;

onDraw()调用之前的任何线程中。

愿这对你有所帮助。

于 2013-01-07T06:37:50.730 回答