Original Post
Hello,
Been stuck on this for a few days. Would really appreciate a little help.
I'm trying to learn the basics of animating movement on a 2D Tileset. Characters can move in 8 directions on the board. If I click several tiles distant, the intermediate moves are not shown - the character just moves instantly to the end point
I used println to feed back information. It seems the thread is being created, but that it is not calling the paint method.
This method gets the tiles the character must pass through to get to his destination, moves the character, starts a thread to call repaint, then sleeps to allow the animation (which doesn't happen) to occur.
[source lang="java"]public void moveActor(int destinationIn)
{
LinkedList actorMoves = calculateMoves(destinationIn); // get the int values representing the co-ordinates the actor must move through to get to destination
// Iterates through the actorMoves
for(int i = 0 ; i < actorMoves.size() ; i++)
{
System.out.println("MoveActor: Move " + i + ": " + actorMoves.get(i));
// Resets the actor Id of the current position to -1 to denote empty
int currentPos = actorList.get(selectedActorId).getActorPosition();
board.setActorIdInTile(currentPos, -1);
// Sets the actor position in the actor object and on board
actorList.get(selectedActorId).setActorPosition(actorMoves.get(i));
board.setActorIdInTile(actorMoves.get(i), selectedActorId);
// Starts a thread to update the board
new Thread()
{
public void run()
{
System.out.println("Paint thread started");
repaint();
}
}.start();
// Puts main thread to sleep
try
{
System.out.println("Main thread sleeping");
Thread.sleep(500);
}
catch (InterruptedException ex)
{
Logger.getLogger(ImageLoader6.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Main thread active");
}
}[/source]
This is the paint method. First it draws the tiles, then the characters on top.
[source lang="java"]// Paints the map
public void paint(Graphics g)
{
System.out.println("Paint Method started");
for(int i = 0 ; i < 100 ; i++)
{
String type = board.getTileType(i);
int x = (i % 10) * 64;
int y = (i /10) * 64;
// Checks the tile object to determine whether it is grass or sand
if(type.equalsIgnoreCase("grass"))
{
g.drawImage(grass, x, y, null);
}
else
{
g.drawImage(sand, x, y, null);
}
}
// Works through the list of actors, draws them
for(int j = 0 ; j < actorList.size() ; j++)
{
// Get Actor's position & convert to X,Y coordinates
int actorPosition = actorList.get(j).getActorPosition();
int actorX = (actorPosition % 10) * tileSize;
int actorY = (actorPosition / 10) * tileSize;
g.drawImage(actorList.get(j).getActorImage(), actorX, actorY, null);
}
try
{
Thread.sleep(20);
}
catch (InterruptedException ex)
{
Logger.getLogger(ImageLoader6.class.getName()).log(Level.SEVERE, null, ex);
}
}// End of paint()[/source]
Here's a sample of the output. It seems the paint method is not being called by the thread.
MoveActor: Move 0: 22
Main thread sleeping
Paint thread started
Main thread active
MoveActor: Move 1: 33
Main thread sleeping
Paint thread started
Main thread active
MoveActor: Move 2: 44
Main thread sleeping
Paint thread started
Main thread active
Paint Method started
If you need me to post the whole program, no problems. Let me know. Any help hugely appreciated!
Been stuck on this for a few days. Would really appreciate a little help.
I'm trying to learn the basics of animating movement on a 2D Tileset. Characters can move in 8 directions on the board. If I click several tiles distant, the intermediate moves are not shown - the character just moves instantly to the end point
I used println to feed back information. It seems the thread is being created, but that it is not calling the paint method.
This method gets the tiles the character must pass through to get to his destination, moves the character, starts a thread to call repaint, then sleeps to allow the animation (which doesn't happen) to occur.
[source lang="java"]public void moveActor(int destinationIn)
{
LinkedList
// Iterates through the actorMoves
for(int i = 0 ; i < actorMoves.size() ; i++)
{
System.out.println("MoveActor: Move " + i + ": " + actorMoves.get(i));
// Resets the actor Id of the current position to -1 to denote empty
int currentPos = actorList.get(selectedActorId).getActorPosition();
board.setActorIdInTile(currentPos, -1);
// Sets the actor position in the actor object and on board
actorList.get(selectedActorId).setActorPosition(actorMoves.get(i));
board.setActorIdInTile(actorMoves.get(i), selectedActorId);
// Starts a thread to update the board
new Thread()
{
public void run()
{
System.out.println("Paint thread started");
repaint();
}
}.start();
// Puts main thread to sleep
try
{
System.out.println("Main thread sleeping");
Thread.sleep(500);
}
catch (InterruptedException ex)
{
Logger.getLogger(ImageLoader6.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Main thread active");
}
}[/source]
This is the paint method. First it draws the tiles, then the characters on top.
[source lang="java"]// Paints the map
public void paint(Graphics g)
{
System.out.println("Paint Method started");
for(int i = 0 ; i < 100 ; i++)
{
String type = board.getTileType(i);
int x = (i % 10) * 64;
int y = (i /10) * 64;
// Checks the tile object to determine whether it is grass or sand
if(type.equalsIgnoreCase("grass"))
{
g.drawImage(grass, x, y, null);
}
else
{
g.drawImage(sand, x, y, null);
}
}
// Works through the list of actors, draws them
for(int j = 0 ; j < actorList.size() ; j++)
{
// Get Actor's position & convert to X,Y coordinates
int actorPosition = actorList.get(j).getActorPosition();
int actorX = (actorPosition % 10) * tileSize;
int actorY = (actorPosition / 10) * tileSize;
g.drawImage(actorList.get(j).getActorImage(), actorX, actorY, null);
}
try
{
Thread.sleep(20);
}
catch (InterruptedException ex)
{
Logger.getLogger(ImageLoader6.class.getName()).log(Level.SEVERE, null, ex);
}
}// End of paint()[/source]
Here's a sample of the output. It seems the paint method is not being called by the thread.
MoveActor: Move 0: 22
Main thread sleeping
Paint thread started
Main thread active
MoveActor: Move 1: 33
Main thread sleeping
Paint thread started
Main thread active
MoveActor: Move 2: 44
Main thread sleeping
Paint thread started
Main thread active
Paint Method started
If you need me to post the whole program, no problems. Let me know. Any help hugely appreciated!