Hello,
Probably this question is too easy to most of you, but I feel like stuck.
I’ve got everything ok with moving the image, but when I draw different images (from array), I see some flashing.
You can try the applet here:
http://asphaltgalaxy.com/test2/animtest.html
Press space to see that unwanted effect or press left or right to move.
Shortly about my animation loop:
I’ve got JPanel:
public class Board extends JPanel implements ActionListener, Runnable {
and there is I think pretty standard run method in it:
public void run () {
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
while (true) {
UpdateGame();
repaint();
timeDiff = System.currentTimeMillis() - beforeTime;
sleep = DELAY - timeDiff;
if (sleep < 0) sleep = 2;
try {
Thread.sleep(sleep);
}
catch (InterruptedException e) {
System.out.println("interrupted");
}
beforeTime = System.currentTimeMillis();
}
}
My paint method is like this:
public void paint(Graphics g) {
super.paint(g);
switch (gamestate) {
case RUNNING: {
DrawRunning (g);
}
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
And finally DrawRunning runs this method of Paddle class:
public void Draw (Graphics g, JPanel p) {
switch (state) {
case GET_NARROWER:
g.drawImage(narrower[narrowerframe], x, y, p);
break;
case GET_WIDER:
g.drawImage(wider[widerframe], x, y, p);
break;
default:
g.drawImage(paddle, x, y, p);
break;
}
}
where wider and narrower arrays of images:
Image[] wider = new Image[WIDER_FRAMES];
Image[] narrower = new Image[NARROWER_FRAMES];
And arrays are filled in Paddle class when you press space (I’ve got one image which I resize frame by frame):
public void Wider () {
if (state == State.NORMAL) {
state = State.GET_WIDER;
sizechange = WIDER_SIZE / WIDER_FRAMES;
widerframe = 0;
for (int i = 0; i < WIDER_FRAMES; i++) {
wider[i] = iid.getImage().getScaledInstance(w + sizechange * (i + 1), H, 0);
}
effectdelay = EFFECT_DELAY;
}
}
Any help needed, what and where I am doing wrong?
If I didnt provided some important code, please, let me know… Just didnt want to paste a lot of it to make main logic as clear as possible.