Changing MouseAdapter while mouse is being pressed - java

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?

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.

Related

MouseListener across several panels?

My user story is the following:
In order to upgrade a tower, the player has to drag a gun from the shop and drop it on the tower.
In practice, the GameView contains both the BattlefieldCanvas and the ShopView. Besides, the ShopView contains a GunSelector for each buyable gun. All those guys are sub-classes of JPanel.
I'm currently using a MousListener to handle several actions performed on the BattlefieldCanvas; I thought I could use the same stuff to handle a mouse trip from one of the GunSelector to the BattlefieldCanvas ( = across several panels ), so I tried to add the same MousListener to the gun selectors and the battlefield.
Problem: doesn't work. The getSource() method of the event object returns a reference to the gun selector while the mouse is actually released on the battlefield.
PS: Unlike gun selectors, towers are not swing components but images drawn by the paintComponent method.
1) To answer to your original question as to why source is still the component mouse was clicked on. You just need to read the JavaDoc:
public Object getSource()
The object on which the Event initially occurred.
Returns:
The object on which the Event initially occurred.
2) Now, how can we get the actual component that mouse is released on. You can try this approach:
#Override
public void mouseReleased(MouseEvent e) {
Component theCOmponentMouseIsReleasedOn = frame.findComponentAt( e.getLocationOnScreen() );
}
You don't need to always call findComponentAt on frame, you can call on the container that contains your BattlefieldCanvas.

Interrupt libgdx Action on collision

I am developing a game using libgdx. My Gameobjects are all subclasses of Actor. Now i want to take Advantage of the Actions which can be used with the Actors. If my Key-Controlled Player collides with a Computer-Controlled Enemie the Player should be knocked back. I can get this with a moveBy-Action. But when my Player collides with a Wall or another Zombie while he stil moves cause of knockback the Action should interrupt.
Is there a way to achieve this? Or are Actions not made for things like this?
Thanks
Of cause there is a way to stop actions.
One way would be that you remove the action from your actor with
actor.removeAction(Action action).
You can clear your actors actions with actor.clearActions(). This does delete all actions.
You can reset your Action or SequenceAction. sequence.reset() or action.reset()
stop updating the Actions at a collision by overriding the .act(float delta) of your actor
Else write a MyAction extends Action or MyAction extends
TemporalAction type which overrides the update (float percent)
and has a boolean for interrupted. If interrupted don't update if
not update.
i would recomend take a look into the actions to see how they work and maybe write your own Action that can be interupted. (your own action should be the best because it will be how you like it) Else remove the Action on collision.

MouseEvents in JFrame seem extremely unpredictable

I'm trying to code a level creator for my game in which you drag objects to their desired positions, but I'm having trouble figuring out whether or not the mouse button is being held down.
I'm using a MouseAdapter to listen for mouseClicked and mouseReleased events, but they seem to fire unpredictably. Usually, the program doesn't register either the mouse being clicked or released, but on occasion, one will be fired when it shouldn't be. A SOE will be thrown here and there, and eventually, they will be thrown repeatedly until the program is terminated. Any suggestions on working around this?
I think you're probably listening for the wrong events. MOUSE_CLICKED means MOUSE_PRESSED + MOUSE_RELEASED.
I think you probably want to be looking for MOUSE_PRESSED instead of clicked.
See the api for MouseEvent for more details: http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseEvent.html
Also, the MOUSE_DRAGGED event may be of some use to you.

Java 2D turn-based game programming: handle 2 mouse clicks per player

So suppose I'm developing a chess-like program using Java's Swing. I added a MouseListener to handle user input. To make a move the user must click a valid piece and then click a valid place. What's the best way to track the 2 mouse clicks in a turn? I'm thinking in use some kind of variable to record if is the turn's first click or second.
You have to distinguish the two game states, using a variable is fine.. you can also think something as suggested by NomeN comment and use two different listeners by swapping them.
Your case is quite simple but in general the formalism you use to handle these things is a sort of finite state machine that describes the states of your game and how to pass from one to another.
In this case you can have some states like:
player 1 turn
player 2 turn
main screen
pause screen
option screen
and you decide how and when to pass from a state to another, for example
after player1 moved you pass to player2 turn
after player2 moves you go back to player1 turn
when game starts you go in main screen
if you start a new game then you go to player1 turn
if you press pause key while in game you go from turn to pause screen and when closed you go back to the turn that was going before pause
This is just to give you an idea, so for example your MouseListener could care about states:
enum State { TURN_P1, TURN_P2, MAIN, PAUSE, ... }
public State gameState
...
public void mouseClicked(MouseEvent e)
{
if (gameState == TURN_P1)
{
...
if (move_is_legal and so on)
gameState = TURN_P2;
}
else if (gameState == TURN_P2)
{
...
if (move_is_legal and so on)
gameState = TURN_P1;
}
}
Oops, I answered too quickly. Yes, a structure that encodes a click location, looks for intervening motion events and then records the second click. There should be an initiated state, an accept state, and it should probably record a abort state (maybe a press of ESC).

Method sleep until event fired

I have a poker framework for which I am trying to develop a "player" for. Basically I am implementing an object that implements a Player interface defined by the framework. I am trying to put a GUI on top of this player, the way that game play works is that the Dealer invokes the act() method on my player and expects a return type of Action. The problem I have is that once the act() method is invokes, I update the GUI (written using Swing) to display the options available, however I now need the method NOT to return until the player has chosen an option. The options are displayed as JButtons, which when clicked are handled by an actionListener object. How can I make the act() method not return until the user has acted? I need the thread to sleep/wait until it is woken up by the event being triggered, am unsure of the syntax and best way to do this. Any ideas?
Thanks,
Aly
I think the approach is flawed. The Act method should not wait. Instead it should register for an event (lets call it the Acted event) on the Player instance. At the same time it should start a timer, of say 20 seconds, and if the Acted event is not raised before the timer runs out, the dealer should make the player fold (or check, depending on the situation) automatically and do the same for the next player in line.
That's just off the top of my head, but think about it.
If I undestood your ploblem, you need to use an ActionListener for this.
The ActionListener is an interface implemented in the class you want to be warned when an event occurs. When an specific event is triggered in other part of your code, this class is warned by an abstract method of the Action Listener interface.
It isn't easy to show you with a short answer, but I got an hello world example that can help you.
http://java.sun.com/docs/books/tutorial/uiswing/events/actionlistener.html
HIH

Categories