0
\$\begingroup\$

I've been trying to solve this for hours now, In theory I totally understand how the GestureDetector works but I'm trying to integrate it with the Libgdx SuperJumper Tutorial (for now).

Here: https://github.com/libgdx/libgdx-demo-superjumper/tree/master/core/src/com/badlogicgames/superjumper

I know that you need to define a class which extends the Androids Gesture Listener Interface and the methods that come along with it, I know that when the user flings I need to change velocity.y and I know when he's in the air I'll need to slow it down in decrements. But I just seem to be making him Jump and he never comes back down. Now though, he does nothing because I've been tinkering to hard. All i want So that instead of him jumping automatically, he would jump on swipe.

This is the code I have so far (Bob.Java, MyGestureListener.Java and World.java). I'll post the relevant code only, Any help would be appreciated.

Bob.Java

public Bob (float x, float y) {
        super(x, y, BOB_WIDTH, BOB_HEIGHT);
        this.state = BOB_STATE_IDLE;
        this.stateTime = 0;
        this.setGrounded(true);
        this.setFacingRight(true);
        Gdx.input.setInputProcessor(new GestureDetector(new MyGestureListener(this)));
    }

    public void update (float deltaTime) {
        velocity.add(World.gravity.x * deltaTime, World.gravity.y * deltaTime);
        bounds.x = position.x - bounds.width / 2;
        bounds.y = position.y - bounds.height / 2;

        if (position.x < 0) position.x = World.WORLD_WIDTH;
        if (position.x > World.WORLD_WIDTH) position.x = 0;

        stateTime += deltaTime;
    }

world.Java

private void updateBob (float deltaTime, float accelX) {
        if (bob.state != Bob.BOB_STATE_HIT && bob.position.y <= 0.5f) bob.hitPlatform();
        if (bob.state != Bob.BOB_STATE_HIT) bob.velocity.x = -accelX / 10 * Bob.BOB_MOVE_VELOCITY;

        if (bob.state != Bob.BOB_STATE_HIT && bob.state == Bob.BOB_STATE_JUMP && MyGestureListener.fling) 
        {
            bob.velocity.y = deltaTime * Bob.BOB_JUMP_VELOCITY;
            bob.stateTime = 0;
        }

        bob.update(deltaTime);
        heightSoFar = Math.max(bob.position.y, heightSoFar);
    }

MyGestureListener.java

package com.wilson.game;

import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.math.Vector2;

public class MyGestureListener implements GestureListener {

    private Bob bob;
    public static boolean fling;

    MyGestureListener (Bob bob) 
    {
        this.bob = new Bob(bob.position.x, bob.position.y);
    }

    @Override
    public boolean touchDown(float x, float y, int pointer, int button) {
        return false;
    }

    @Override
    public boolean tap(float x, float y, int count, int button) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean longPress(float x, float y) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean fling(float velocityX, float velocityY, int button) {
         if(Math.abs(velocityX)>Math.abs(velocityY)){
                if(velocityX>0){
                        //right
                }else{
                        //left
                }
            }else{
                if(velocityY>0){
                    //down
                }else{ 
                    if (bob.state == Bob.BOB_STATE_IDLE || bob.state == Bob.BOB_STATE_WALKING);
                    bob.state = Bob.BOB_STATE_JUMP;
                    fling = true;
                }
            }
        return false;
    }

    @Override
    public boolean pan(float x, float y, float deltaX, float deltaY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean panStop(float x, float y, int pointer, int button) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean zoom(float initialDistance, float distance) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2,
            Vector2 pointer1, Vector2 pointer2) {
        // TODO Auto-generated method stub
        return false;
    }

}

My assumption is that when the user flings on the screen, the state of the character will change, depending on the state of that character change the velocity for the y axis but it doesn't do anything. HELP IS NEEDED.

\$\endgroup\$
  • \$\begingroup\$ Just noticed: "this.bob = new Bob(bob.position.x, bob.position.y);" Don't create new instance, just do "this.bob = bob;" If you do it like that, you end up having two different bob in code, thus it gets messed up pretty fast \$\endgroup\$ – Katu Apr 18 '15 at 4:56
  • \$\begingroup\$ I edited my answer to explain bob thing and introduced a fix for it. \$\endgroup\$ – Katu Apr 18 '15 at 5:02
2
\$\begingroup\$

You have to return true from touchDown.

Documentation says:

The GestureListener can signal whether it consumed the event or wants it to be passed on to the next InputProcessor by returning either true or false respectively from its methods.

So, if you return false from touchDown, that is the first even raised in all cases, it stops event handling. If you return true, it continues to other events ( ie. fling, longPress etc).

So, change touchDown to this:

@Override
public boolean touchDown(float x, float y, int pointer, int button) {
    return true;
}

EDIT: The way you are initializing the MyGestureListener is wrong. You are creating new instance of Bob and all the changes to bob inside MyGestureListener will only affect that instance. It means that the "Bob" you have in Bob.Java is not the same, thus no matter what gestures you record, those changes never affect the main bob.

Change this:

MyGestureListener (Bob bob) 
{
    this.bob = new Bob(bob.position.x, bob.position.y);
}

to this:

MyGestureListener (Bob bob) 
{
    this.bob = bob;
}
\$\endgroup\$
  • \$\begingroup\$ Good catch. I still don't see him actually changing bob's position, so that's another problem... \$\endgroup\$ – StrongJoshua Apr 15 '15 at 12:32
  • \$\begingroup\$ @StrongJoshua Yeah might be, but it could be auto-scrolling? Like flappy birds or something. \$\endgroup\$ – Katu Apr 15 '15 at 12:40
  • \$\begingroup\$ Could be, but it doesn't look like he's updating the y position either, so unless he moves the screen around the character, he shouldn't expect any movement. \$\endgroup\$ – StrongJoshua Apr 15 '15 at 12:43
  • \$\begingroup\$ @StrongJoshua github.com/libgdx/libgdx-demo-superjumper/blob/master/core/src/… Had to check it :D . It is somekind of jumper game and it seems that he's actually setting the state to JUMP, even thou he should do it bit different. Ill edit my answer a bit \$\endgroup\$ – Katu Apr 15 '15 at 12:54
  • \$\begingroup\$ Wow... he added the line I told him to add (check my answer and his code) and didn't even thank me :3 \$\endgroup\$ – StrongJoshua Apr 15 '15 at 12:57
1
\$\begingroup\$

In Bob's update method you do nothing with his velocity. He has no way of actually moving, with the code you have shown. You should do something along the lines of this before you reset the bounds:

position.add(velocity.x * deltaTime, velocity.y * deltaTime);
\$\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.