2

In a game that I am making I have two different mousePressed() methods, one for single fireing and one for auto fireing, If you get a automatic weapon it will change the mousePressed() methid from the one for single fireing to the one for automatic fire.

Later on when you lose the automatic weapon it will cahnge back to the mousePressed() for single fireing (I do this by having two MouseAdapters and the using addMouseListener and removeMouseListener).

The problem is that if you keep holding down the mouse as you lose your aoutomatic weapon you will still shoot automatically until you release the mouse and then press it again and the it will switch to the single fireing mousePressed().

How would I make it so that it will switch the MouseAdapter while the mouse is being pressed?

  • can you trigger some mouseUp() or mouseCancel() event? That way the current MouseAdapter might think the mousePressed() event has stopped – and releases the MouseAdapter so you can change it to the "single fire" MouseAdapter. – GameDroids May 07 '13 at 14:28
  • See ***[this](http://stackoverflow.com/questions/5601010/change-mouselistener-while-mouse-is-pressed)*** post – Extreme Coders May 07 '13 at 14:28
  • Instead of having two separate `ActionListeners` , make a single `ActionListener`. For the two types of weapons make two different classes. Now the `ActionListener` should call the correct class depending on the type of weapon. – Extreme Coders May 07 '13 at 14:41

1 Answers1

2

I suggest you implement two classes: NormalWeapon and AutomaticWeapon or something similar. These classes should implement a fire method that keeps firing until there is no more bullets. Your MouseAdapter will still send the fire command normally, but it won't fire anymore. It will "force" the player to release the button and it will also prevent that you don't keep firing when you don't have any bullets left.

Daniel Pereira
  • 2,720
  • 2
  • 28
  • 40