I'm writing a RPG with 2-D graphics in java and for right now i have it where you can move the character using the arrows keys and the mouse and a few other things, when i click the mouse somewhere my character goes there but while the character is going there and i click somewhere else the character still goes to where i first clicked then once the character is there, then it goes to the second location that i clicked, instead i want the character to go to the second location once i click there and ignore the first one, what could be the problem here?, here is some of my code.
note that the gui class is mostly here the other part of the class is code for displaying the buttons which is quite large in length
position class
package Game;
import java.math.*;
import java.awt.event. *;
public class Position {
public int x = 0;
public int y = 0;
static MouseEvent e;
public Position() {}
public Position(int xCoord, int yCoord)
{
x = xCoord;
y = yCoord;
}
public int getXpos()
{
return x;
}
public int getYpos()
{
return y;
}
public static void gotohere(Positi on p1, Position p2)
{
while (getDistance(p1 , p2) > 0.0)
{
if (p2.x > p1.x)
{p1.x = p1.x + 1;}
if (p1.x > p2.x)
{p1.x = p1.x - 1;}
if (p2.y > p1.y)
{p1.y = p1.y + 1;}
if (p1.y > p2.y)
{p1.y = p1.y - 1;}
try
{
Thread.sleep(20 );
}
catch (InterruptedExc eption e)
{
e.printStackTra ce();
}
}
}
public void setPosition(int xCoord, int yCoord)
{
x = xCoord;
y = yCoord;
}
public void move(int xDistance, int yDistance)
{
x = x + xDistance;
y = y + yDistance;
}
public static double getDistance(Pos ition p1, Position p2)
{
return Math.sqrt(Math. pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y , 2));
}
}
////////////////////////////////////////////////////
main gui class
import javax.swing.*;
import java.awt.*;
import java.awt.event. *;
import java.awt.Graphi cs;
import java.awt.Image;
import java.awt.Toolki t;
import javax.swing.eve nt.MouseInputAd apter;
public class Game_GUI implements ActionListener, KeyListener, MouseListener {
Map gameMap = new Map();
Position playerPos = new Position(0,0);
Position cpuPos = new Position(245, 185);
Integer xCoord = 0;
Integer yCoord = 0;
int progress = 0;
Dimension canvasSize = new Dimension();
public void actionPerformed (ActionEvent evt)
{
//textField.setTe xt("Press 'h' any time to see help screen");
healthBar.setVa lue(0);
// canvas1.setBack ground(Color.LI GHT_GRAY);
draw(canvas1.ge tGraphics());
draw2(canvas1.g etGraphics());
//
if(evt.getSourc e() == jButton1)
{
prompt.dispose( );
nameField.setTe xt(this.jTextFi eld1.getText()) ;
draw(canvas1.ge tGraphics());
draw2(canvas1.g etGraphics());
gameFrame.setVi sible(true);
}
if(textField.ge tLineCount() > 4)
{
textField.setTe xt("");
}
}
public void keyTyped(KeyEve nt e) { }
public void keyPressed(KeyE vent e)
{
manaField.setTe xt(Integer.toSt ring(e.getKeyCo de()));
gameFrame.valid ate();
switch(e.getKey Code())
{
case 38:
if(playerPos.ge tYpos() > 0)
playerPos.setPo sition(playerPo s.getXpos(),pla yerPos.getYpos( ) - 5);
break;
case 40:
if(playerPos.ge tYpos() < canvasSize.heig ht -25)
playerPos.setPo sition(playerPo s.getXpos(),pla yerPos.getYpos( ) + 5);
break;
case 37:
if(playerPos.ge tXpos() > 0)
playerPos.setPo sition(playerPo s.getXpos() - 5,playerPos.get Ypos());
break;
case 39:
if(playerPos.ge tXpos() < canvasSize.widt h - 25)
playerPos.setPo sition(playerPo s.getXpos() + 5,playerPos.get Ypos());
break;
case 49:
Thread myThread = new Thread(healChar );
myThread.start( );
break;
case 50:
RandomNumbers generator = new RandomNumbers(1 0);
textField.setTe xt(textField.ge tText() + Integer.toStrin g(generator.get RandomNumber()) );
break;
case 54:
helpScreen help = new helpScreen();
gameFrame.valid ate();
break;
default:
break;
};
gameFrame.valid ate();
draw(canvas1.ge tGraphics());
draw2(canvas1.g etGraphics());
drawPotion(heal thBTN2.getGraph ics());
}
public void keyReleased(Key Event e) {}
public void mouseEntered (MouseEvent e) {}
public void mousePressed (MouseEvent e) {}
public void mouseReleased (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}
public void mouseClicked(Mo useEvent e)
{
if (e.getX() > 10 && e.getX() < canvasSize.widt h - 12 && e.getY() > 10 && e.getY() < canvasSize.heig ht - 12)
{ Position there = new Position((e.get X() - 10), (e.getY() - 10));
Position.gotohe re(playerPos, there);
}
else if (e.getX() < 11 && e.getX() > -1 && e.getY() < 11 && e.getY() > -1)
{ Position there = new Position(e.getX (), e.getY());
Position.gotohe re(playerPos, there); }
else if (e.getY() < 11 && e.getY() > -1)
{ Position there = new Position((e.get X() - 10), e.getY());
Position.gotohe re(playerPos, there); }
else if (e.getX() < 11 && e.getX() > -1)
{ Position there = new Position(e.getX (), (e.getY() - 10));
Position.gotohe re(playerPos, there); }
}
public void draw(Graphics g)
{
gameMap.getCurM ap(g, canvasSize.widt h, canvasSize.heig ht, canvas1, playerPos);
g.drawImage(Too lkit.getDefault Toolkit().getIm age("C:/game_maps/character.GIF") ,playerPos.getX pos(), playerPos.getYp os(), 23, 23, canvas1);
}
public void draw2(Graphics g2)
{
g2.drawImage(To olkit.getDefaul tToolkit().getI mage("C:/game_maps/character2.GIF" ),cpuPos.getXpo s(), cpuPos.getYpos( ), 23, 23, canvas1);
} }