Android - AndEngine "No Longer Touched" method? - java

Hi I'm using AndEngine in my Android app. I'm wondering if there is a method that detects when an object (in this case an object of AnalogOnScreenControls) goes from being touched to untouched? I want to set a specific command that executes only when someone lets go of the "analog stick" entity. The controls also use float values to detect what position they're in, so it could also be a method for when the values go from some value other than zero to zero, since the variables are set to zero when the controls are idle. Thanks for any help in advance!

There is a flag in the TouchEvent that you can check.
Most times I do something like this:
#Override
public boolean onAreaTouched(final TouchEvent touchEvent, ITouchArea touchArea, float touchAreaLocalX, float touchAreaLocalY) {
switch(touchEvent.getAction()){
case TouchEvent.ACTION_MOVE:{
// do stuff when finger moves
return true; // don't forget to break, or return true directly if the event was handled
}
case TouchEvent.ACTION_DOWN:{
// do stuff, the first time the finger touches the display
return true;
}
case TouchEvent.ACTION_UP:{
// do stuff when the finger goes up again and ends the touch event (your case)
return true;
}
case TouchEvent.ACTION_CANCEL:{
// If the event is somehow canceled - e.g. the finger leaves the display
return true;
}
default:{
// none of the above
return false;
}
}
}
Something like that. If you need more information about the event than theses simple actions, get the MotionEvent with touchEvent.getMotionEvent() and check out the additional options there.
BTW: I prefer to use the return true statement directly instead of a break here, just to make sure that the touch event won't get used otherwise in the app. But you can change that of course.
hope this helps
Christoph

You could create a field (global variable) named isTouched and set it initially to false. Then inside onControlChange() you do this:
if(pValueX == 0 && pValueY == 0 && isTouched){ //means knob has recently been touched
isTouched = false; //set to false so that being idle does not come here
//do your thing here
}
else{
isTouched = true;
//do normal stuff here
}

Related

Switch statement , only using one case at a time

Im trying to translate a square 'p' 5 units left or right depending on what key has been pressed . The problem is that when 'right' or 'left' is pressed it will translate 5 units in that direction but when I press again I can't move and I have to press 'left' to move right again so I never get anywhere.
Does anyone know why this is?
Task PlyThread=new Task() {
#Override
protected Object call() throws Exception {
myScene.setOnKeyPressed(event -> {
switch (event.getCode()){
//case UP: p.setTranslateY(((p.getY())-5)) ; break;
case LEFT: p.setTranslateX(((p.getX())-5)) ; break;
case RIGHT: p.setTranslateX(((p.getX())+5)) ;break;
}
});
return null;
}
};new Thread(PlyThread).start();
Ok, I assume that "Rectangle" is javafx.scene.shape.Rectangle. In this case setTranslateX() only modifies transformation matrix and does not change the rectangle coordinate X. Value of property X remains unchanged and next call to setTranslateX(getX()+5) does exactly the same job as the one before.
You need to work either with translations or with coordinates, i.e. either use setTranslateX()/getTranslateX() or setX()/getX().
Both choices may have other consequences, beyond moving rectangle on the screen, but I have no experience with JavaFX, so unfortunately I cannot elaborate more.

LWJGL: detecting left/right clicks when getButtonCount returns 0

On the Dell Inspiron 15 3000, the touchpad doesn't have any physical left/right buttons. Instead, it is one giant touchpad that is pressure sensitive. I'm assuming it detects right/left clicks based off of hand position on the trackpad.
In my LWJGL application, I detect mouse button clicks with Mouse.isButtonDown(0). This works fine on computers with a mouse with physical buttons, but doesn't work on touchpads that lack physical buttons. Mouse.getButtonCount() returns 0.
Has anybody had any success in detecting if a mouse button is pressed, should the user be using a trackpad that doesn't have physical buttons?
I think, instead of using
org.lwjgl.input.Mouse
This class could be what you are searching for:
org.lwjgl.input.Controllers
http://legacy.lwjgl.org/javadoc/org/lwjgl/input/Controllers.html
Im not entirely sure though since I only have a mouse and no way to test this with a touchpad.
For those who find this in the future, I did find a fix:
You cannot, should there be no physical buttons, use the Mouse.isButtonDown() method. Instead, you are going to have to read the event buffer. To do so, I wrote my own helper class:
public class Mouse{
private static boolean button_left = false, button_right = false;
public static boolean isButtonDown(int button){
if(button == 0) return button_left;
if(button == 1) return button_right;
return false;
}
public static void update(){
while(org.lwjgl.input.Mouse.next()){
if(org.lwjgl.input.Mouse.getEventButton() == 0) button_left = org.lwjgl.input.Mouse.getEventButtonState();
if(org.lwjgl.input.Mouse.getEventButton() == 1) button_right = org.lwjgl.input.Mouse.getEventButtonState();
}
}
}
The update() method is called every tick, and as such I can get button states using Mouse.isButtonDown(button).

Android - How to distinguish first pointer and second pointer move in onTouchEvent

I want handle a gesture in my activity. To do this i have override the public boolean onTouchEvent(MotionEvent MEvent) method on my Activity. The content looks like this:
motionaction = MEvent.getAction();
if(motionaction == MotionEvent.ACTION_DOWN)
{
...
return true;
}
if(motionaction == MotionEvent.ACTION_UP)
{
...
return true;
}
if(motionaction == MotionEvent.ACTION_MOVE)
{
...
return true;
}
motionaction = MEvent.getActionMasked();
if(motionaction == MotionEvent.ACTION_POINTER_DOWN)
{
...
return true;
}
if(motionaction == MotionEvent.ACTION_POINTER_UP)
{
...
return true;
}
return true;
The gesture it's the follow:
-finger1 on the screen hold its position (virtually because there is always a little movement)
-finger2 move on the screen. This is the movement that i want to grab.
I can grab the 5 action but the problem it's that when two fingers are on the screen the ACTION_MOVE grabs the movement of both first and second finger. The method MEvent.getActionIndex() don't works for the ACTION_MOVE that is return always 0; The only thing that i can do it's to save the position of the finger1 and to discard the movement near to that point. The result it's not perfect indeed sometime the movement of the finger2 it's "tainted" by the little finger1 movement because though the finger holds it's position on the screen the listener feels each minimal movement.
How can i improve this ?
Referring to the answer of Quintin Balsdon:
I have a similar thing in my code. I save the position of the finger1 in the ACTION_DOWN case, then when the finger2 move i see if the Y coordinate of the move is over the saved finger1 Y coordinate. If so the movement it's referred to the finger2 otherwise is referred to finger1 and i discard that in two finger mode.
If i try to draw a circle on my view in one finger mode i have got something like this:
http://img208.imageshack.us/img208/8113/onefingercircle.jpg
If i try to draw a circle on my view in two finger mode i have got something like this:
http://img256.imageshack.us/img256/6778/twofingercircle.jpg
So in one finger mode it work perfectly but not in two finger mode.
I don't know if it's related to the phone multitouch handler or the touch screen. It can be only hardware related also or my misunderstanding of the API.
This is managed by you as the developer. You are going to have to create some boolean variable to set (to true) when the "DOWN" action takes place and then unset (false) when the "UP" or "MOVE" motionevent occurs. In the code below, I maintain the coordinates when the "DOWN" even happens and make adjustments while the user moves around.
switch (e.Action) //e is a MotionEvent type
{
case MotionEvent.ACTION_DOWN:
{
_prevx = e.getX();
_prevy = e.getY();
}
break;
case MotionEvent.ACTION_MOVE:
{
_xoffset += e.GetX() - _prevx;
_yoffset += e.GetY() - _prevy;
Invalidate();
_prevx = e.GetX();
_prevy = e.GetY();
}
break;
}
If you want to do multi-finger dragging you need to implement ScaleGestureDetector.OnScaleGestureListener (http://developer.android.com/reference/android/view/ScaleGestureDetector.OnScaleGestureListener.html)

Android - How to handle two finger touch

The documentation say this about that:
A gesture starts with a motion event with ACTION_DOWN that provides
the location of the first pointer down. As each additional pointer
that goes down or up, the framework will generate a motion event with
ACTION_POINTER_DOWN or ACTION_POINTER_UP accordingly.
So i have done the override of onTouchEvent function in my activity:
#Override
public boolean onTouchEvent(MotionEvent MEvent)
{
motionaction = MEvent.getAction();
if(motionaction == MotionEvent.ACTION_DOWN)
{
System.out.println("DEBUG MESSAGE POINTER1 " + MEvent.getActionIndex() );
}
if(motionaction == MotionEvent.ACTION_POINTER_DOWN)
{
System.out.println("DEBUG MESSAGE POINTER2 " + MEvent.getActionIndex() );
}
}
Unfortunately the second if is never entered. The activity contains 2 view with 2 OnTouchListener, i know that onTouchEvent is called only if the view of the activity don't consume the event so i tried to return false in the listener and in that way i can recognize only the first finger touch but this avoid the listener to receive the ACTION_UP event and don't allow me to recognize the second finger touch. I also tried to return true in the listener but after manually invoke the onTouchEvent function but this allow me to recognize only the first finger touch too.
What's wrong in my code ?
I believe your code is missing the masking operation like:
switch (motionaction & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
}
This code should be able to check for ACTION_POINTER_DOWN.
Good luck & tell us what happens.
Tommy Kwee

Slide finger into buttons android

How can I make it possible that when someone slides their finger into or over a button, the button acts as if it were getting pushed?
An example of what i'm looking for would be a keyboard app. Where you slide your fingers over all the keys to play sounds.
You can use an OnTouchListener like this:
boolean firstTime = true;
OnTouchListener testTouchListener = new OnTouchListener(){
public boolean onTouch(View v, MotionEvent me){
Rect r = new Rect();
secondButton.getDrawingRect(r);
if(r.contains((int)me.getX(),(int)me.getY())){
//Log.i(myTag, "Moved to button 2");
if(firstTime == true){
firstTime = false;
secondButton.performClick();
}
}
if(me.getAction() == MotionEvent.ACTION_UP){
//When we lift finger reset the firstTime flag
firstTime = true;
}
return false;
}
};
firstButton.setOnTouchListener(testTouchListener);
With this approach though you are going to get a flood of touch events because onTouch() gets called a lot with MotionEvent.ACTION_MOVE. So you'll have to keep a boolean that tells you if its the first time you've gotten onTouch() call. Then you can reset that boolean in onTouch() for MotionEvent.ACTION_UP so it works again the next time. However this could get kind of complicated if you are trying to do more than just 2 buttons. I think you'd have to keep a boolean for each of them seperately (or perhaps an array of booleans to hold them all). and you'd need an additional if(r.contains(x,y) statements for every button. This should get you started on the right path though.

Categories