Processing input for a label actor - java

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 =]]

Related

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);

LibGDX variable accessed from within inner class

I want to make a method(addButton) that would all what's now done by the constructor, but taking some variables. Now i'm stuck cause there is an error which says that I need to make the boolean final, but I don't want to do that. How should I go about this?
Here is the code:
public void addButton(Table table,String key,boolean bool){
atlas=new TextureAtlas(Gdx.files.internal("buttons/buttons.pack"));
skin=new Skin(atlas);
buttonStyle.up=skin.getDrawable(key+".up");
buttonStyle.down=skin.getDrawable(key+".down");
button=new Button(buttonStyle);
button.addListener(new ClickListener(){
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
bool=true; //this boolean
return super.touchDown(event, x, y, pointer, button);
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
bool=false; //and this
super.touchUp(event, x, y, pointer, button);
}
});
table1.add(button);
}
There are many ways to do it. If you just want to know if the button is pressed or not, you can use Button.isPressed(), because Button already keeps track of that.
If you want to do something else, you would be better off creating your own MyButton which extends Button. MyButton could have a field private boolean bool and add that ClickListener in the constructor. This click listener will then be able to access the field of the button via MyButton.this.bool and can change it.
public class MyButton extends Button {
private boolean bool;
public MyButton(...) {
addListener(new ClickListener(){
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
MyButton.this.bool=true;
return super.touchDown(event, x, y, pointer, button);
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
MyButton.this.bool=false;
super.touchUp(event, x, y, pointer, button);
}
});
}
}
Another solution would be to keep your current setup, but wrap the primitive value in another class:
public class DataWrapper {
public boolean bool;
}
public void addButton(Table table,String key, final DataWrapper data) {
...
button=new Button(buttonStyle);
button.addListener(new ClickListener(){
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
data.bool=true;
return super.touchDown(event, x, y, pointer, button);
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
data.bool=false;
super.touchUp(event, x, y, pointer, button);
}
});
table1.add(button);
}

LibGDX updating a label while a mouse button is being clicked

I have a float value that is set from 0 to 100, and I want to register a long click and update a Label progressivly, so that like every 0.25seconds the label's text will be updated. For now I am using a custom made Label that has a method to update the text displayed.
I tried using touchDown like this:
while (Gdx.input.isButtonPressed(Buttons.LEFT)) {
double currentValue = Double.parseDouble(Button.getText()
.toString());
if ((int) currentValue != 0)
currentValue--;
Button.updateText(Double.toString(currentValue));
}
but that doesn't seem to work, how do I update a label while a button is clicked?
Edit: Most of the code for the ClickListener:
#Override
public void enter(InputEvent event, float x, float y, int pointer,
Actor fromActor) {
super.enter(event, x, y, pointer, fromActor);
TextButtonStyle style = ButtonLower.getStyle();
style.font = red;
ButtonLower.setStyle(style);
}
#Override
public void exit(InputEvent event, float x, float y, int pointer,
Actor toActor) {
super.exit(event, x, y, pointer, toActor);
TextButtonStyle style = ButtonLower.getStyle();
style.font = white;
ButtonLower.setStyle(style);
}
#Override
public void clicked(InputEvent event, float x, float y) {
super.clicked(event, x, y);
System.out.println(event.toString());
double currentValue = Double.parseDouble(Button.getText()
.toString());
if ((int) currentValue != 0)
currentValue--;
Button.updateText(Double.toString(currentValue));
}
#Override
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
if (Gdx.input.isButtonPressed(Buttons.LEFT)) {
double currentValue = Double.parseDouble(Button.getText()
.toString());
if ((int) currentValue != 0)
currentValue--;
Button.updateText(Double.toString(currentValue));
}
return super.touchDown(event, x, y, pointer, button);
}
without knowing which of your classes implements your InputListener, it's a little hard to tell you exactly what to do to fix your problem. However, in the general sense what you probably want to do is have a boolean flag set to true on your touchDown method, set to false on your touchUp method, and then in some method that gets called every frame (act, draw, etc., depending on your implementation), youll want to update your text.
The code would look something like
private boolean flag = false;
#Override
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
if (button == Buttons.LEFT) {
flag = true;
}
//other stuff
return true; //or whatever you want
}
#Override
public boolean touchUp(InputEvent event, float x, float y,
int pointer, int button) {
if (button == Buttons.LEFT) {
flag = false;
}
//other stuff
return false; //or whatever you want
}
#Override
public void act(float delta) {
if (flag) {
double currentValue = Double.parseDouble(Button.getText()
.toString());
if ((int) currentValue != 0)
currentValue--;
Button.updateText(Double.toString(currentValue));
}
//other stuff
}
depending on your targetted audience, you may also have to deal with pointer id's in some way (this code example wont work if the user clicks on the label with two fingers, then releases one but not the other)

Categories

Resources