Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
Please check out stackoverflow.com/questions/5584977/… –  amb Sep 5 '12 at 22:58

2 Answers 2

up vote 4 down vote accepted

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

share|improve this answer
    
thank you, It cleared it up a bit, will try again tomorrow. Btw, did this: if(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH != null) { Log.d("TAG", "IT HASZSZSZSZSSZ IT"); }else{ Log.d("TAG", "IT DOSNT :(:(:(:(:("); } And my phone does have it, so thats a relief :) Ill just have to figure out how to do it, thanks for ur help m8 :) –  Max Sep 5 '12 at 23:54
    
FYI, you need to have PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH_DISTINCT for what you're trying to do in your game, not just PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH –  TheMaster42 Sep 5 '12 at 23:56
    
you buzzkill :) turned my smile into a frown. But tried that aswell and it has that to so :). I think its mostly american phones that lacks multi-touch since apple has some sort of patent on it there. –  Max Sep 5 '12 at 23:58
    
Yeah my first answer is totally going up the wrong tree. Looking at your code and the hints you gave in your edit, it's impossible for your code to receive the multi-touch events currently. So yeah, check out the article in the second answer. –  TheMaster42 Sep 6 '12 at 0:00

What exactly "doesn't work" about the multi-touch? The second touch provides no apparent effect at all, or it provides some input but the inputs don't work properly?

Many Android phones, even newer ones, may not have "true" multi-touch. These phones have hardware limitations that effectively let them mimic pinching and rotating gestures, but don't actually provide true multi-touch. These phones break down when two touch inputs are near each other on the horizontal or vertical axis - as your two joysticks are very likely to be.

Further reading: http://www.badlogicgames.com/wordpress/?p=1007

Further Post-Edit Edit: The Galaxy Nexus does support true multi-touch. This answer is unrelated to the problem you're currently facing.

share|improve this answer
    
Ill edit question to make it more clear –  Max Sep 5 '12 at 22:09
    
@Max you may be missing TheMaster42's point - the point is that it's very possible, even likely, that you cannot get what you're trying to do to work - that the device itself simply isn't capable of doing what you're trying to do. –  Steven Stadnicki Sep 5 '12 at 22:57
    
Max, this sounds exactly like the problem I'm describing. Check that the phone you're testing on supports true multi-touch by using the PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH_DISTINCT flag on the PackageManager.hasSystemFeature() method. See the article for more details. –  TheMaster42 Sep 5 '12 at 23:19
    
alright I see what your saying. But im using a galaxy nexus with the newest android version. And Ive played a bunch of multi-touch games on it. –  Max Sep 5 '12 at 23:20
    
Check the article. The Nexus is exactly one of the newer phones that doesn't actually support true multi-touch. You can do gestures that seem to use multi-touch, but it's not actually true multi-touch. The phone can't differentiate which touch is which when some of the touches are close on the horizontal or vertical axis. That's why a pinch or rotate will work fine - you don't care which finger is which when you're rotating or pinching. But two joysticks at the same level is exactly the kind of setup the phone fails at. –  TheMaster42 Sep 5 '12 at 23:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.