Reputation:
I am making a game and I am using delta for game speed monitoring. But for some reason, if the player holds down the key too long, the creeper face will move WAY too fast! Could someone please help me?
package net.trenterprises;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import net.trenterprises.frame.MainFrame;
import net.trenterprises.frame.Window;
public class Main implements KeyListener {
static boolean wPressed = false;
public static boolean isWPressed() {
synchronized (Main.class) {
return wPressed;
}
}
static int creeperX = 0;
static int creeperY = 0;
static JLabel Creeper = null;
static long lastFrame;
public static long getTime() {
return (System.nanoTime() * 1000) / 1000000;
}
public static int getDelta() {
long time = getTime();
int delta = (int) (time - lastFrame);
lastFrame = time;
return delta;
}
static Main Main = new Main();
public static void main(String[] args) {
Window Frame = new MainFrame();
Frame.addKeyListener(Main);
Frame.setLayout(null);
Frame.setVisible(true);
Frame.setSize(1380, 750);
try {
Creeper = new JLabel(new ImageIcon(ImageIO.read(new URL("https://slm-assets0.secondlife.com/assets/8754271/lightbox/creeper_head2.jpg?1384657553"))));
Frame.add(Creeper);
Creeper.setSize(Creeper.getPreferredSize());
Creeper.setBounds(creeperX, creeperY, Creeper.getSize().width, Creeper.getSize().height);
} catch (Exception e) {
e.printStackTrace();
}
new CreeperUpdate().start();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyChar() == 'w') {
wPressed = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyChar() == 'w') {
wPressed = false;
}
}
}
class CreeperUpdate extends Thread implements Runnable {
public void run() {
while(true) {
if(Main.wPressed) {
if(Main.getDelta() > 0) {
System.out.println("Creeper X: " + Main.creeperX);
Main.creeperX += 1 * (Main.getDelta() / 50);
}
}
Main.Creeper.setBounds(Main.creeperX, Main.creeperY, Main.Creeper.getSize().width, Main.Creeper.getSize().height);
}
}
}
Upvotes: 0
Views: 233
Reputation: 1379
return (System.nanoTime() * 1000) / 1000000; Isn't this too high compared to your divisor 50. This is like a lot of pixels per second, i.e. 2e4.
Please, don't overuse constants, if not at all, use named variables instead.
public static final int PIXELS_PER_SECOND = 100
This rule always applies in software accompanied by their friends; assumptions. In your case this will make your game easier to tweak and maybe later several difficulty levels if you change multipliers/divisors into variables and such.
And please refactor time and event handling code into the CreeperUpdate class. Push the state to Thread
that requires it, concurrency is hard and it is best to avoid it if you can. Static methods are usually fine, in your case anyway, if they do not mess with the state.
Sorry if this is too much.
Upvotes: 1