0

I am making a Pong game and when I click on the screen the paddle jump to my cursor point. I want that I need to drag the cursor to move him and without jumping like normal Pong game. how can I do this?

This is my Paddle class:

public class Paddle {

private Vector3 position;
private int width, height;
private Texture texture;

public Paddle(int x, int y, int width, int height){
    this.width = width;
    this.height = height;
    createTexture(width,height);
    position = new Vector3(x, y, 0);
}

private void createTexture(int width, int height) {
    Pixmap pixmap = new Pixmap(width, height, Pixmap.Format.RGBA8888);
    pixmap.setColor(Color.BLACK);
    pixmap.fillRectangle(0, 0, width, height);
    texture = new Texture(pixmap);
    pixmap.dispose();
}

public void update(int y){
    position.add(0, y - position.y,0);
    position.y = y;
    position.set(position.x, HeadGuns.HEIGHT - position.y, position.z);
}

public void draw(SpriteBatch sb){
    sb.draw(texture, position.x, position.y, width,height);
}

This is my PlayState class:

public class PlayState extends State {

private Paddle myPaddle;

public PlayState(GameStateManager gsm) {
    super(gsm);
    myPaddle = new Paddle(25, HeadGuns.HEIGHT/2, 25, 150);
}

@Override
public void handleInput() {
    if (Gdx.input.isTouched()){
        //when I touched the screen
        myPaddle.update(Gdx.input.getY());
    }
}

@Override
public void update(float dt) {
    handleInput();
}

@Override
public void render(SpriteBatch sb) {
    sb.begin();
    myPaddle.draw(sb);
    sb.end();
}

@Override
public void dispose() {

}
Guy Rajwan
  • 33
  • 1
  • 10
  • Are you saying that you want the user to click and drag the paddle, or are you saying that you want the paddle to scroll to where the user clicked? – Vice Jan 18 '18 at 23:38
  • I want the user to click and drag the paddle – Guy Rajwan Jan 18 '18 at 23:48

1 Answers1

1

You are reading touch position:

Gdx.input.getY()

and using it directly to set pad position - you can't do that.

You should use InputLister to get events.

First you should listen to touchDown and see is user touching your pad or not (compare touch coordinates with pad coordinates)

Then, for dragging you should use touchDragged() event...to update pad position when dragging happen, but only if touchDown detected that touch:

https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/InputListener.html#touchDragged-com.badlogic.gdx.scenes.scene2d.InputEvent-float-float-int-

MilanG
  • 6,994
  • 2
  • 35
  • 64
  • How can I do this? – Guy Rajwan Jan 20 '18 at 00:06
  • Implement InputListener interface in your class, add your code into those 2 methods I mentioned above. In touch down just check if touch happened on your pad. If it is set some variable to TRUE - movement just started. You could also implement touchUp method to set same variable to false, when user lifts the finger. Then in touchDragged check if this variable is true and if it is adjust pad coordinate for the value you get as parameter. – MilanG Jan 20 '18 at 14:06
  • I need to implement InputListener to my paddle class or to my PlayState class? – Guy Rajwan Jan 21 '18 at 21:41
  • It's up to you how you are going to organize your code. Where ever you want to receive input events. And when you implement interface don't forget to register your listener. Just call some method with your object that implements listener as a parameter - check on some doc/tutorial. – MilanG Jan 22 '18 at 07:33
  • Mark my answer as solving then please. – MilanG Jan 25 '18 at 07:24