Hi all, I am brand new to programming games in Java and this is my very first attempt. This game that I am currently working on follows a really nice tutorial that I found to help get me started. I have however taken what is in the tutorial and expanded on it somewhat. The problem I am having is that the movement of my craft sprite is very clunky and I am not sure how to fix it. I am also not sure that my method of continuous fire is a very good idea. My method of movement is a combination of checking for a key press and a boolean to try and eliminate the clunkyness but it doesn’t work very well. My method of firing is to create a new Thread that fires every few seconds depending on what FIRE_SPEED is set to, until canFire is false. canFire is true while the space bar is held down and, false when the space bar is released.
Craft Movement:
public void move()
{
x += dx;
y += dy;
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
long currTime = System.currentTimeMillis();
if(key == KeyEvent.VK_SPACE && !canFire)
{
canFire = true;
new Fire().start();
}
if(key == KeyEvent.VK_LEFT || moveLeft)
{
moveLeft = true;
dx = -1;
}
if(key == KeyEvent.VK_RIGHT || moveRight)
{
moveRight = true;
dx = 1;
}
if(key == KeyEvent.VK_UP || moveUp)
{
moveUp = true;
dy = -1;
}
if(key == KeyEvent.VK_DOWN || moveDown)
{
moveDown = true;
dy = 1;
}
}
public void keyReleased(KeyEvent e)
{
int key = e.getKeyCode();
if(key == KeyEvent.VK_SPACE)
{
canFire = false;
}
if(key == KeyEvent.VK_LEFT)
{
moveLeft = false;
dx = 0;
}
if(key == KeyEvent.VK_RIGHT)
{
moveRight = false;
dx = 0;
}
if(key == KeyEvent.VK_UP)
{
moveUp = false;
dy = 0;
}
if(key == KeyEvent.VK_DOWN)
{
moveDown = false;
dy = 0;
}
}
Firing:
public void fire()
{
missiles.add(new Missile(x + CRAFT_SIZE,y + CRAFT_SIZE / 2));
}
if(key == KeyEvent.VK_SPACE && !canFire)
{
canFire = true;
new Fire().start();
}
class Fire extends Thread
{
@Override
public void run()
{
while(canFire)
{
long curr = System.currentTimeMillis();
while(curr - lastShot < FIRE_SPEED)
curr = System.currentTimeMillis();
fire();
lastShot = System.currentTimeMillis();
}
}
}