Libgdx: Touch up event acts unexpectedly - android

I'm writing a simple android game using Libgdx.
I had set boundary to my Actor like that:
setBounds(posX, posY, width, heigh);
I believe that all touch events will only occur in that zone. But touchUp event acts abnormally, when I touch inside actor's boundary, then drag out of that boundary, touchUp event still occurs.
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
//Touch up event still occur out of actor's boundary
}
Did I make anything wrong here?
Thanks in advance and sorry for my English :)

I assume you are talking about InputListener's touchUp method. According to docs this is called when a finger touch goes up anywhere! So no, you didn't do anything wrong: It is designed like this.
public void touchUp(InputEvent event,
float x,
float y,
int pointer,
int button)
Called when a mouse button or a finger touch goes up anywhere, but
only if touchDown previously returned true for the mouse button or
touch. The touchUp event is always handled.

Related

Android How to detect swipe between two touch points on image view

I have an Imageview with different number of touch points on them. It basically an app which is detecting the swipe between 2 touch points and not allowing the user to swipe any other point or in or out of other direction. It should constrict user to just swipe between two touch points.
Just take a look at following picture:
Now the user should start swiping from point 1 to point 2. if the swipe is not started from starting point 1, it should not color the path between point 1 and point 2.
But if the user successfully swipe between the point 1 and point 2 now swipe between point 2 to 3 should be enabled. Thus user should go through Point 1 to 2, Point 2 to 3 , Point 3 to 4 , point 4 to point 5 to complete round 1.
Please tell me how to achieve this functionality . I know about gestures, gesture overlay etc but none of them fits to my condition as they uses general touch events and gesture directions.
Please suggest me the way to achieve this and keep in mind I want to make this app to be able to run on all type of devices , so I can simply give the hard coded x,y values.
Edit : on Demand
I am posting the link of the app on play store who has same functionality , But I do not know How they are achieving this functionality .
https://play.google.com/store/apps/details?id=al.trigonom.writeletters
If each touch point can be created as individual views(e.g. ImageView), then you can create an inViewInBounds() function.
I used code from here to create code where I needed to detect finger press movement over multiple imageViews:
Rect outRect = new Rect();
int[] location = new int[2];
//Determine if a touch movement is currently over a given view.
private boolean inViewInBounds(View view, int x, int y){
view.getDrawingRect(outRect);
view.getLocationOnScreen(location);
outRect.offset(location[0], location[1]);
return outRect.contains(x, y);
}
To use this function, on a parent view to all those child touch points, set the Click and Touch Listeners:
//This might look redundant but is actually required: "The empty OnClickListener is required to keep OnTouchListener active until ACTION_UP"
parentView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {}
});
//All the work gets done in this function:
parentView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int)event.getRawX();
int y = (int)event.getRawY();
// ** myTouchPoint might be an array that you loop through here...
if ( inViewInBounds(myTouchPoint, x, y) ) doLogic(myTouchPoint);
return false;
}
});
The code above only shows detecting when one of your views are 'touched'.
if none are 'touched' but a view is 'active' (e.g. When a touch is detected, set a variable like: viewLastTouched = myTouchPoint) then you would call something like drawingLine(viewLastTouched, x, y) function - for whatever it needed to do to draw the line and/or detect boundaries etc.
They are not using android native java code to build this app.
The app is running with this code
import Runtime.MMFRuntime;
public class Main extends MMFRuntime {
}
This in turn is from https://github.com/ClickteamLLC/android/blob/master/docs/index.md
This is used to package apps / games written using - http://www.clickteam.com/clickteam-fusion-2-5

How to start a new scene while touchevent in libgdx

I am developing a small agme in android with libgdx. I placed an actor as button. I want to navigate to other screen while clicking on that actor. How is this possible.
Thanks in advance
You just need to add a InputListener to your Actor:
actor.addListener( new InputListener {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
game.setScreen(new OtherScreen());
return true;
}
});
Here you give the Actor an InputListener (there are many other methods you need to override). This InputListener reacts on touch down methods and if it is touched, you set the games Screen (you need to have the game instance somewhere, so that you are able to set its Screen) to the OtherScreen.
The Actor must be touchable (setTouchable(true)) and visible (setVisible(true)), to recieve the Events.
Hope it helps.

How to enable drawing with in the bitmap on canvas android

This is my requirement in my project,I am developing an application in android on canvas.The canvas covers the entire screen,I am drawing a bitmap on the canvas depends upon the aspect ration with the canvas.How to make restrict drawing outside of bitmap,Could any one help in this.
Your onTouch should look something like this, yes?
public boolean onTouch(View v, MotionEvent event) {
x = (int) event.getRawX();
y = (int) event.getRawY();
/* other code */
}
Well, what you can do is restrict the onTouch method from doing anything when X and Y are not on the bitmap.
onTouch also returns a boolean if an onTouch has "really" consumed an event. In this case, you want to return true when the X, Y values are on the bitmap, but false when X, Y values are off the bitmap.
Also, if you want the event to act like they are lifting their finger up when they are off the bitmap, you can even call onTouch again passing in an event like:
this.onTouch(v, MotionEvent.ACTION_UP);
I hope this helps!

Fling implementation on android canvas

I have the usual gesture detector for detecting fling , It is an instance attribute of a SurfaceView
GestureDetector flingDetector = new GestureDetector(getContext(),new SimpleOnGestureListener() {
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// Fling implementation
return true;
}
});
I am drawing a lot of complex stuff on a canvas and I have a translate(dx,dy) method that I use with onScroll.
So my question is how do I implement the fling using the translate method ?
There seem to be a lot of questions on detecting fling , my question is on implementing it .
I am not sure this will answer your question, I'll give it a try.
Check http://developer.android.com/reference/android/view/MotionEvent.html for MotionEvent.
You can use the two events received as e1 and e2 on the onFling method, and calculate coordinate differences with e1.getX(), e2.getX(), e1.getY(), e2.getY().... With this you would have the dx and dy to use with translate(dx,dy).
Since the fling seems more of a dynamic gesture, you could decide that fling means an ampler movement, and apply an amplification factor to dx and dy, so that when the user scrolls, they get a precise movement, but on fling, the actual movement gets amplified.
If this factor depends on velocity, you have a custom response for every user input.
(A different thing would be animating the result, which I guess would depend on other things).
An example I might try if it were me:
User scrolls softly: Movement is dx,dy. Translate(dx,dy).
User flings:
Real motion: dx=(e2.getX()-e1.getX(). dy = (e2.getY()-e1.getY().
Fling factor: (Custom implementation).
Modified motion: dxModified = dx*velocityX*F. dyModified = dy*velocityY*F.
Finally: translate (dxModified,dyModified)
Hope this helps to some extent.
Edit: I did not realize this question was from 2012, hopefully this will help someone some time. It would be nice to know about the final implementation anyway!.

Android images and buttons

What should I use if I want to have buttons on an image? I mean, being able to click on certain points of an image and see some info.
What if I want to display text on that points? Imagine I am having a map of my neighbor for example, and I want some info to be displayed (for example my home). And if someone press on my home he could see further details, like my name, my phone and so on. The image(aka the map) will be stable, it wont change, like a background.
You'll have to implement the method onTouch from the interface OnTouchListener. For example:
OnTouchListener listener = new OnTouchListener() {
#Override
public false onTouch(View v, MotionEvent event) {
if(!event.getAction() == MotionEvent.ACTION_DOWN)
return false; //If the touch event was not putting the finger down on the screen, return false(Actions may be move, up, and so on)
final float x = event.getX();
final float y = event.getY();
if(x and y are in some range)
doFirstThing();
else if(x and y are in another range..)
doSecondThing();
//... and so on...
//In the end, you must return a boolean saying whether you "consumed" the event - if you handled the event or not.
}
}
And don't forget to set the listener as the OnTouchListener for your ImageView!
imageView.setOnTouchListener(listener);
I believe for what you are trying to achieve you don't need to use buttons. Instead, create a custom view, in your onDraw method draw the images and handle the onTouch events as Jong suggested. Once you detected a click on your home for example, you could create another view with info about it and display it to user.
For info on how to create custom view you may refer to this queestion for example. Google around for it

Categories

Resources