Could any one explain what is consume() method and its purpose since I couldn't find much relevant information of it on the net? (example might be the best for a novice like me)
The consume method marks the event as processed. Typically this is executed from your program code when you've chosen to act upon the input, e.g. when you wish to prevent a text input from receiving the key stroke.
Or, to take the example given in the Javadocs:
[The consume method] allows listeners and component subclasses to "consume" the event so that the source will not process them in their default manner. For example, consuming mousePressed events on a Button component will prevent the Button from being activated.
Suppose you playing a Game FIFA for example your team is South Africa playing Russia .In normal circumstances if am marking a Russian player and l press key T in instance ,it will invoke a SlideTackle() ,sliding tackling function on the Russian player .
But what if l want to only make it possible that a sliding tackle can only occur whilst they is a opponent player near only that's when l will call consume() method so that the SlidingTackle() function doesn't respond to key press T when Russian opponent is not close .
That's how best l understood
Related
I wrote a simple little maze game for a terminal which repeatedly asks the user to do something (e.g. "In which direction would you like to go? [N/E/S/W]"). I have a navigate() method running in a loop that fires off these questions, stores their answers and does something depending on the answer.
public enum Dir (N, E, S, W);
public void navigate() {
Dir nextDir = utils.askDirection("Which way do you want to go?");
// Do stuff with answer, like changing position of user in maze
}
Now, I've written a simple GUI for my game. I deliberately put all the references to the terminal in a ConsoleUtils class which implements a Utils interface (this has methods like askQuestion()) - the idea being that I could create a GuiUtils class and have my game either as a terminal game or as a GUI game.
The problem is that the navigate method asks the user a question and then "waits" for the response, which the Utils class gives it by using a Scanner to read the newest line of input. However if I use Event Listeners for the new N/E/S/W buttons in my GUI, they fire off events regardless whether the navigate method has asked for one or not.
--> Image of GUI
Is there any way I can combine this or do I need to write a new navigate method for the GUI?
(To be honest, I'm also not entirely sure whether my GUI class should instantiate a game class, in which case the logic for navigate could end up in a GUI method anyway, or whether the game should have a GUI. I haven't written any code for the event listener either yet, since I'm not sure which class should be calling which. This is probably a separate question.)
Your text based game has a loop that repeatedly asks questions to gather user input. Swing provides this loop for you by continually executing Runnable blocks of code that have been posted to the EventQueue. For example, when the user presses a button labeled E, code is posted to the queue that invokes your ActionEvent implementation to handle your game's interpretation of the move east command.
For reference, a complete example of a very simple guessing game is examined here. In pseudocode, the corresponding text based game might look like this:
initialize
loop
prompt "Guess what color!"
get chosenColor
if chosenColor = actualColor
say "You win!"
reset game
else
say "Keep trying."
end loop
A more elaborate game cited there includes the original text-based source.
I'm currently programming a game in java and am using an interface to handle input/output for the game. I currently have a text interface working properly. I'm using code similar to the following:
while (moveExists())
{
String in = interface.getInput();
processInput(in);
interface.displayOutput(this.getState);
}
The text only interface works because it pauses to wait for input, but I am not sure how to accomplish a similar behaviour in a GUI implementation. How may I 'wait' for input from an actionListener?
If not, I'll probably use code less like a game loop and more like a finite state machine so that I don't need to deal with two different threads trying to co-ordinate their actions.
You could simply start a GUI (that it has its own separated thread) containing a text area and maybe a button or something like that, then you add an ActionListener to the text area or button and then you execute the code you need when the Listener is triggered (i.e. some code has been inserted or button clicked).
Another story if you have also another 'background' thread that needs to run in a loop...
I would like to make a program that allows the user to type in a message. The message will be typed in a JTextField. I would like to make it so that when every key is pressed it will play a short sound. I have the sound file as an mp3, its a short beep.
I only want it to work for keys A - Z, 0 - 9, backspace, and space.
I know how I would do this with the enter key; an action listener. I don't know how to do this with every key that is pressed before hitting enter.
A KeyListener in a JTextField will likely have problems with focus.
Instead, you can bind a key to an Action that plays a short Tone.
You need to add a KeyListener to your text field.
Here is some example documentation.
Here is a question from this site that will help in playing the mp3.
Good luck.
The best bet I would say is use a KeyListener. Implement that within your class, create the instance of whatever sound class you're going to use, create a method and within the keyPressed method, call your function, here is an example of how to do that. http://www.daniweb.com/software-development/java/threads/108969/linking-jbuttons-to-key-events-through-key-listeners
How would one implement KeyListener so that I can create a two-player system where one person uses '.' and '/' to control a character, and the other person can use the arrow keys without them interrupting each other? The way I have it now is that when one person holds down the arrow key, their character moves, but the instant you use the other player's controls, the first person's character stops.
Create a HashMap<Int,Boolean> that marks which keys are currently pressed/depressed.
Then in your game loop, you can move your objects depending on if the keys are depressed in this map.
For example:
if (keyMap.get(VK_COLON) == Boolean.TRUE) //True indicates pressed
playerAXPos+= 10;
From the sounds of things you're listening to a keyPressed event. Basically, you need to maintain stateful information about what keys are currently "down" and only stop the appropriate action when the keyReleased event occurs.
This will require to have two seperate lines action handler, one for when the key is pressed and one when the key is released.
One of the other things you might need to do is maintain some kind of cache of the active keys...which just got mentioned by Ethan as I was typing :P
I want to create a simple animation of a game so you can see the playout of a game. It should be an animation in which you just see the game Tic-Tac-Toe played. I have the states of the games in a description. So player 1 marks a cell = state 1; player 2 marks a cell = state 2 etc..
I currently have the game parsed in a ruby program; it will be easy to display just one state (like in the image), but how do I create an animation from it? Is there an easy way to do this? I'm open to solutions in every language but it shouldn't take to much time to implement. I want to show such an animation in a presentation.
<state1>
cell(1,1,x).
cell(1,2,o).
cell(1,3,o).
cell(2,1,o).
cell(2,2,x).
cell(2,3,x).
cell(3,1,b).
cell(3,2,b).
cell(3,3,x).
</state1>
<state2>
...
</state2>
Since you already know how to display one state, the way to animate that is to display each state one after the other in sequence.
Display the first state and set a timer, then when the timer goes off show the next state and set another delay, and so on through all the states.
Since you said you are open to all languages I don't really want to bother with any specific code beyond that high-level overview. For example, in Flash/ActionScript I would use TweenLite to display the symbols with a delay, but I don't know if that specific code would be of any use to you.
I've never programmed Ruby but it probably has a command like setDelay() or setTimer() or something. If not, the same effect can be accomplished with a main loop that will check the time each cycle and if the delay has been long enough it shows the next state.
For ruby you can have a awesome tic-tac-toe from https://github.com/grosser/tic_tac_toe
just do "gem install tic_tac_toe"