0

I have a bit of a problems with the mousePressed and mouseDragged events. I am trying to create a Space Shooter game and I want the player to be able to shoot by pressing and moving around the mouse. The big problem I think I have it's with the mouseDragged event. To be more specific: when I press the mouse button mousePressed is called and it runs perfectly, then when I move the mouse around (still keeping the mouse pressed) mouseDragged gets in and it works fine also, but when I stop moving the mouse (note I still have it pressed) my Spaceship stops firing and I'm not sure why.

This is the code:

private void initShootingThread(final MouseEvent e) {
 new Thread() {
 public void run() {
 do {
 playerShoot(e);
 } while (buttonPressed);
 }
 }.start();
}
// // PLAYER SHOOTING EVENTS ////
public void mouseClicked(MouseEvent e) {
 playerShoot(e);
}
public void mousePressed(MouseEvent e) {
 buttonPressed = true;
 initShootingThread(e);
}
public void mouseDragged(MouseEvent e) {
 buttonPressed = false;
 playerShoot(e);
}
public void mouseReleased(MouseEvent e) {
 buttonPressed = false;
}

Thank you in advance!

KenD
5,3387 gold badges53 silver badges90 bronze badges
asked Oct 27, 2013 at 20:39

2 Answers 2

3

As long as you drag the mouse you manually invoke the playerShoot(e) method from the mouseDragged() method..

However, your mouseDragged() method sets your buttonPressed variable to false so as soon as you stop dragging your main loop stops.

So don't set the buttonPressed variable to false.

answered Oct 27, 2013 at 20:43
Sign up to request clarification or add additional context in comments.

3 Comments

The problem is if I don't put buttonPressed = false in mouseDragged, the bullets will go from the same location, regardless of the fact that I'm moving away the Spaceship. link. This is what I mean.
So fix the problem. Your playerShoot(e) code must be wrong. Maybe you need two versions. One for mousePressed and one for mouseDragged(). We don't have access to your code so you will need to do some basic debugging yourself. We pointed you in the right direction.
Ok, thanks for the reply, I will try to come up with a solution and if I'll manage to find it in a reasonable period of time, I'll post it. ;)
0

Finally found the answer! camickr you were right, my problem was in the playerShoot method. To be more specific that MouseEvent e from the method's parameters was taking care of the position of the ship and also of the bullet starting place and since it was final I don't think it updated itself correctly when the method was called. Now the position of the ship is updated "manually" accordingly to x and y variables.

Now my code is something like this and it works like a charm:

private void initShootingThread() {
 new Thread() {
 public void run() {
 do {
 playerShoot();
 } while (buttonPressed);
 }
 }.start();
}
// // PLAYER SHOOTING EVENTS ////
public void mouseClicked(MouseEvent e) {
 playerShoot(e);
 x = e.getX();
 y = e.getY();
}
public void mousePressed(MouseEvent e) {
 buttonPressed = true;
 initShootingThread();
 x = e.getX();
 y = e.getY();
}
public void mouseDragged(MouseEvent e) {
 playerShoot();
 x = e.getX();
 y = e.getY();
}
public void mouseReleased(MouseEvent e) {
 buttonPressed = false;
}
public void mouseMoved(MouseEvent e) {
 x = e.getX();
 y = e.getY();
}
answered Oct 28, 2013 at 21:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.