java移动代码大全,Android在屏幕上移动图片代码,,Speed.javapa

Android在屏幕上移动图片代码,,Speed.javapa

Speed.javapackage cn.outofmemory.model.components;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; }}

[Java]代码public void run() { Canvas canvas; Log.d(TAG, "Starting game loop"); while (running) { canvas = null; // try locking the canvas for exclusive pixel editing // in the surface try { canvas = this.surfaceHolder.lockCanvas(); synchronized (surfaceHolder) { // update game state this.gamePanel.update(); // render state to the screen // draws the canvas on the panel this.gamePanel.render(canvas); } } finally { // in case of an exception the surface is not left in // an inconsistent state if (canvas != null) { surfaceHolder.unlockCanvasAndPost(canvas); } } // end finally }}public void update() { // check collision with right wall if heading right if (droid.getSpeed().getxDirection() == Speed.DIRECTION_RIGHT && droid.getX() + droid.getBitmap().getWidth() / 2 >= getWidth()) { droid.getSpeed().toggleXDirection(); } // check collision with left wall if heading left if (droid.getSpeed().getxDirection() == Speed.DIRECTION_LEFT && droid.getX() - droid.getBitmap().getWidth() / 2 <= 0) { droid.getSpeed().toggleXDirection(); } // check collision with bottom wall if heading down if (droid.getSpeed().getyDirection() == Speed.DIRECTION_DOWN && droid.getY() + droid.getBitmap().getHeight() / 2 >= getHeight()) { droid.getSpeed().toggleYDirection(); } // check collision with top wall if heading up if (droid.getSpeed().getyDirection() == Speed.DIRECTION_UP && droid.getY() - droid.getBitmap().getHeight() / 2 <= 0) { droid.getSpeed().toggleYDirection(); } // Update the lone droid droid.update();}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值