i am creating an android game and have an onTouchEvent like this:
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (!action) {
action = true;
}
} else {
action=false;
}
return true;
}
My problen is now, that action is false as soon as i swipe my finger a little bit.
thanks for helping,
Florian
If you want to return true even when a swipe input occurs, you could store the last action to compare if the last action was ACTION_DOWN and if the current 'event.getAction()' is also ACTION_DOWN, then you can assume that the swipe action occurs (also if the previous 'x' and 'y' positions differ to the current 'x' and 'y' positions), therefore still return true. Any other scenario, you can return false.
You could also try looking in the android API documentation about a swipe input action/gesture.
Hope that is helpful.
Related
I am doing a beginner project and I have had a little issue with a radio button. This button's job is to change the theme of the window from light to dark and vice versa.
I'm not too sure on how to ask for Java to detect the value for the Color.decode() method. I want it to check if the current color is either "#21252B" or "#FFFFFF"
I expect it to look kind of like:
if(*however you are supposed to do it*.equals("#21252B")) {
frame.getContentPane().setBackground(Color.decode("#FFFFFF"));
darkMode.setBackground(Color.decode("#FFFFFF"));
} else {
frame.getContentPane().setBackground(Color.decode("#21252B"));
darkMode.setBackground(Color.decode("#21252B"));
}
What can I do?
I figured out what I had to do. Thanks to #AndrewThompson for the suggestion. If anyone needs an answer to a similar problem, here it is. Make
private boolean isDark = true //or false if you want from the get go.
Then, whenever you do your button do the following code
public void actionPerformed(ActionEvent arg0) {
if(isDark == true) {
lightTheme();
isDark = false;
} else {
darkTheme();
isDark = true;
}
after this you should be good to go.
I'm new in LibGdx, actually I'm creating a simple Libgdx game for Android devices.
I'm facing to a problem with my play/pause music in LibGdx here is the simplified code.
//...
#Override
public void render(float delta) {
//...
if(inputHelper.isTouched()){
input.x = inputHelper.getScreenX();
input.y = inputHelper.getScreenY();
camera.unproject(input);
if(soundRect.contains(input.x, input.y)){
if(parent.getSound()) parent.pauseBackMusic();
if(!parent.getSound()) parent.playBacMusic();
}
}
}
//...
public void pauseBackMusic(){
if(backMusic.isPlaying()){
backMusic.pause();
isSound = false;
}
}
public void playBackMusic(){
if(!backMusic.isPlaying()){
backMusic.play();
isSound = true;
}
}
public Boolean getSound() {
return isSound;
}
As you can see in the code when I touch the soundRect it's looping between pauseBackMusic() and playBackMusic().
Thank you.
Your Problem is that you use two if statements:
if(parent.getSound()) parent.pauseBackMusic();
if(!parent.getSound()) parent.playBacMusic();
So the first statement is true because isSound = true
Then you call method pauseBackMusic() in this method you set isSound = false
Then your next if statement tests: !parent.getSound() this is also true because: isSound = false and the method playBacMusic() set isSound = true
And it starts at the beginning by the next click.
Use else if statement instead:
if(parent.getSound()) parent.pauseBackMusic();
else if(!parent.getSound()) parent.playBacMusic();
So if parent.getSound() is true the else if statement will be skipped and isSound is still false. And by the next click only the else if statement will be true and the music will turn back on.
Or in this example you can simplify by only use else:
if(parent.getSound()) parent.pauseBackMusic();
else parent.playBacMusic();
You can use an InputProcesser in order to listen to touch events. Using this, you can listen to a touch event that is only called once when a user presses your button unlike running in a constant loop like in your example.
You can do this by using the touchDown Method
I have a fragment who has 2 faces like it was two separated activities, i want the home button transforms into back arrow when it is in the face two and open the menu when it is in the face 1; the toolbar is outside the fragment. and the code to display the menu is in the container.
public boolean onOptionsItemSelected(MenuItem item) {
// in the container activity "home"
//calls a menĂº i dont want to show it when i'm on the face 2
int id = item.getItemId();
if (face == 2 && id == android.R.id.home) {
show_face(false);//animation true = show face 2; false = show face 1
return true;
}else
return super.onOptionsItemSelected(item);
}
I returned true because of:
"boolean Return false to allow normal menu processing to proceed, true to consume it here."
but i think i misunderstood it.
PD: I want the code be in the fragment for managment purposes because i use too many fragments.
excuse my writing, I'm no good English.
So I have spent a long time looking to see if there is a solution for this. Basically, the below code allows me to press on a button, when I press (action_down) the 'pressed version' of the button is displaying good, then when I remove my finger the graphic returns to normal (action_up).
This part is working good BUT I need the ACTION_DOWN to stay active a little longer after the user presses the button and not return to ACTION_UP so fast. I can see lots of different people asking this question but no answers relating to editing the action_up or action_down part. I thought there must be some simple way to achieve this goal..
The current code:
#Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction()) {
int img = Integer.parseInt(v.getTag(R.id.tag_press_down_img).toString());
if (v instanceof ImageView) {
((ImageView) v).setImageResource(img);
}
} else if (MotionEvent.ACTION_UP == event.getAction()) {
int img = Integer.parseInt(v.getTag(R.id.tag_press_up_img).toString());
if (v instanceof ImageView) {
((ImageView) v).setImageResource(img);
}
}
return false;
}
(This code is working fine but the user experience is bad because the button reverts back to ACTION_UP too fast (immediately) after being released - I would like ACTION_DOWN to 1) display for 1-2 seconds then revert back to ACTION_UP OR 2) remain in action_down until the called event is complete - in this case loading another screen).
Thank you!
I am make a application on android that need multi-touch gesture! I found that my devices Novo 7 (ainol) supports 5 touch points. I implement onTouch for a View like this
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_DOWN || event.getAction() == MotionEvent.ACTION_DOWN){
Log.i("Touch", "Touch "+event.getPointerCount() + " -- Position " +
event.getRawX()+","+event.getRawY());
}
else if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_UP|| event.getAction() == MotionEvent.ACTION_UP){
Log.i("Touch up","Pointer = "+event.getPointerCount());
}
return true;
}
At first, everything is ok, the log is printed out perfectly with 5 message. But things happened when i touch and release the 6th point on screen. The log re-printed out 5 message. Can you tell me what is going on here. Sorry for my bad english and if this is a noob question! Thank you very much!
----------- EDITED--------------------
After logging for ACTION_POINTER_UP and ACTION_UP, i found out that when the 6th finger touch (touch down) the screen, all the current pointers (5 pointers) is release from the Event (e.g the action_up and action_pointer_up is call on 5 previous pointers)! Now, the question is, how the android process this situation when the limitation of touch point is reach.