foreword: on desktop everything works just perfect.
Trouble appers when i'm testing my project on phone
I start the game. Here is some buttons on screen: button_1, button_2, button_3. For example i'm touching button_1: with very first touch after app start nothing happing at all. If i'm touching button_1 again, it works fine -> touchDown (button image goes down for 10px) then touchUp (button image goes up for 10px and button code run). But if i touch another button instead, for example button_2, only touchDown of button_1 occurs (button_1 image goes down for 10px and up) and nothing else. It's happens for every button, so i need to touch button twice to make it work.
Button class:
public class myButton {
private float x, y, width, height;
private Texture buttonUp;
public Rectangle bounds;
private boolean isPressed = false;
public myButton(float x, float y, float width, float height, Texture buttonUp) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.buttonUp = buttonUp;
bounds = new Rectangle(x, y, width, height);
}
public boolean isClicked(int screenX, int screenY) {
return bounds.contains(screenX, screenY);
}
public void draw(SpriteBatch batch) {
if (isPressed) {
batch.draw(buttonUp, x, y - 10, width, height);
} else {
batch.draw(buttonUp, x, y, width, height);
}
}
public boolean isTouchDown(int screenX, int screenY) {
if (bounds.contains(screenX, screenY)) {
isPressed = true;
return true;
}
return false;
}
public boolean isTouchUp(int screenX, int screenY) {
if (bounds.contains(screenX, screenY) && isPressed) {
isPressed = false;
return true;
}
isPressed = false;
return false;
}
}
InputHandlerer:
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
screenX = (int) touchPos.x;
screenY = (int) touchPos.y;
playButton.isTouchDown(screenX, screenY);
return true;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
screenX = (int) touchPos.x;
screenY = (int) touchPos.y;
if (playButton.isTouchUp(screenX, screenY)) {
start();
return true;
}
}
in create() method i've got:
touchPos = new Vector3();
Gdx.input.setInputProcessor(new InputHandlerer());
camera = new OrthographicCamera();
viewport = new FitViewport(1080, 1920, camera);
in render() method i've got:
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
batch.setProjectionMatrix(camera.combined);
I repeat - on desktop every button works properly, but on phone not. What could be the problem?
Maybe on desktop is working because you can do only one click at a time, instead on phone you should manage the pointers, also if you don't want to use the multitouch.
On your InputHandlerer class you have to initilize:
private int button1pointer = -1;
private boolean button1isPressed = false;
Then you have to manage the pointers on your tochDown/Up methods:
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
for (int i = 0; i < 2; i++) { //here you can choose how many touches you can manage
if (Gdx.input.isTouched(i)) {
screenX = (int) touchPos.x;
screenY = (int) touchPos.y;
if (playButton.isTouchDown(screenX, screenY) && (button1pointer != i) && (!button1isPressed)) {
button1pointer = i;
button1isPressed = true;
}
return true;
}
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
if (!(Gdx.input.isTouched(pointer)) && (button1pointer == pointer) && (button1isPressed)) {
button1pointer = -1;
button1isPressed = false;
start();
return true;
}
}
Related
I recently asked this question, it works perfectly, the only problem I'm having is that the whole game is based on touch events, when a user touches the screen, an object gets created.
What's happening now is that when a user touches the pause button (texture packer), an object gets created and the game is paused. I want to prevent objects from being created if the pause is touched. I used to be able to do something like this:
private Vector3 touchPos;
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
if (Gdx.input.justTouched()) {
if (touchPos.x > pauseX && touchPos.x < pauseX + pauseX) {
if (touchPos.y > pauseY && touchPos.y < pauseX + pauseY) {
setGamePause(!getGamePause());
}}}
But doesn't seem to be working with texture packer, maybe my implementation is wrong, not sure, is there another approach?
private float pauseY = Gdx.graphics.getHeight() - 115;
private float pauseX = Gdx.graphics.getWidth() / 6;
button.setSize(150, 150)
It's on the top left of the screen
If pauseX and pauseY is left position and right position of a rectangle/button respectively then
you need width and height of button/rectangular area, like suppose that buttonWidth, buttonHeight.
if(Gdx.input.justTouched()) {
if (touchPos.x > pauseX && touchPos.x < pauseX + button.getWidth()) {
if (touchPos.y > pauseY && touchPos.y < pauseY + button.getHeight()) {
setGamePause(!getGamePause());
}
}
}
Please Check Test :
public class GdxTest extends Game implements InputProcessor{
private Stage stage;
Vector3 vector3;
TextButton button;
float pauseX,pauseY;
#Override
public void create() {
vector3=new Vector3();
ExtendViewport extendViewport=new ExtendViewport(700,1200,new OrthographicCamera());
stage=new Stage(extendViewport);
Skin skin=new Skin(Gdx.files.internal("skin/uiskin.json"));
skin.get("font-label", BitmapFont.class).getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
pauseX=300;
pauseY=500;
button=new TextButton("Pause",skin);
button.setPosition(pauseX,pauseY);
stage.addActor(button);
Gdx.input.setInputProcessor(this);
}
#Override
public void render() {
super.render();
Gdx.gl.glClearColor(0,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();
stage.act();
////Touch Detection without processor
vector3.set(Gdx.input.getX(),Gdx.input.getY(),0);
stage.getCamera().unproject(vector3);
if(Gdx.input.justTouched()){
if(vector3.x>pauseX && vector3.x<pauseX+button.getWidth() && vector3.y>pauseY && vector3.y<pauseY+button.getHeight())
System.out.println("TOuched");
}
///////
}
#Override
public void resize(int width, int height) {
super.resize(width,height);
stage.getViewport().update(width,height);
}
#Override
public void dispose() {
stage.dispose();
}
#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 button1) {
vector3.set(screenX,screenY,0);
stage.getCamera().unproject(vector3);
if(vector3.x>pauseX && vector3.x<pauseX+button.getWidth() && vector3.y>pauseY && vector3.y<pauseY+button.getHeight())
System.out.println("TOuched");
return false;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
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;
}
}
I have tried finding an answer to solve the problem, but I think that I don't seem to understand how to use the long press in Libgdx.
I want my character to move right when I long press on the right half of the screen and left when I long press on the left half of the screen.
I have searched and tried.
Here is my InputHandler class :
public class InputHandler implements InputProcessor {
private MainCharacter myMainCharacter;
public InputHandler(MainCharacter mainCharacter) {
myMainCharacter = mainCharacter;
}
#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) {
myMainCharacter.onClick();
return false;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
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;
}
}
And here is my MainCharacter class :
public class MainCharacter {
private Vector2 position;
private Vector2 velocity;
private Vector2 acceleration;
private float rotation;
private int width;
private int height;
public MainCharacter(float x, float y, int width, int height) {
this.width = width;
this.height = height;
position = new Vector2(x, y);
velocity = new Vector2(0, 0);
acceleration = new Vector2(0, 460);
}
public void update(float delta) {
velocity.add(acceleration.cpy().scl(delta));
if (velocity.y > 200) {
velocity.y = 200;
}
position.add(velocity.cpy().scl(delta));
}
public void onClick() {
if (Gdx.input.getX() <= 135) {
Gdx.app.log("TAG", "LEFT");
position.x--;
} else if (Gdx.input.getX() >= 137) {
Gdx.app.log("TAG", "RIGHT");
position.x++;
}
}
public float getX() {
return position.x;
}
public float getY() {
return position.y;
}
public float getWidth() {
return width;
}
public float getHeight() {
return height;
}
public float getRotation() {
return rotation;
}
}
I used onClick() method as a replacement until I find a solution for the problem. It works fine but it doesn't have the same effect as the long press. My character moves left when I click on the left side of the screen and right when I click on the right side of the screen. But of course it doesn't work when I long press.
So how can I use 'Long Press' ?
I would really appreciate your help.
Gokul gives a nice overview of the GestureListener but I do not believe this is what you are looking for. LongPress indeed only registers after some seconds of pressing and you want to have a touch control the character immediately.
There is no out of the box method in the listeners to keep detecting touched but you can create it yourself.
if (Gdx.input.isTouched())
{
//Finger touching the screen
// You can actually start calling onClick here, if those variables and logic you are using there are correct.
if (Gdx.input.getX() >= screenSize / 2)
{
//Right touched
}
else if (Gdx.input.getX() < screenSize / 2)
{
//Left touched
}
}
Just check for this every frame and do your logic.
You can implement the long press by implementing the GestureListner interface.
GestureDetector will let you detect the following gestures:
touchDown: A user touches the screen.
longPress: A user touches the screen for some time.
tap: A user touches the screen and lifts the finger again. The finger must not move outside a specified square area around the initial touch position for a tap to be registered. Multiple consecutive taps will be detected if the user performs taps within a specified time interval.
pan: A user drags a finger across the screen. The detector will report the current touch coordinates as well as the delta between the current and previous touch positions. Useful to implement camera panning in 2D.
panStop: Called when no longer panning.
fling: A user dragged the finger across the screen, then lifted it. Useful to implement swipe gestures.
zoom: A user places two fingers on the screen and moves them together/apart. The detector will report both the initial and current distance between fingers in pixels. Useful to implement camera zooming.
pinch: Similar to zoom. The detector will report the initial and current finger positions instead of the distance. Useful to implement camera zooming and more sophisticated gestures such as rotation.
A GestureDetector is an event handler. To listen for gestures, one must implement the GestureListener interface and pass it to the constructor of the GestureDetector. The detector is then set as an InputProcessor, either on an InputMultiplexeror as the main InputProcessor:
public class MyGestureListener implements GestureListener{
#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) {
return false;
}
#Override
public boolean longPress(float x, float y) {
return false;
}
#Override
public boolean fling(float velocityX, float velocityY, int button) {
return false;
}
#Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
return false;
}
#Override
public boolean panStop(float x, float y, int pointer, int button) {
return false;
}
#Override
public boolean zoom (float originalDistance, float currentDistance){
return false;
}
#Override
public boolean pinch (Vector2 initialFirstPointer, Vector2 initialSecondPointer, Vector2 firstPointer, Vector2 secondPointer){
return false;
}
#Override
public void pinchStop () {
}
}
You have to set the GestureDetector that contains your GestureListener as the InputProcessor:
Gdx.input.setInputProcessor(new GestureDetector(new MyGestureListener()));
For more details, check out the link below
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/input/GestureDetector.GestureListener.html
By following Kilbolt's Zombie Bird tutorial I created in my project a class to represent all buttons:
public class SimpleButton {
private float x, y, width, height;
private TextureRegion buttonUp;
private TextureRegion buttonDown;
private Rectangle bounds;
private boolean isPressed = false;
public SimpleButton(float x, float y, float width, float height,
TextureRegion buttonUp, TextureRegion buttonDown) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.buttonUp = buttonUp;
this.buttonDown = buttonDown;
bounds = new Rectangle(x, y, width, height);
}
public boolean isClicked(int screenX, int screenY) {
return bounds.contains(screenX, screenY);
}
public void draw(SpriteBatch batcher) {
if (isPressed) {
batcher.draw(buttonDown, x, y, width, height);
} else {
batcher.draw(buttonUp, x, y, width, height);
}
}
public boolean isTouchDown(int screenX, int screenY) {
if (bounds.contains(screenX, screenY)) {
isPressed = true;
return true;
}
return false;
}
public boolean isTouchUp(int screenX, int screenY) {
// It only counts as a touchUp if the button is in a pressed state.
if (bounds.contains(screenX, screenY) && isPressed) {
isPressed = false;
return true;
}
// Whenever a finger is released, we will cancel any presses.
isPressed = false;
return false;
}
}
Whenever I want to create a button I do it in my InputProcessor class by creating a SimpleButton object and after filling its parameters I add it to List list.
My current problem is that I'm trying to create a sound button, which I did create and it work fine, but what I'm struggling with is making the button change TextureRegion between ON and OFF states.
This is an example of how I create a button(actually I created the sound button) and put it in the list:
soundButton = new SimpleButton(
136 / 2 - (AssetLoader.soundOnNotClicked.getRegionWidth() / 0.25f),
midPointY - 102, 15, 15, AssetLoader.soundOnNotClicked,
AssetLoader.soundClicked);
gameButtons.add(backButton);
I tried using the same condition that when sound ON is on it will put inside the parameters "AssetLoader.soundOnNotClicked" and when it's OFF "AssetLoader.soundOFFNotClicked", but it just show the first TextureRegion in the condition. Can someone help me figure out how I can change TextureRigions when sound is ON and OFF?
EDIT WITH CHANGES:
First created a second constractor:
public SimpleButton(float x, float y, float width, float height,
TextureRegion buttonUp, TextureRegion buttonDown, TextureRegion buttonOn) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.buttonUp = buttonUp;
this.buttonDown = buttonDown;
this.buttonOn = buttonOn;
bounds = new Rectangle(x, y, width, height);
}
Then changed a bit the isClicked method:
public boolean isClicked(int screenX, int screenY) {
if (isOn == false) {
isOn = true;
}else {
isOn = false;
}
return bounds.contains(screenX, screenY);
}
Then changed the draw method:
public void draw(SpriteBatch batcher) {
if (isPressed) {
batcher.draw(buttonDown, x, y, width, height);
} else {
if (isOn == false) {
batcher.draw(buttonUp, x, y, width, height);
}else{
batcher.draw(buttonOn, x, y, width, height);
}
}
}
Lastly I changed the creation of the button by adding the textureRegion of the OFF sound:
soundButton = new SimpleButton(
136 / 2 - (AssetLoader.soundButtonOff.getRegionWidth() / 0.25f),
midPointY - 102, 15, 15, AssetLoader.soundButtonOff,
AssetLoader.soundButtonOn, AssetLoader.soundButtonOffX);
menuButtons.add(soundButton);
Make another boolean called isOn just like isPressed
Add another TextureRegion called buttonOn just like buttonDown
Add TextureRegion buttonDown to your constructor and set it there like you do with buttonDown
Flip the boolean whenever the button is clicked in isClicked(). (isOn = !isOn)
Add the condition in your draw method to draw the buttonOn texture if isOn is true.
That should pretty much do it.
In the past few days I've been porting my game (Apopalypse) to the Android Mobile platform. I've did a quick search on Google of sprite touch detection but didn't find anything helpful. Each balloon will pop once touched and I just need to detect if it's touched.
Here's my balloon spawning code:
Rendering (x, y, width, and height are randomized):
public void render() {
y += 2;
balloon.setX(x);
balloon.setY(y);
balloon.setSize(width, height);
batch.begin();
balloon.draw(batch);
batch.end();
}
Spawning in main game class:
addBalloon(new Balloon());
public static void addBalloon(Balloon b) {
balloons.add(b);
}
in your class having the render method use can do following code:
Vector3 touchPoint=new Vector3();
void update()
{
if(Gdx.input.justTouched())
{
//unprojects the camera
camera.unproject(touchPoint.set(Gdx.input.getX(),Gdx.input.getY(),0));
if(balloon.getBoundingRectangles().contains(touchPoint.x,touchPoint.y))
{
// will be here when balloon will be touched
}
}
}
This is how I did it, but depending on the scene you are using and the elements that can be touched, there can be slightly more optimized ways of doing this:
public GameScreen implements Screen, InputProcessor
{
#Override
public void show()
{
Gdx.input.setInputProcessor(this);
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button)
{
float pointerX = InputTransform.getCursorToModelX(windowWidth, screenX);
float pointerY = InputTransform.getCursorToModelY(windowHeight, screenY);
for(int i = 0; i < balloons.size(); i++)
{
if(balloons.get(i).contains(pointerX, pointerY))
{
balloons.get(i).setSelected(true);
}
}
return true;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button)
{
float pointerX = InputTransform.getCursorToModelX(windowWidth, screenX);
float pointerY = InputTransform.getCursorToModelY(windowHeight, screenY);
for(int i = 0; i < balloons.size(); i++)
{
if(balloons.get(i).contains(pointerX, pointerY) && balloons.get(i).getSelected())
{
balloons.get(i).execute();
}
balloons.get(i).setSelected(false);
}
return true;
}
public class InputTransform
{
private static int appWidth = 480;
private static int appHeight = 320;
public static float getCursorToModelX(int screenX, int cursorX)
{
return (((float)cursorX) * appWidth) / ((float)screenX);
}
public static float getCursorToModelY(int screenY, int cursorY)
{
return ((float)(screenY - cursorY)) * appHeight / ((float)screenY) ;
}
}
i made a little class that i use for my games to detect is Sprite is touched
public class SpriteTouchable extends Sprite {
public SpriteTouchable(Texture texture) {
super(texture);
}
public SpriteTouchable(Sprite sprite) {
set(sprite);
}
public static float xTrans(float x)
{
return x*Gdx.graphics.getWidth()/480;
}
public static float yTrans(float y)
{
return y*Gdx.graphics.getHeight()/320;
}
private boolean isTouched;
/**
* Type: Input Listener function
* listen if this sprite button was pressed (touched)
* #param marge : the extra touchable space out of sprite
* #param x : x position touched by user
* #param y : y position touched by user
*
* return true : Sprite touched
* return false : Sprite not touched
*/
public boolean isPressing(int marge,int x, int y) {
if((x>getX() -xTrans(marge))&& x<getX() +getWidth()+xTrans(marge)) {
if((y>getY() -yTrans(marge))&& y<getY()+getHeight()+yTrans(marge)) {
return true;
}
}
return false;
}
}
public boolean isTouched() {
return isTouched;
}
here how i use it
Gdx.input.setInputProcessor(new GameInputListener() {
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
#Override
public boolean touchDown(int x, int yy, int pointer, int button) {
int y = Gdx.graphics.getHeight() - yy;
// if sprite + 10 of px marge is touched
if(mySpriteTouchable.isPressing(10, x, y)) {
// sprite is touched down
}
return false;
}
}
with the same logic you can detect sprite releasing and also you can customize it for effect size when the sprite is touched and more...
hope this was helpfull !
You have Gdx.input.getX() and Gdx.input.getY(). The X and Y coordinates of the last touch.
Just compare them with balloon frame.
You could add a tiny 1x1 pixel rectangle to your mouse and check if it intersects or contains other boxes. You do need to use boxes for all your objects you want to make clickable this way.
i have a simple solution
it's like that create a small rectangle 1 pixel by one pixel
Rectangle rect;
rect = new Rectangle(0, 0, 1, 1);
then make a method called
touching_checking();
in this method we will do three things
first check if screen is touched
second put the rectangle where you touch the screen
then at last check if your rectangle overlaps with the sprite rectangle.
like that
private void touching_checking() {
if (Gdx.input.isTouched()) {
rect.setPosition(Gdx.input.getX(), Gdx.input.getY());
if (rect.overlaps(back.getBoundingRectangle())) {
//do what you want here
}
}
}
i have an array of sprites called levels i check which one i pressed like that
private void touching_checking() {
if (Gdx.input.isTouched()) {
rect.setPosition(Gdx.input.getX(), Gdx.graphics.getWidth() - Gdx.input.getY());
for (int i = 0; i < levels.size; i++) {
if (rect.overlaps(levels.get(i).getBoundingRectangle())) {
//do what you want here
}
}
}
}
Here is what i use in my sprite class. I give it the vector where i clicked camera.unproject(new Vector3().set(x,y,0));. returns true if clicked in the area of the sprite.
/***
*
* #param v3 Vector with MouseClickPosition
* #return true if click was inside rectangle x --> x + width, y --> y +
* height
*/
public boolean clicked(Vector3 v3){
Rectangle rec = getBoundingRectangle();
if(v3.x >= rec.x && v3.x < rec.x + getWidth()){
if(v3.y >= rec.y && v3.y < rec.y + getHeight()){
System.out.println("click_on\t" + v3.x + ", " + v3.y);
return true;
}
else{
return false;
}
}
else{
return false;
}
}
below is my code which I am trying to add touchDragged, I am trying to use the drag method to make an object move left and right by using the touchDragged feature.
Problem is that when i swipe on the right side of the screen be it left or right swipe the object moves right only. when i swipe on the left side be it left or right the object moves left only.
is there something I am missing?
input class and below that the object class.
also is the Tilt action same as this or is that something different?
it will be nice if that can be explained.
Input class is as
public class Input implements InputProcessor
{
private Kame myKame;
public Input(Kame kame) {
myKame = kame;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
myKame.onClick();
return true; // Return true to say we handled the touch.
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
myKame.onClick();
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
myKame.onSwipe(screenX, screenY, pointer);
return true;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
#Override
public boolean scrolled(int amount) {
return false;
}
}
and Kame class is as :
public class Kame
{
private Vector2 position;
private Vector2 velocity;
private Vector2 acceleration;
private int width;
private int height;
public float x;
public Kame(float x, float y, int width, int height) {
this.width = width;
this.height = height;
position = new Vector2(x, y);
velocity = new Vector2(0, 0);
acceleration = new Vector2(0, 460);
}
public void update(float delta) {
}
public boolean onSwipe(int velocityX, int velocityY, int delta) {
position.add(position.cpy().scl(delta));
// if (Math.abs(velocityX) < Math.abs(velocityY)) {
if (velocityX > 136) {
position.x += 3;//x cordinate
velocity.y += 10;
} else if (velocityX < 136) {
position.x -= 3;
velocity.y += 10;
} else {
// Do nothing.
}
// } else {
// Ignore the input, because we don't care about up/down swipes.
// }
return true;
}
public float getX() {
return position.x;
}
public float getY() {
return position.y;
}
public float getWidth() {
return width;
}
public float getHeight() {
return height;
}
}
This call to onSwipe:
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
myKame.onSwipe(screenX, screenY, pointer);
return true;
}
does not seem to match this implementation of onSwipe:
public boolean onSwipe(int velocityX, int velocityY, int delta) {
There are a couple problems here.
The screenX and screenY parameters passed to touchDragged are screen coordinates. They range between 0 and the width (or height) of the screen. onSwipe says it expects a "velocity" (though the algorithm inside doesn't seem to treat it as such).
The pointer in the touchDragged signature is the id number of the cursor (think about using 4 fingers on a touchpad, the "pointer" identifies which finger is which). You're using is as a 'delta'.
You can probably just compare the screenX to pos.x, and add or subtract 3 from pos.x depending on if screenX is less than or greater than pos.x.
Computing an actual velocity will require tracking the change in location (which means keep track of the previous touch location), and then scaling it by the frame rate (the delta passed to render).
change the code from
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
myKame.onSwipe(screenX, screenY, pointer);
return true;
}
#Override
public boolean touchDragged(int screenX, int screenY, int button) {
myKame.onSwipe(screenX, screenY, pointer);
return true;
}
try that