473,664 Members | 3,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a problem with the mouse in a video game

12 New Member
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);


} }
Jun 5 '07 #1
1 1526
blazedaces
284 Contributor
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
1359
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 = capCreateCaptureWindowA(lpszName, WS_VISIBLE Or WS_CHILD, 0, 0, DefaultWidth, DefaultHeight, Me.Handle.ToInt32, 0) Now, when I try placing Debug.WriteLine(e.X & "," & e.Y) at the form's
18
1591
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 link them. e.g.
2
1625
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 able to shed some light on the issue. I have the following XML: <departmentStore> <name>S-Mart</name> <department>
0
1270
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 get mouse data. Can anyone help me retain mouse data within an active video window. Thanks
0
1396
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 do some other functions like zoon in and zoom out. we already have camera working fine on network but quality is not very good we think if we use flash and quicktime we will get better quality but then the problem is how we are going to put the...
0
1238
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 do some other functions like zoon in and zoom out. we already have camera working fine on network but quality is not very good we think if we use flash and quicktime we will get better quality but then the problem is how we are going to put the...
1
1224
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 the screen and being redirected to a new screen. For example, clicking on uhh a sofa would bring you to a closeup of the sofa. It would be nice if I could incorporate background music and maybe some other audio files of characters speaking, but I...
1
1616
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 which will have buttons. In testing one button, (before running wild and consider job well done)... I decided to test it lo and behold, slides are mute, no movement. I tried all available options, it looks like the slides are not reacting to...
0
5000
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 function. $btnRewind.addEventListener(MouseEvent.MOUSE_DOWN,onBtnBack); $btnRewind.addEventListener(MouseEvent.MOUSE_UP,onBtnBack);
0
8861
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2764
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1759
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.