Tuesday, May 19, 2015

2d - android multitouch problem


Im aware that there a a couple of posts on this matter, but Ive tried all of them and none of them gets rid of my problem.


Im starting to get close to the end of my game so I bought a cabel to try it on a real phone, and as I expected my multitouch dosnt work. I use 2 joysticks, one to move my character and one to change his direction so he can shoot while walking backwards etc.


my local variable:


public void update(MotionEvent event) {
if (event == null && lastEvent == null) {
return;
} else if (event == null && lastEvent != null) {
event = lastEvent;

} else {
lastEvent = event;
}

int index = event.getActionIndex();
int pointerId = event.getPointerId(index);

statement for left Joystick:


if (pointerId == 0 && event.getAction() == MotionEvent.ACTION_DOWN && (int) event.getX() > steeringxMesh - 50 && (int) event.getX() < steeringxMesh + 50 && (int) event.getY() > yMesh - 50 && (int) event.getY() < yMesh + 50) {
dragging = true;

} else if (event.getAction() == MotionEvent.ACTION_UP) {
dragging = false;
}

if (dragging) {
touchingPoint.x = (int) event.getX(); //used to calculate angle of sprites movement
touchingPoint.y = (int) event.getY();

//code for moving my character


statement for my right joystick:


if (pointerId == 1 && event.getAction() == MotionEvent.ACTION_DOWN && (int) event.getX() > shootingxMesh - 50 && (int) event.getX() < shootingxMesh + 50 && (int) event.getY() > yMesh - 50 && (int) event.getY() < yMesh + 50) {
shooting = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
shooting = false;
}

if (shooting) {

touchingPoint2.x = (int) event.getX(); //used to calculate sprites angle and get the correct direction

touchingPoint2.y = (int) event.getY();
// code for aiming
}

This class is my main-Views onTouchListener and is called in a update-method that gets called in my game-loop, so its called every frame.


Im really at a loss here, I've done a couple of tutorials and Ive tried all relevant solutions to similar posts. Can post entire Class if necessary but I think this is all the relevant code. Just hope someone can make some sence out of this.


EDIT: both shooting and dragging joysticks work fine, but only when I press them one at a time, so I cant drag and shoot at the same time. I've looked around for a bit more and have understood that you need a MotionEvent.ACTION_POINTER_UP and MotionEvent.ACTION_POINTER_DOWN , but still dont know a way to implement it, some people use loops that goes through event.getPointerCount. Also with this Code my pointerId is always 0. To sum up: I want to be able to use both joysticks at the same time.



Answer



Your code is only looking at the Single Touch portion of the event. You will only ever receive information about the first finger to hit the screen. If you lift that finger, your event will jump over to the second finger that is down, right?


You need to implement MotionEvent.ACTION_MASK to get the multi-touch parts. Something like:



        int action = event.getAction() & MotionEvent.ACTION_MASK;

action can now contain MotionEvent.ACTION_POINTER_DOWN events in addition to the normal MotionEvent.ACTION_DOWN events you're polling for.


In addition, you need to get the index of the pointer associated with the event (i.e. the second finger down, third finger down, kind of).


        int pointerId = event.getPointerId(pointerIndex);

I'm sorry I'm very hazy on this as I didn't implement it directly myself for my project. This might be of help: http://tutorials-android.blogspot.com/2011/09/multi-touch-in-android.html


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...