0
\$\begingroup\$

On the bottom left of my screen is a circle with an Arrow and on the bottom right is a circle with a gun. If the player is in jetpack mode and the Arrow "button" is clicked he flies like flappy bird (touch down = push him upwards, touch up = he falls down) - if the player is in jump mode he simply jumps.

My problem is I can't seem to fly the jetpack upwards AND shoot a bullet at the same time (i.e. the case where I'm tapping both the Arrow and the Gun). Is there some sort of trick I can use here?

My touchDown() method is in a class Controller looks like this:

@Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        int tapY = Gdx.graphics.getHeight() - screenY;
        //true if the arrow (bottom left) is tapped
        arrowClicked = inRange(upArrowSprite, screenX, tapY);
        //true if the gun (bottom right) is tapped
        gunClicked = inRange(gunSprite, screenX, tapY);


        // if arrow is pressed and player isn't in jetpack mode, make him jump
        if (arrowClicked) {
            if (!(player.getState() == ObjectState.JETPACK_MODE)) {
                player.setState(ObjectState.JUMPING);
                player.jump();
            // if the player is in jetpack mode, accelerate him upwards every time arrow is tapped
            } else {
            player.setTouchedDownFlying(true); // set boolean to be true
            player.setState(ObjectState.JETPACK_MODE);
        }
        }
        //if the gun is tapped (bot right), shoot a bullet
        if (gunClicked) {
            player.shoot();
            }
        return true;
    }

these are the relevant methods in Player:

public void update() {
    moveBy(xSpeed, -gravity);
    ...
    if(touchedDownFlying) {
        jetPackFly();
    }
    ...
}

public void setTouchedDownFlying(boolean bool) {
    touchedDownFlying = bool;
}

public void jetpackFly() {
        setySpeed(getySpeed() + flyAcceleration);
}

public void shoot() {
        Bullet i = new Bullet(this);
        bullets.add(i);
}

Any help is highly appreciated, thank you :)

\$\endgroup\$
1
\$\begingroup\$

It doesnt work, because you dont count fingers on screen, so no matter how many fingers will be on screen, touchDown always returns first touched coodrinates, next touches are ignored. If you want to use multitouch, then you have to make use of pointer variable in touchDown method. Actually i answered very similar question once in stackoverflow, and im sure it works, so i will just shamelessly copy and paste my answer ;)

I encoutered similar problem once,trying to set up 3 buttons on screen. Those buttons can be pressed separately (then there is no problem, you always use just one finger and one pointer - therefore pointer ID can be ignored), but they should also react properly, if any two of them are pressed, or player presses all at once. You have to make use of pointer ID, given by InputProcessor. Pointer ID is an int from 0 to 9 range (for 10 fingers). I did it that way:

first declared three variables for three pointers:

int forwardPointer, leftPointer, rightPointer;

then in TouchDown method i checked, which finger pressed which button (was it first, second or third press?), and stored this information in my variables for future use.

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    if (game.forwardButton.contains(screenX, screenY)) {
        game.forwardButton.isPressed=true;
        forwardPointer=pointer; 
    }
    if (game.leftButton.contains(screenX, screenY)) {
        game.leftButton.isPressed=true;
        leftPointer=pointer;
    }
    if (game.rightButton.contains(screenX, screenY)) {
        game.rightButton.isPressed=true;
        rightPointer=pointer;
    }}

and then, in TouchUp method i checked, which finger was lifted up (by comparing pointer ID from TouchUp method with stored pointer variable value), and according to this, i "relased" proper button.

    @Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    if (pointer==forwardPointer) {
        game.forwardButton.isPressed=false;
    }
    if (pointer==leftPointer) {
        game.leftButton.isPressed=false;
    }
    if (pointer==rightPointer) {
        game.rightButton.isPressed=false;
    }}

https://stackoverflow.com/questions/28130461/libgdx-multitouch-handling-using-inputprocessor/28131934#28131934

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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