By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,333 Members | 1,843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,333 IT Pros & Developers. It's quick & easy.

a problem with the mouse in a video game

P: 12
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(Position 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 (InterruptedException e)
{
e.printStackTrace();
}




}

}



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(Position 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.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.event.MouseInputAdapter;



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.setText("Press 'h' any time to see help screen");
healthBar.setValue(0);
// canvas1.setBackground(Color.LIGHT_GRAY);

draw(canvas1.getGraphics());
draw2(canvas1.getGraphics());

//

if(evt.getSource() == jButton1)
{
prompt.dispose();
nameField.setText(this.jTextField1.getText());
draw(canvas1.getGraphics());
draw2(canvas1.getGraphics());


gameFrame.setVisible(true);



}

if(textField.getLineCount() > 4)
{
textField.setText("");
}
}




public void keyTyped(KeyEvent e) { }


public void keyPressed(KeyEvent e)
{
manaField.setText(Integer.toString(e.getKeyCode()) );
gameFrame.validate();
switch(e.getKeyCode())
{
case 38:
if(playerPos.getYpos() > 0)
playerPos.setPosition(playerPos.getXpos(),playerPo s.getYpos() - 5);

break;
case 40:
if(playerPos.getYpos() < canvasSize.height -25)
playerPos.setPosition(playerPos.getXpos(),playerPo s.getYpos() + 5);
break;
case 37:
if(playerPos.getXpos() > 0)
playerPos.setPosition(playerPos.getXpos() - 5,playerPos.getYpos());
break;
case 39:
if(playerPos.getXpos() < canvasSize.width - 25)
playerPos.setPosition(playerPos.getXpos() + 5,playerPos.getYpos());
break;
case 49:
Thread myThread = new Thread(healChar);
myThread.start();
break;

case 50:
RandomNumbers generator = new RandomNumbers(10);


textField.setText(textField.getText() + Integer.toString(generator.getRandomNumber()));


break;

case 54:

helpScreen help = new helpScreen();
gameFrame.validate();



break;

default:

break;
};



gameFrame.validate();


draw(canvas1.getGraphics());
draw2(canvas1.getGraphics());

drawPotion(healthBTN2.getGraphics());




}



public void keyReleased(KeyEvent e) {}


public void mouseEntered (MouseEvent e) {}
public void mousePressed (MouseEvent e) {}
public void mouseReleased (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}


public void mouseClicked(MouseEvent e)
{


if (e.getX() > 10 && e.getX() < canvasSize.width - 12 && e.getY() > 10 && e.getY() < canvasSize.height - 12)
{ Position there = new Position((e.getX() - 10), (e.getY() - 10));

Position.gotohere(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.gotohere(playerPos, there); }
else if (e.getY() < 11 && e.getY() > -1)
{ Position there = new Position((e.getX() - 10), e.getY());

Position.gotohere(playerPos, there); }
else if (e.getX() < 11 && e.getX() > -1)
{ Position there = new Position(e.getX(), (e.getY() - 10));

Position.gotohere(playerPos, there); }

}

public void draw(Graphics g)
{

gameMap.getCurMap(g, canvasSize.width, canvasSize.height, canvas1, playerPos);
g.drawImage(Toolkit.getDefaultToolkit().getImage(" C:/game_maps/character.GIF"),playerPos.getXpos(), playerPos.getYpos(), 23, 23, canvas1);

}

public void draw2(Graphics g2)
{

g2.drawImage(Toolkit.getDefaultToolkit().getImage( "C:/game_maps/character2.GIF"),cpuPos.getXpos(), cpuPos.getYpos(), 23, 23, canvas1);


} }
Jun 5 '07 #1
Share this Question
Share on Google+
1 Reply


blazedaces
100+
P: 284
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
A couple of things:

1. Please from now on, please use [ code = java ][ /code ] brackets (without spaces of course). It is a heck of a lot easier to read that way, especially since you posted a lot of code.

2. You didn't need to post this much. It hurt you in two ways, one is according to the posting guidelines you're not supposed to post just about ALL your code. In reality though, even if it wasn't a rule, posting this much causes people to not want to read it, sift through it, and find your problem. You should have only posted your goToPosition (whatever it was called) method and your mouseClick method.

In your goToPosition method (whatever it is called) your process is in a while loop. No matter how many events stack up, that while loop doesn't get broken out of until whatever is in the while loop case isn't true.

Add something in your mouse click event and your goToPosition method that would interrupt that while loop somehow. Use a recursive method instead if need be.

Good luck and I hope that solved your problem,

-blazed
Jun 5 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.