【发布时间】:2011-01-18 17:44:20
【问题描述】:
我必须制造一辆静止不动但移动炮塔并发射导弹的坦克。
由于这是我的第一个 Android 应用程序,而且我也没有做过任何游戏开发,所以我遇到了一些问题......
现在,当我阅读了 LunarLander 示例代码的 Android 教程后,我做了坦克和移动炮塔。所以这段代码是基于 LunarLander 代码的。
但是我在发射导弹时遇到问题,然后按下 SPACE 按钮。
private void doDraw(Canvas canvas) {
canvas.drawBitmap(backgroundImage, 0, 0, null);
// draws the tank
canvas.drawBitmap(tank, x_tank, y_tank, new Paint());
// draws and rotates the tank turret
canvas.rotate((float) mHeading, (float) x_turret + mTurretWidth, y_turret);
canvas.drawBitmap(turret, x_turret, y_turret, new Paint());
// draws the grenade that is a regular circle from ShapeDrawable class
bullet.setBounds(x_bullet, y_bullet, x_bullet + width, y_bullet + height);
bullet.draw(canvas);
}
更新游戏方法
private void updateGame() throws InterruptedException {
long now = System.currentTimeMillis();
if (mLastTime > now)
return;
double elapsed = (now - mLastTime) / 1000.0;
mLastTime = now;
// dUP and dDown, rotates the turret from 0 to 75 degrees.
if (dUp)
mHeading += 1 * (PHYS_SLEW_SEC * elapsed);
if (mHeading >= 75) mHeading = 75;
if (dDown)
mHeading += (-1) * (PHYS_SLEW_SEC * elapsed);
if (mHeading < 0) mHeading = 0;
if (dSpace){
// missile Logic, a straight trajectorie for now
x_bullet -= 1;
y_bullet -= 1;
}
}
运行游戏的方法 run...
public void run() {
while (mRun) {
Canvas c = null;
try {
c = mSurfaceHolder.lockCanvas(null);
synchronized (mSurfaceHolder) {
if (mMode == STATE_RUNNING)
updateGame();
doDraw(c);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
mSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
所以问题是,如何让子弹在从炮塔到屏幕末端的单个 SPACE 键下发射?
你能帮帮我吗,我似乎在黑暗中......
谢谢,
日照
【问题讨论】:
-
有什么问题?你的
(dSpace)触发器不工作吗?还是子弹不可见?还是轨迹错了? -
问题是 dSpace 正在工作,但我无法使用 update 方法让子弹从炮塔移动到屏幕边缘。
-
继续,
x_bullet和y_bullet会发生什么?以及这些变量最初有什么值,当子弹在屏幕边缘时它们应该有什么值? -
最初 x_bullet 和 y_bullet 与 x_turret & y_turret 相同...并且当击中屏幕边缘时,它们必须是 x_bullet = 0,y_bullet 取决于它垂直击中的角度炮塔。我知道距离 = 时间 * 速度,所以我做了 x_bullet -= BULLET_SPEED * elapsed ...这有点用,但动画效果不佳...