LibGDX: Action when touchscreen is pressed and action when touchscreen is released - java

I have a problem with my code at libGDX in Android Studio. I'm trying to have an object perform an action while I press the screen. If I let go, the previous action should be aborted and a second should begin. When I press again on the screen, the second action should stop again and start the first while I hold down.
I have unfortunately no idea how I should do this and the Internet there is unfortunately also no exact information about it.
This also means that I have no code, which I can show as an aid position.
I would be very happy if someone has a solution, or can help me. :)

I would use an input listener which has a boolean value called touching. Then in the touchDown event set the touching to true. Again in the input listener in the touchUp event set the touching to false.
public class MyController implements InputProcessor {
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
touching = false;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
touching = true;
}
//.. more methods etc
}
In your application create this MyController and set it as the inputListsner with:
controller = new MyController();
Gdx.input.setInputProcessor(controller);
Now you can detect when the user is touching and do what ever action you need with:
if(controller.touching){
//user is touching do touching action
}else{
// user isn't touching do the non touchin action
}

You can use this way:
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// PRESSED
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
// RELEASED
return true; // if you want to handle the touch event
}
return false;
}
});
Let me know if this helps..

You can use in this way inside render method :
if(Gdx.input.isTouched()){
//whether the screen is currently touched.
}else{
}
or
You can pass InputProcessor implemented class inside Gdx.input.setInputProcessor(..)
after then override touchDown(..) and touchUp(...) method of that interface,
choose flag or some other technique for your requirement.

Tapped:
if (Gdx.input.justTouched()) {
//do something; }
Untapped:
if (Gdx.input.getPressure()==0){
//do something-else; }

Related

how to do something at the end of a finger drag in libgdx/android?

so pretty much i made a game that if you miss the target on screen the game is over, it works on my computer because it is a single click in a location but doesn't register on android because i feel like my finger is dragging by accident. i want the game to be over at the end of the drag in libgdx and am not sure how to go about it.
#Override
public boolean touchDragged(int screenX, int screenY, int pointer)
{
game.state = (GameState.GAMEOVER);
return false;
}
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if(arg1.getAction() == MotionEvent.ACTION_DOWN) {
// This is what happens on the click down...
} else if(arg1.getAction() == MotionEvent.ACTION_UP) {
// this is what happens on the click release
return true;
}
return false;
}});
This would process the click if any of their motion is on the object and is based on release to vague first-engagement will be refined to further accuracy where they choose to remove their finger.
You just can't, because on android the touch drag method is called almost everytime, because your finger is still moving a little bit.. with the mouse you can keep it in the same place without problem..
But why you game should stop when the touch dragg is called?
To see if you miss your target on screen use a collision detection..

Android ACTION_UP does not get fired after ACTION_MOVE

I'm trying to create a view programmatically, and change its background according to its click state.
I know I can do this with xml drawables, but I want to learn to program it too.
But if I press on my view, then slide my finger, the view gets stuck in pressed state. (white = 0xaaffffff background)
My code is this :
Edit:
I solved the problem. Here is the working code :
mainContainer.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
if (action==MotionEvent.ACTION_DOWN)
{
v.setBackgroundColor(0xaaffffff);
return false;
}
if(action==MotionEvent.ACTION_MOVE)
{
}
if(action==MotionEvent.ACTION_CANCEL)
{
v.setBackgroundColor(0x00ffffff);
}
if (action==MotionEvent.ACTION_UP)
{
v.setBackgroundColor(0x00ffffff);
return true;
}
return false;
}
});
As you see, I tried returning true wherever I want onTouch() to be called again. But apparently there is something I do not understand here.
What am I doing wrong ?
Thanks for any help.
What about returning true in your ACTION_UP aswell?
if (action==MotionEvent.ACTION_UP)
{
v.setBackgroundColor(0x00ffffff);
return true;
}

make RadioGroup to check button on ACTION_DOWN

i managed to do that by adding "actionDownChecker" on every RadioButton.
private View.OnTouchListener actionDownChecker = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN){
mTabbar.check(view.getId());
}
return true;
}
};
public void add(int radioButtonResourceId, Class<? extends Fragment> contentClass) {
mContent.put(radioButtonResourceId, new TabInfo(radioButtonResourceId, contentClass));
//hack to check radio button on ACTION_DOWN, not UP!
mTabbar.findViewById(radioButtonResourceId).setOnTouchListener(actionDownChecker);
}
Is there another more elegant way to do that?
The only other way I would know how to do it, is to physically change the code in the Listener. I'm not the smartest guy when it comes to developing for android, but the default constructor sets the boxes attached to RadioButtons to check on release.
Unless someone else knows something I don't, you either have to modify the listener class, or modify the entry after the listener has been evoked (by adding your actionDownCheker). Personally if it was me, i would just copy and paste your actionDownChecker, that way your listener class isn't messed up for your next project.
I'm sure there are some applications for it, but I'm not sure why you would want to do that. If someone presses down on the wrong button, they can just move off the button before it is released to cancel the press.

Close activity when clicking anywhere

I want my activity (a normal activity with a digital clock at the center of it) to close when I tap it anywhere. Is there a sort of Activity.setOnClickListener or a way to do it?
Yes on an Activity you override the existing handlers, on controls you add touch listeners, and [activityinstance].finish() gracefully closes your app.
#Override
public boolean onTouchEvent(MotionEvent event) {
this.finish();
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// Write Code Goes Here means >> this.finish();
return false;
}
This will help you.
use onUserInteraction() for finishing your activity this method called Called whenever a key, touch, or trackball event is dispatched to the activity. as doc says

Pressing the green key in blackberry

I want to know when the user presses the green button in the device to initiate a phonecall from the app directly. What event is fired when the green key is pressed?
Thanks
You need to implement KeyListener
import net.rim.device.api.system.KeyListener;
override the function
public boolean keyDown(int keycode, int time)
and inside it catch the event of pressing buttons.
How do you check which button was pressed?
if (Keypad.KEY_SEND == Keypad.key(keycode)) {//your code}
find the API DOC here : http://www.blackberry.com/developers/docs/4.0.2api/net/rim/device/api/ui/Keypad.html
I found the answer to be something similar to this.
Override keyDown:
public boolean keyDown(int keycode, int time)
{
if (keycode == Keypad.SEND)
{
//handle your event
return true;
}
return super.keyDown(keycode, time);
}

Categories

Resources