473,320 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

a problem with the mouse in a video game

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
1 1506
blazedaces
284 100+
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

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

Similar topics

0
by: Henry C. Wu | last post by:
Hi, I have a form that has a video "inserted" at the form's Handle. Like so: '//Create Capture Window capGetDriverDescriptionA(0, lpszName, 100, lpszVer, 100) '// Retrieves driver info lwndC =...
18
by: DP | last post by:
hi, i'm designing a video and games rental database. i've got the customer table, with all the correct and relavant details. i've got a films, and games table. But i'm confused which way to...
2
by: The alMIGHTY N | last post by:
I've been having some issues with recursive summing and have been unsuccessful at getting some of the solutions I've seen online to work. I'm hoping that perhaps somebody in the newsgroup will be...
0
by: avgasman | last post by:
Hi all, I am a very novice c# programmer trying to use the mouse event args to place a graphic on a directshow video window. As soon as the mouse enters the active video panel, I lose ability to...
0
by: Harshpandya | last post by:
Hi, I want to show the live video on the network using flash video and quicktime. Now since its a live video i want to put mouse over it and able to click on screen and change the camera view or...
0
by: hpandya | last post by:
Hi, I want to show the live video on the network using flash video and quicktime. Now since its a live video i want to put mouse over it and able to click on screen and change the camera view or...
1
by: BrooklynBees | last post by:
I'm making a very very short amateur video game for fun. A friend recommended I use C#. I'm a total noob to programming, so I need *a lot* of help. Coding would involve clicking on a portion of...
1
Dököll
by: Dököll | last post by:
Greetings, and salutations! Hope you can help me... I started to finally begin a process in powerpoint which I think should help my son with his ABCs and 123s. I created some slides, most of...
0
by: kronus | last post by:
Hi everyone, Yes, I need to use you guys as a sounding board once more :-) I have a button that has two listeners one for mouse up and the other for mouse down and they point to the same...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.