Libgdx: touchDragged, the object is not moved touch - java

My code moves an object by clicking on it and on the touch, I want to just touch on the moving object. How to disable the ability to navigate the click event object
Vector3 touchPos;
touchPos = new Vector3();
Gdx.input.setInputProcessor(this);
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
bucket.x = touchPos.x - 64 / 2;
return true;
}

Isn't your touchDragged method missing a parameter?
public void touchDragged( InputEvent event, float x, float y, int pointer )
The event object has 2 methods for affecting the propagation of an event,setBubbles(boolean) and cancel().
Also, the methods touchDown and touchUp should have the same event parameter.

Related

How do you implement Horizontal-Scrolling with LibGDX?

I want to program a game that is like the old "Warfare 1917" browser games. I am stuck on the scrolling part.I'll try to explain what I want to with a gif:
I have searched and tried everything the whole day but I am not able to find any real solutions to this. I hope you can understand what I am trying to do.
Since moving camera is a bad practice, better moving a Layer (the container of your sprites) or you can try ScrollPane, the implementation of the WidgetGroup
See https://libgdx.badlogicgames.com/ci/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/ScrollPane.html
p.s. tested on v. 1.9.11
I think this should do the trick
public class MovingCamera extends InputAdapter {
OrthographicCamera camera; // The camera to be moved
float pivotX; // The pivot for the movement
public MovingCamera() {
camera = new OrthographicCamera(); // Initialize camera
}
// Create a pivot
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
Vector3 unprojected = camera.unproject(new Vector3(screenX, screenY, 0)); // Convert from pixel to world coordinates
pivotX = unprojected.x; // Save first finger touch on screen (Will serve as a pivot)
return true; // Input has been processed
}
// Move the camera
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
Vector3 unprojected = camera.unproject(new Vector3(screenX, screenY, 0)); // Convert from pixel to world coordinates
camera.position.x += unprojected.x - pivotX; // Change camera position
camera.update(); // Apply changes
return true; // Input has been processed
}
}
And in your render method:
public void render(SpriteBatch spriteBatch) {
spriteBatch.setProjectionMatrix(camera.combined); // Let the Sprite Batch use the camera
spriteBatch.begin();
// [Draw your Textures, TextureRegions, Sprites, etc...]
spriteBatch.end();
}

LibGDX actor doesn't respond to click event

I got a screen class with a stage. In the constructor, I add UIBar and BattleScene to the stage. UIBar draws everything on the UI Bar, and BattleScene draws everything on the battlefield. BattleScene holds an array of CharacterSlots[6]. Each character slot has x and y positions, and actor attached to it.
In my BattleScene draw method, I call the draw methods of each CharacterSlot, and they call the draw method of the actor attached to them.
I got bounds set on my actor like so:
actor.setBounds(actor.getX(), actor.getY(), actor.getWidth(),actor.getHeight());
Now, everything gets nicely drawn on the screen, yet actors don't respond to the listener I attached to them in the constructor like so:
this.addListener(new ClickListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
System.out.println("ay");
currentlyChosen = reference;
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
}
});

Libgdx: How to detect touch on a separate stage actor?

I'm trying to create a game which is something like this:
Play area - No stage (scene2D) used.
In the play area, in render method:
b2dr.render(world, b2dcam.combined); // main camera
sb.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw(); // draws HUD over screen
I've touchDown/touchUp/touchDragged events defiend in play area and they work good. Inside HUD, I'm making use of a stage and a table to show it properly.
Now lets say I've a button in HUD on which I want to receive click events. How do I do that? I tried this inside my HUD class but no success:
myBtn.addListener(new InputListener()
{
#Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button)
{
System.out.println("touchdown");
return true;
}
#Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button)
{
System.out.println("touchup");
}
});

libgdx touchDown event is executed only once [duplicate]

I have a problem with using scene2d in libgdx. I can't find anywhere a method that allows me to check wheter the actor is touched or not. I can only find methods that told me if actor was touched or released. In my game, when actor is pressed and hold, some things should be done every frame, not only in one moment that I put my finger on it. I want to stop the things when I release my finger.
You can keep track of this in your InputListener. Create a boolean field isTouched, set to true when you get a touchDown, false when you get a touchUp. I use this method in my top-down shooter and it works very well.
you can check your input simply by do this in your render method
gdx.app.log("","touched"+touchdown);
firstly set the input processor..
Gdx.input.setInputProcessor(mystage);
then you can add input listener to your actor in the create method
optone.addListener(new InputListener() {
#Override
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
boolean touchdown=true;
//do your stuff
//it will work when finger is released..
}
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
boolean touchdown=false;
//do your stuff it will work when u touched your actor
return true;
}
});
I had the similar problem and moreover I needed to know the current coordinates; So I resolved the issue something like this:
At first we extend the standard listener:
class MyClickListener extends ClickListener {
float x, y = 0;
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
this.x = x;
this.y = y;
return super.touchDown(event, x, y, pointer, button);
}
#Override
public void touchDragged(InputEvent event, float x, float y, int pointer) {
this.x = x;
this.y = y;
super.touchDragged(event, x, y, pointer);
}
}
Then add an instance to the Actor:
class MyActor extends Actor {
private final MyClickListener listener = new MyClickListener();
MyActor() {
addListener(listener);
}
...
}
And in a draw (act) method use the following:
if (listener.getPressedButton() >= 0)
System.out.println(listener.x + "; " + listener.y);

Processing input for a label actor

I have a Label actor on a Stage and I can't, for the life of me, get input, i.e. touchDown and touchUp events fired.
In my show() method I have this too:
Gdx.input.setInputProcessor(mainMenuStage);
Initialise the Label and set its position and the bounds, then I add an EventListener to the Label like so:
myLabel.addListener(new InputListener(){
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
super.touchDown(event, x, y, pointer, button);
System.out.println("touchdown");
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
super.touchUp(event, x, y, pointer, button);
System.out.println("touchup");
}
});
Once the Label is all set-up I add it to the Stage
But nothing is printed in the LogCat/Console, when I tap on myLabel.
I am so stupid!! -.-
I was calling Gdx.input.setInputProcessor(mainMenuStage); before I had even intialised my mainMenuStage. Funny that I never got a NullReferenceException though.
Anyway, got it working, finally =]]

Categories

Resources