Hey guys, so I am developing my multiplayer game but I found out that my playe sprite while’st moving tends to get stuck sometimes… not flickering but rather just CPU context switch is really long & obvious, is this makes any sense…
so I made just a rectangle which you can move with keys but it is not smooth moving, sometimes IT seems like vsync decides to drop few frames(it’s just assumption, I dont know what exactly is going on…)
I have tried many high precision timers and different ways, here is the code:
here is my main window, taking care of sleeping and moving.
public class Main {
public static void main(String[] args) {
System.out.println("2D sprite test.");
Window fx = new Window();
fx.g.createBufferStrategy(2);
fx.g.buffer = fx.g.getBufferStrategy();
long lastLoopTime = System.currentTimeMillis();
int offset = 250;
while(true)
{
long delta = System.currentTimeMillis() - lastLoopTime;
lastLoopTime = System.currentTimeMillis();
for(int i = 0; i < 4; i++){
if(fx.g.keystates[i]){
System.out.println("It is " + i);
switch(i){
case 0:
// down
fx.g.y += (delta * offset) / 1000;
break;
case 1:
// left
fx.g.x -= (delta * offset) / 1000;
break;
case 2:
// up
fx.g.y -= (delta * offset) / 1000;
break;
case 3:
// right
fx.g.x += (delta * offset) / 1000;
break;
}
}
}
fx.g.draw();
try{Thread.sleep(10);}catch(Exception e){}
}
}
}
MAIN.JAVA
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
public class GraphicsX extends Canvas implements KeyListener {
public BufferedImage image;
public BufferStrategy buffer;
public boolean keystates[] = new boolean[4];
public int x = 20, y = 20;
public GraphicsX()
{
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
image = gc.createCompatibleImage(400, 400);
setSize(500, 500);
}
//public void repaint() {}
public void draw()
{
Graphics g = buffer.getDrawGraphics();
g.setColor(Color.cyan);
g.fillRect(0, 0, 400, 400);
g.setColor(Color.red);
g.fillRect(x, y, 25, 25);
g.dispose();
buffer.show();
}
public void keyPressed(KeyEvent e)
{
keystates[e.getKeyCode() % 4] = true;
}
public void keyReleased(KeyEvent e)
{
keystates[e.getKeyCode() % 4] = false;
}
public void keyTyped(KeyEvent e) { }
}
GraphicsX.java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Window extends JFrame
{
public GraphicsX g;
public Timer fpstimer;
public Window()
{
super("lol");
g = new GraphicsX();
g.setIgnoreRepaint(true);
g.setBackground(Color.cyan);
g.setFocusable(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.BLACK);
setSize(600, 600);
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(g, BorderLayout.NORTH);
JButton hay = new JButton("LOL");
content.add(hay, BorderLayout.SOUTH);
g.addKeyListener(g);
setContentPane(content);
setVisible(true);
//
g.createBufferStrategy(2);
g.buffer = g.getBufferStrategy();
}
}
Window.java
Can anybody check out and see what could be possibly wrong or provide me code which renders really nicely and doesnt randomly ‘lag’…
I really suspect this might be the because of my Java version or maybe OS(Win7 64 bit) ???