battleStage.addActor(aPlayer.handCard1);
aPlayer.handCard1.setPosition(300, -256);
aPlayer.handCard1.addAction(Actions.sequence(Actions.delay(0),
Actions.moveTo(300, 50, 1)));
aPlayer.handCard1.getChildren().items[1].addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
aPlayer.handCard1.getChildren().items[1].addAction(Actions.hide());
System.out.println("duh");
}
});
This is included in a method that is called in my render method.
It doesn't get Clicked. Nothing comes up in my console. I debugged the application and "items[1]" is actually there as a child. It's an Image and when I run the application the Image does show up.
Can anyone help? Thanks in advance!
edit
handCard1 is a Group with 2 Image children.
edit
I 'm doing battleStage.addActor(aPlayer.handCard1); in show()
and
aPlayer.handCard1.setPosition(300, 50);
aPlayer.handCard1.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
aPlayer.handCard1.getChildren().items[1].addAction(Actions.hide());
System.out.println("duh");
}
});
in render() and it's working.
Then I'm doing
aPlayer.handCard1.setPosition(300, 50);
aPlayer.handCard1.getChildren().items[1].setHeight(256);
aPlayer.handCard1.getChildren().items[1].setWidth(192);
aPlayer.handCard1.getChildren().items[1].addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
aPlayer.handCard1.getChildren().items[1].addAction(Actions.hide());
System.out.println("duh");
}
});
}
in render() and it's not working.
"This is included in a method that is called in my render method."
You should be adding your actors to the stage in the show() method rather than the render() method.
You need to use actor's setBounds() method set the boundaries for the click event to work. Please check
this link
Are you sure you are doing
Gdx.input.setInputProcessor(battleStage);
and
battleStage.act(delta);
Related
I have an actor called arrow that I want to repeat a sequence action to it.
This arrow points to an actor which if is clicked the arrow should fade out.
Here is my code:
Action moving = Actions.sequence(
(Actions.moveTo(arrow.getX(), arrow.getY() - 35, 1)),
(Actions.moveTo(arrow.getX(), arrow.getY(), 1)));
arrow.addAction(moving);
actor.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
arrow.addAction(Actions.fadeOut(1));
}
});
Code works fine but I want to repeat 'moving' action untile actor is clicked.
I read about RepeatAction in this question Cannot loop an action. libGDX but I didn't know how I can apply
You can use RepeatAction in this case, with Actions.forever():
final Action moving = Actions.forever(Actions.sequence(
(Actions.moveTo(arrow.getX(), arrow.getY() - 35, 1)),
(Actions.moveTo(arrow.getX(), arrow.getY(), 1))));
arrow.addAction(moving);
actor.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
// you can remove moving action here
arrow.removeAction(moving);
arrow.addAction(Actions.fadeOut(1f));
}
});
If you want to remove arrow from Stage after fading out, you can use RunnableAction:
arrow.addAction(Actions.sequence(
Actions.fadeOut(1f), Actions.run(new Runnable() {
#Override
public void run() {
arrow.remove();
}
}))
);
I have a play button like this;
private void drawPlayButton() {
playButton = new ImageButton(new TextureRegionDrawable(playTexture), new TextureRegionDrawable(pressTexture));
stage.addActor(playButton);
playButton.setPosition(UIConstants.PLAY_X, UIConstants.PLAY_Y, Align.center);
playButton.addListener(new ActorGestureListener() {
#Override
public void tap(InputEvent event, float x, float y, int count, int button) {
super.tap(event, x, y, count, button);
game.setScreen(new GameScreen(game));
}
});
}
I want to add scale in and out effect to this button using Actions.
I am not that familiar with Actions,I tried something like this;
float duationsec= 0.5f;
playButton.addAction(Actions.sequence(Actions.scaleBy(0.2f,0.2f,duationsec),
Actions.scaleTo(1f, 1f, duationsec)));
img.setOrigin(Align.center);
stage.addActor(playButton);
But this is not giving any effect to the button also same code works for an Image.Is this because button uses drawable texture?
How can I give this effect to a button using Actions?
Also my code is working for one time only.I am calling it in show().If I call it in render(),it works in an abnormal manner.I want this effect to be displaying forever.
Is it possible to achieve this?
Any help would be appreciated.
For performance reason most scene2d.ui groups have transform set to false by default and ImageButton is part of that ui group so you've to enable transform by using setTransform(..) method.
For more detail you can check
https://github.com/libgdx/libgdx/wiki/Scene2d.ui#rotation-and-scale
float duationsec= 0.5f;
playButton.setOrigin(Align.center);
playButton.setTransform(true); // <-- Enable Transform
stage.addActor(playButton);
playButton.addAction(Actions.sequence(Actions.scaleBy(0.2f,0.2f,duationsec),Actions.scaleTo(1f, 1f, duationsec)));
EDIT
If you want to scale up/down on user Click, Do in this way :
playButton.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
playButton.addAction(Actions.sequence(Actions.scaleBy(0.2f,0.2f,duationsec),Actions.scaleTo(1f, 1f, duationsec), Actions.run(new Runnable() {
#Override
public void run() {
game.setScreen(new GameScreen(game));
}
})));
super.clicked(event, x, y);
}
});
I am trying to remove the placeholder-text I put inside my TextField (instructions etc) when a user clicks them, but I am getting the error:
com.badlogic.gdx.scenes.scene2d.ui.TextField is not an enclosing class
My code:
final TextField searchField = new TextField("Who are you looking for?", newSkin);
searchField.setFocusTraversal(false);
searchField.setTextFieldListener(new TextField.TextFieldClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
super.clicked(event, x, y);
searchField.clear();
}
});
I am not sure what I am doing wrong.
The problem is that you are confusing TextField.TextFieldListener and TextField.TextFieldClickListener.
In your code you want to use setTextFieldListener but with the TextFieldClickListener parameter, so compiler cannot resolve it properly.
You may simply use ClickListener instead:
searchField.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
super.clicked(event, x, y);
searchField.setText("");
}
});
Also, note, that you should set the empty string as a text since clear() method is used for:
Removes all actions and listeners on this actor.
and this is definitely not what you are looking for
Is it possible to allow the user to touch an actor only one time.
I've tried using boolean but the problem is I write addListener in the class constructor and I want it to be here not in a separate method
I believe you can remove the listener like this, I have not tested it though and no time for it right now.
final Table t = new Table();
t.addListener(ClickListener listener = new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
super.clicked(event, x, y);
//Do stuff
//...
//remove this listener
t.removeListener(this);
}
});
I want to use scene2D, in libGDX, to detect click on object.
I have this simple Game sample :
Stage stage;
#Override
public void create()
{
this.stage = new Stage(1280, 720, true);
MyActor actor = new MyActor();
Gdx.input.setInputProcessor(stage);
actor.setTexture(new TextureRegion(new Texture(Gdx.files.internal("plateau.jpg"))));
actor.setScale(0.1f);
stage.addActor(actor);
actor.addListener(new InputListener()
{
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
{
System.out.println("down"+Math.random());
return true;
}
});
}
#Override
public void render()
{
stage.act();
stage.draw();
}
MyActor is a simple class extending Actor with a rendertexture.
The Event is working fine at start.
But when resizing the frame, while the Actor is streched as wanted, the coordinates of the click are not updated and thus misplaced.
How to make the stage using the new size of the items as base to the event ?
You need to update the viewport of your stage in resize method of your application like this:
public void resize(int width, int height) {
stage.setViewport(stage.getWidth(), stage.getHeight(), false, 0, 0, width, height);
}
What worked for me was similar to what mentioned above but you have to modify it slightly:
public void resize(int width, int height) {
stage.getViewport().update(width, height);
}
You shouldn't use the Gdx.graphics.width and height but really use what resize() is providing to you.
You have to use camera.project() method for this purpose I think.
camera.project(Vector3 worldPoint);
Does this works for you?