StateBased Java Game - How to tell if the mouse button is released? - java

i'm creating a statebased game using slick 2d, and lwjgl. I currently am using if statements to check whether the mouse is inside the area of the button, and another if statement to check whether the mouse button is down (the button is just a picture g.drawImage("buttonImage.png", x,y); ). So I have a button where it changes states to the main menu from the play state, but because the mouse button is down it clicks a button on the main menu state before the user is able to release the button. So I need to check if they released the button before the if statement on the main menu is triggered.

What you need is a boolean flag that is triggered when the Mouse is down, like this:
(psuedo code)
if(Mouse.isButtonDown()) {
downFlag = true;
}
Then after that:
if(!Mouse.isButtonDown() && downFlag)) {
changeState();
}
Please let me know if this helped :)

Related

How to override onBackPressed but also get the touch event?

I'm working on an Android project in Java and I'd like to get the MotionEvent when the user hits the back button (onBackPressed).
I know how to override the back button press, and i know how to get the touch event from touches in my activity, but I couldn't find a way to get the touch event details out of the back button press.
The goal is to write the user's touch event details (as if the user touches a regular view) to a file, even when the back button is pressed.
Is it possible? if so, how can I do it?

Android firebase favorite button changes

I have a list. I shoot data using firebase database. I have a favorite button image on my list. When I click the button, favorites are added to the favorite page. When I click the button, the favorite button image changes. But when I scroll up or down on the page or switch to another page, this changed image returns to its original state. What can I do.
The state of the button is not preserved when moving through activities, because the activity object is re-instantiated each time, so in order to fix it, you just make it a static variable.
private static boolean isCandyPressed = false;
Then in your onclick() of button, set it to true. And thus, handle your logic throughout by cheking this variable.
Hope this solves your problem

Sending value to activity through another

I am working on an update for my Rock Paper Scissors game (I know it's a simple concept and has been done before but still), and the update includes massive changes in layouts and the programming.
So far I have Three working Activities; MainMenu, SinglePlayer, SettingsActivity
The way that things work currently is the user will change a setting (Background, Vibration, etc) inside the SettingsActivity, and when the user hits the Main Menu button it will bundle all values (float backgroundNumber, boolean Vibration) and will send it over to the MainMenu activity. The MainMenu will then unpackage the values and save them, so when the user opens SinglePlayer or any other activity it can send the appropriate values over to that activity.
The issue comes in when the user wants to reset the SinglePlayer score counter...
Here's what works: User presses "Reset Score" inside Settings -> User presses Main Menu button -> User open Single Player (Score resets and everything works fine)
Here's what doesn't work: User presses "Reset Score" inside Settings -> User presses Main Menu button -> User opens a different activity other that Single Player/exits app -> User goes back to main menu -> User goes into Single Player (Score does not reset)
The Main Menu saves all values other than the resetScore boolean (So the score counter doesn't get fed 'resetScore = true;' every time SinglePlayer gets opened).
I'm not trying to advertise but if you are having problems following my description, try following the steps provided above live with the app: https://play.google.com/apps/testing/com.simplegames.chris.rockpaperscissors20
This may be considered a "hack", but this helped me.
After pressing the "Reset Score" button the SinglePlayer Activity is immediately opened to reset the score and then sent back to the Settings Activity. It happens so quickly that it doesn't even look like it left the Settings Activity.
I would remove the question, but who knows... someone else (or me in the future) might need it?

How can I check if a button is pressed from another class?

In my code, on one class, I'm trying to check if the button has been pressed on my other frame (class). If this is true, then some code will be carried out. However, I cannot find any code whatsoever to check if the button has been pressed or not.
Here is my code:
editBanana bFrame = new editBanana();
String getLocation = bFrame.editLocationField.getText();
javax.swing.JButton saveButt = bFrame.saveEditButton;
...
...
//If button is pressed from my other form, then do this code
if (saveButt.getModel().isPressed())
{
System.out.println("Saved");
}
When I run this code, and press the button, this code does not work and nothing happens.
I'm thinking because this isn't working, that it runs through this code first, and then I press the button. So for the code where I click my button, I simply call the main class again so that it iterates through the code with the button now pressed. But it still does not execute my code.
Which code should I be using in order to find whether or not my button has been pressed?
Thanks.

drag/slide finger to perform onclick buttons- multitouch

i have a game, consisting of 7 buttons , arranged such that 1 button is in the center and rest 6 around it.
click on the buttons results in change in textview. using click can really be cumbersome for the user, therefore i would like to add functionality of dragging finger over buttons to perform onclick ..something similar to boggle type of games like -> https://market.android.com/details?id=com.ant.wordfind.client&feature=search_result
i have tried to implement the same ,problem is when i drag of finger only one button gets registered , even if i press other buttons by dragging cursor -> other button touch are just ignored.given the
following code.
public boolean onTouch(View v, MotionEvent arg1)
{
if(arg1.getAction() ==MotionEvent.ACTION_MOVE)
{
Button b;
switch (v.getId())
{
case R.id.b1:
case R.id.b2:
case R.id.b3:
case R.id.b4:
case R.id.b5:
case R.id.b6:
case R.id.b7:
b = (Button)findViewById(v.getId());
b.performClick();
break;
}
return true;
}
am i missing somethng ? wht i want is whn buttons are touched ..they perform onclick functionality.once one button click is registered rest touches are ignored
any help would be appreciated .
i dont want something like piano/soundboard where there is continuous click of button ..but just one click as i drag finger over the button
thanks !
At onTouch(), you shouldn't always use return true. Return false if you want the subsequent actions to be captured.
onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.
Ref: http://developer.android.com/guide/topics/ui/ui-events.html

Categories