I'm probably doing this wrong, so please be nice. I'm developing a Java game, and I'm at the stage of testing character movement / animation.
The "person" can move up down left and right on a grid. The class the grid is drawn in is the gamePanel class. The buttons are in the gameControlPanel class.
I have a button which spawns a person on the grid. I then have a button to move the person up down left and right.
When the move up button is pressed, it calls the move up method from the person class. (At the moment, I'm only testing one "person" at a time.) In that method is the following code...
int move = 10;
while(move!=0)
{
setTopLeftPoint(new Point((int)getTopLeftPoint().getX(),
(int)getTopLeftPoint().getY() - 3));
try
{
Thread.sleep(300);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
move-=1;
}
The problem is that I can't seem to call the repaint method for the gamePanel class from within the Person class. To get around this, I created a timer in the gamePanel class which repaints every 20ms.
When I press the up button after the person is spawned, the button remains pressed down until the cycles of the while loop have been completed, and then the circle representation of the person is displayed in the grid square above.
I will try to answer any questions regarding this.