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.