Search code examples
javaandroidlibgdx

inputprocessor touch down gives late response?


So I am currently busy with a kind of pong game for android. All i have currently is the paddle and the ball. The controls for the player (the paddle) is as followed:

  • If player touches on left side of screen, paddle goes left
  • If player touches on right side of screen, paddle goes right

Actually the above 2 points work fine, but what bothers me, is that when im holding right with my right finger, and after that release it and really fast holding left with my left finger, the paddle only executes one time "going left", and then stops, while I am still holding left. (and vice versa)

I discovered this only happens if I use two fingers. If im holding down right with my finger, and try to go really fast to press the other side, it doesnt stop and actually keeps going left.

But it is important to use two fingers, since that is the way how the game should be played.

This all may be explained unclear, so you can ask in the comments more specific questions.

Player.java: http://pastebin.com/pdFZJTRB

MyInputProcessor.java: http://pastebin.com/XPzi8JPB


Solution

  • I think your problem resides in the fact that you have the touchDown boolean being shared between the left and right sides. If you press the left side fast enough, the touchDown for the left finger gets set to true before the touchDown for the right finger is set to false, thus making them all false. Try something like this instead:

    package com.nahrot.teleportball;
    
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.InputProcessor;
    import com.badlogic.gdx.math.Vector2;
    
    public class MyInputProcessor implements InputProcessor
    {
        public Vector2 leftTouchPos = new Vector2();
        public Vector2 rightTouchPos = new Vector2();
        public boolean touchLeft = false;
        public boolean touchRight = false;
    
        @Override
        public boolean keyDown(int keycode)
        {
            return false;
        }
    
        @Override
        public boolean keyUp(int keycode)
        {
            return false;
        }
    
        @Override
        public boolean keyTyped(char character)
        {
            return false;
        }
    
        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button)
        {
            if(sideTouched(screenX, true))
        { 
                touchLeft = true;
                leftTouchPos.set(screenX, screenY);
        }
        if(sideTouched(screenX, false))
        {
            touchRight = true;
            rightTouchPos.set(screenX, screenY);
        }
            return false;
        }
    
        @Override
        public boolean touchUp(int screenX, int screenY, int pointer, int button)
        {
            if(sideTouched(screenX, true))
            { 
                touchLeft = false;
        }
        if(sideTouched(screenX, false))
        {
            touchRight = false;
        }
            return false;
        }
    
        @Override
        public boolean touchDragged(int screenX, int screenY, int pointer)
        {
            return false;
        }
    
        @Override
        public boolean mouseMoved(int screenX, int screenY)
        {
            return false;
        }
    
        @Override
        public boolean scrolled(int amount)
        {
            return false;
        }
    
        private boolean sideTouched(int x, boolean checkLeft)
        {
        if(checkLeft)
        {
            if(x <= Gdx.graphics.getWidth() / 2)
                return true;
        }
        else
        {
            if(x > Gdx.graphics.getWidth() /2)
                return true;
        }
        return false;
        }
    }
    

    Here, I separated the booleans and vectors into left side and right side, and made a convenience function for checking whether a touchDown or touchUp is on the right or left side of the screen, and adjusted the corresponding booleans and vectors accordingly. I assume the vector was needed to check which side was pressed anyway, so I suspect you could work it so that all you need is the two booleans for the sides touched. This code is untested, by the way, but you should get the idea if it doesn't work.