1
\$\begingroup\$

I'm back with another question that may be really simple.

I've a texture drawn on my spritebatch and I'm making it move up or down (y-axis only) with Libgdx's Input Handler: touchDown and touchUp.

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    myWhale.touchDownY = screenY;
    myWhale.isTouched = true;
    return true;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    myWhale.isTouched = false;
    return false;
}

myWhale is an object from Whale Class where I move my texture position:

public void update(float delta) {
    this.delta = delta;
    if(isTouched){
        dragWhale();
    }
}

public void dragWhale() {
    if(Gdx.input.getY(0) - touchDownY < 0){
        if(Gdx.input.getY(0)<position.y+height/2){
            position.y = position.y - velocidad*delta;
        }
    }
    else{
        if(Gdx.input.getY(0)>position.y+height/2){
            position.y = position.y + velocidad*delta;
        }
    }
}

So the object moves to the center of the position where the person is pressing his/her finger and most of the time it works fine but the object seems to take about half a second to move up or down and sometimes when I press my finger it wont move.

Maybe there's another simplier way to do this. I'd highly appreciate if someone points me on the right direction.

\$\endgroup\$
2
  • \$\begingroup\$ The code points out that your current approach is not to move the whale to the finger, but to drag it, keeping as offset the initial touch position. Be clearer what you're currently facing, and what you want to achieve. \$\endgroup\$ Jun 21, 2014 at 20:38
  • \$\begingroup\$ Hi. Yes, the whale will move upwards or downwards depending on how the finger is moving, like dragging to where the finger is. \$\endgroup\$
    – user48352
    Jun 21, 2014 at 21:26

3 Answers 3

1
\$\begingroup\$

Try to extend the whale as an Actor and add an ActorGestureListener to it. Use the pan function to drag the whale. It will be something like this:

void pan(event, x, y, deltaX, deltaY){
    myWhale.position.add(deltaX, deltaY);
}

And I think you'll have to use Stage for that, but I'm not sure.

\$\endgroup\$
0
0
\$\begingroup\$

No need to take an extra class for Input just write the following code in render method :-

if (Gdx.input.isTouched())
 {                
    Rectangle   touchRectangle = new Rectangle(Gdx.input.getX( ),
                        Gdx.graphics.getHeight() - Gdx.input.getY( ), 10,
                        10);

//just check if your finger overlaps on your character then move according to it
                if (touchRectangle 
                        .overlaps(whaleRectangle)) {
                      whaleRectangle.x=Gdx.input.getX();
                      whaleRectangle.y=Gdx.input.getY();

   }
   }
\$\endgroup\$
0
\$\begingroup\$

If your class implements InputProcessor:

  @Override
  public boolean touchDown(int screenX, int screenY, int pointer, int button) {

    MoveToAction mta = new MoveToAction();
    mta.setPosition(screenX, Math.abs(stage.getHeight()-screenY));
    mta.setDuration(5f);  //moves to new location by this time interval
    stage.addAction(mta);
    return false;
  }
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .