0

So I'm making a sort of space game in Java. Currently, the spacecraft stays in place if the user isn't doing anything, and moves at a constant velocity in different directions based on the user's key input.

I'd like to change it so that when the spacecraft is moving, as the user continues to press the key, the spacecraft's velocity accelerates faster.

In my move method, I currently have

public void move() {
    x += dx;
    y += dy;
}

I've tried doing such things as

public void move() {
    x *= dx;
    y *= dy;
}

But constantly multiplying values makes the spacecraft move way too fast.

Is there another approach to doing this?

Thanks in advance.

Evan Haas
  • 2,524
  • 2
  • 22
  • 34
minoue10
  • 81
  • 1
  • 10
  • You might want to look here: http://stackoverflow.com/questions/667034/simple-physics-based-movement – tckmn Nov 29 '12 at 01:27

1 Answers1

1

Looks like you need to change dx according to a keyboard event of your choice. So:

  • Write a listener to capture keypresses
  • Test the key to see if it's the one you want (up arrow, down arrow, etc)
  • Increment/decrement dx (testing for min/max values) accordingly. You could do some fun physics formula here based on the current value of dx?

Then, when the spaceship moves, dx will be either large, or small, depending on how many times the user has pounded on the key.

Community
  • 1
  • 1
Ben
  • 54,723
  • 49
  • 178
  • 224