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);
bullet.draw(canvas);

}

UPDATE GAME method

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;

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

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...

Thanks,

Niksa

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

2 Answers 2

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

I have the same problem. I didn't follow the steps in the Lunar Lander, I came a cross that too late. How did you take the angle of the firing in to account? I mean with:

x_bullet -= BULLET_SPEED * elapsed;
y_bullet -= BULLET_SPEED * elapsed;

The firing does not look realistic, or am I wrong?

Thank you

share|improve this answer

Your Answer

 
discard

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