In my "SpcaeShip" game i have a loop game that updates and drawing all of my objects. Now i want to add an array list of asteroids to the game and to draw them, but the problem is that if i drawing the array of asteroids in the loop game, the game im moving very slow. My solution was to draw the asteroids on a timer thread and the game speed is perfect. The only problem i have is that my astreoids are flickering all the time. I need to use a bufferStrategy or something similier?
This is my draw method in the game loop (in this loop i call to the game state draw method):
private void tick()
{
//update all objects
}
private void render()
{
//how many buffers(hold the same data as our screen) the canvas would use
bs = display.getCanvas().getBufferStrategy();
if(bs == null)
{
display.getCanvas().createBufferStrategy(3);
return;
}
g = bs.getDrawGraphics();
g.clearRect(0, 0, width, height);
if(State.getState() != null)
{
State.getState().render(g);
}
bs.show();
g.dispose();
}
public void run()
{
init();
int fps = 60;
double timePerTick = 1000000000/ fps;
double delta = 0;
long now;
long lastTime =System.nanoTime();
long timer = 0;
//game loop
while(running)
{
now = System.nanoTime();
delta += (now - lastTime) / timePerTick;
timer += now - lastTime;
lastTime = now;
if(delta >= 1)
{
tick();
render();
}
}
stop();
}
This is my game state methods:
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.Timer;
public class GameState extends State
{
private SpaceShip spaceShip;
private BgHandler bgHandler;
private ArrayList<Meteor> meteors;
public GameState(Handler handler)
{
super(handler);
bgHandler = new BgHandler(handler.getWidth(), handler.getHeight());
spaceShip = new SpaceShip(handler, handler.getWidth() / 2 - Things.DEFAULT_OBJECT_WIDTH / 2,
handler.getHeight() - Things.DEFAULT_OBJECT_HEIGHT - 100);
meteors = new ArrayList<Meteor>();
for(int i = 0; i < 5; i++)
{
Meteor m = new Meteor(handler,(int) (Math.random() * (handler.getWidth() - Things.DEFAULT_OBJECT_WIDTH ))
, -Things.DEFAULT_OBJECT_WIDTH,
Things.DEFAULT_OBJECT_WIDTH,
Things.DEFAULT_OBJECT_HEIGHT);
meteors.add(m);
}
timer.start();
}
Timer timer = new Timer (50, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
tickMeteor();
renderMeteor();
}
});
public void tickMeteor()
{
for(int i = 0; i < meteors.size(); i++)
{
Meteor m = meteors.get(i);
if(m.getVisible())
{
m.tick();
}
else
{
meteors.remove(i);
}
}
}
public void renderMeteor()
{
for(Meteor m: meteors)
{
Meteor m1 = (Meteor) m;
if(m1.getVisible())
{
m1.render(handler.getGame().getGraphics());
}
}
}
@Override
public void tick()
{
spaceShip.tick();
for(int i = 0; i < meteors.size(); i++)
{
Meteor m = meteors.get(i);
if(m.getVisible())
{
m.tick();
}
else
{
meteors.remove(i);
}
}
}
}
@Override
public void render(Graphics g)
{
bgHandler.render(g);
spaceShip.render(g);
}
}