Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have to make a tank that sits still but moves his turret and shoots missiles.

As this is my first Android application ever and I haven't done any game development either, I've come across a few problems...

Now, I did the tank and the moving turret once I read the Android tutorial for the LunarLander sample code. So this code is based on the LunarLander code.

But I'm having trouble doing the missile firing then SPACE button is being pressed.

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);



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 *

 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;

//doesn't work, has to be updated every pixel or what?


boolean doKeyDown(int keyCode, KeyEvent msg) {
            boolean handled = false;
            synchronized (mSurfaceHolder) {
                if (keyCode == KeyEvent.KEYCODE_SPACE){
                    dSpace = true;
                    handled = true;
                if (keyCode == KeyEvent.KEYCODE_DPAD_UP){
                    dUp = true;
                    handled = true;
                if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN){
                    dDown = true;
                    handled = true;
                return handled;

} a method run that runs the game...

public void run() {
            while (mRun) {
                Canvas c = null;
                try {
                    c = mSurfaceHolder.lockCanvas(null);
                    synchronized (mSurfaceHolder) {
                        if (mMode == STATE_RUNNING) 
                } catch (InterruptedException e) {
     // TODO Auto-generated catch block
    } 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) {

So the question would be, how do I make that the bullet is fired on a single SPACE key press from the turret to the end of the screen?

Could you help me out here, I seem to be in the dark here...



share|improve this question
Where are you setting the value of dSpace? –  CiscoIPPhone Jan 19 '11 at 13:12
Is the problem that the turret keeps firing when space is held? –  CiscoIPPhone Jan 19 '11 at 13:13
I've edited the thread: dSpace is being handled in the doKeyDown method... The problem is that the bullet should have a constant motion from the turret to the edge of the screen. I doesn't have it, that's my problem, I don't know how to do the physics in the updateGame method when dSpace is true.... –  Niksa Jan 19 '11 at 13:30

1 Answer 1

up vote 4 down vote accepted

All the movements in the lunar lander game are multiplied by the elapsed time since the last frame. This comes from the formula velocity * time = distance. Because of this the motion will be constant no matter what the frame rate is.

Instead of

x_bullet -= 1; y_bullet -= 1;

You should have something like:

x_bullet -= BULLET_SPEED * elapsed;
y_bullet -= BULLET_SPEED * elapsed;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.