我一直在研究http://obviam.net/index.php/a-very-basic-the-game-loop-for-android/上的例子.在这里我想做一些改变.
Speed.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;
}
}
Run Code Online (Sandbox Code Playgroud)
使用此功能,图像可以向各个方向移动.现在我只想限制这种从下到上的运动.onclick的功能是,我们可以单击并将图像拖动到所需位置.我想替换它只是让图像消失或去另一个活动.请帮我改变这段代码.提前致谢.
如果您使用过 SurfaceView 那么在onDraw(Canvas canvas)方法中用于从下到上移动图像的代码是这样的。
canvas.drawBitmap(bitmap,xPoint,yPoint,paint);
Run Code Online (Sandbox Code Playgroud)
其中 bitmap 是图像(要移动的位图),xPoint 是 x 坐标,yPoint 是 y 坐标,paint 是 Paint,也可以为 null。
对于从下到上的移动只需更新
yPoint = yPoint - 1;
Run Code Online (Sandbox Code Playgroud)
在调用之前的任何线程中onDraw()。
愿这对你有帮助。
| 归档时间: |
|
| 查看次数: |
1136 次 |
| 最近记录: |