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) {
}
});
Related
I am developing an app and i have followed this tutorial to add movement
but whenever i slide my finger to a new button it is still recognizing the button press for the button i firt pressed resulting in having to lift my finger to change buttons.
I would like to find a way to allow sliding to a new button
the only way i though of doing this is by creating a rectangle whenever the player presses the screen and if it overlaps the button which is of the Image class in libgdx and if it overlaps move in that direction and if the player moves their finger move the rectangle with it.
is there a better way to do this and if not how would i detect if it overlaps the Image.
What Arctic45 said works so in my class that handles touch controls i have for each button
leftImg.addListener(new InputListener() {
#Override
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
player.getComponent(MovementComponent.class).setGoLeft(true);
super.enter(event, x, y, pointer, fromActor);
}
#Override
public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
player.getComponent(MovementComponent.class).setGoLeft(false);
super.exit(event, x, y, pointer, toActor);
}
});
and when i run it and press a button then slide it to another button it changes what button is pressed without needed a touch up event.
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");
}
});
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.
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);
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 =]]