I have tried using an InputListener on an Actor but it seems that the touchUp does not get called. Every other method does work for me.
stage = new Stage(new ScreenViewport());
Widget actor = new Widget();
actor.setFillParent(true);
actor.addListener(new InputListener() {
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.log("", "");
}
});
stage.addActor(actor);
Gdx.input.setInputProcessor(stage);
stage = new Stage(new ScreenViewport());
Widget actor = new Widget();
actor.setFillParent(true);
actor.addListener(new InputListener() {
#Override
public void touchDown(InputEvent event, float x, float y, int pointer, int button) {
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.log("", "");
}
});
stage.addActor(actor);
Gdx.input.setInputProcessor(stage);
Related
Is there a way for an image to move to a different position when the user clicks on that image.
public Players() {
deck.displayHand();
stage = new Stage(new ScreenViewport());
Image card1 = new Image(sprite);
card1.addListener(new InputListener(){
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
}
});
}
#Override
public void draw(Batch batch, float parentAlpha) {
stage.act();
stage.draw();
}
I know I have to add a listener but I'm quite confuse what to do after. The objective is for the user to touch the card image and it would move to a different location.
Move to a predefined position when you/user click on that image :
Add an Action to that image(Actor child),
Let's assume predefined position is x_pos,y_pos and movement time is t in sec
card1.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
card1.addAction(Actions.moveTo(x_pos, y_pos,t));
}
});
Objective is to touch the card/image and it would move to a different location according to the user touch :
card1.addListener(new DragListener(){
#Override
public void drag(InputEvent event, float x, float y, int pointer) {
card1.moveBy(x - card1.getWidth() / 2, y - card1.getHeight() / 2);
super.drag(event, x, y, pointer);
}
});
You need to set your stage as InputProcessor
Gdx.input.setInputProcessor(stage);
Image is an Actor so you can add Actions to it.
card1.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
card1.addAction(Actions.moveTo(100, 100, .5f, Interpolation.circleIn));
}
});
I have a problem with Table attributes on Libgdx my code:
Table root = new Table();
root.setFillParent(true);
stage.addActor(root);
Table table = new Table(MyGdxGame.gameSkin);
table.setBackground(new NinePatchDrawable(getNinePatch(("background.jpg"))));
root.add(table).grow().pad(25.0f);
Label label = new Label("Marsel\nTale", MyGdxGame.gameSkin, "title");
label.setColor(Color.WHITE);
label.setScale(.9f,.9f);
table.add(label);
table.row();
TextButton playButton = new TextButton("Tournament",MyGdxGame.gameSkin);
playButton.addListener(new InputListener(){
#Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
game.setScreen(new ChampionScreen(game));
}
#Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}
});
table.add(playButton);
TextButton optionsButton = new TextButton("Options",MyGdxGame.gameSkin);
optionsButton.addListener(new InputListener(){
#Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
game.setScreen(new OptionScreen(game));
}
#Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}
});
table.add(optionsButton).padBottom(3.0f);
I'm not able to modify the structure of my table, I want to put playButton and optionButton in the bottom.
There should be a method called bottom() that you can use on the buttons like:
table.add(playButton).bottom();
More information can be found in this helpful article: https://github.com/libgdx/libgdx/wiki/Table
public void FillTableValueGrid(int[] Arr)
{
grp = new Group();
grp.setHeight(AssetsHelper.convertHeight(4*187));
grp.setWidth(AssetsHelper.convertWidth(124));
grp.setPosition(680, AssetsHelper.convertHeight(180));
for(int i=0;i<Arr.length;i++)
{
ActualPieceArray[i]=ImagesPieceArray2[set1Array[i]];
ImagesAnswerArray[Arr[i]].setVisible(false);
ActualIntArray[i]=Arr[i];
}
ActualPieceArray[0].setPosition(AssetsHelper.convertWidth(0), AssetsHelper.convertHeight(100));
grp.addActor(ActualPieceArray[0]);
for(int i=1;i<9;i++)
{
ActualPieceArray[i].setPosition(AssetsHelper.convertWidth(0), ActualPieceArray[i-1].getY()- ActualPieceArray[i-1].getHeight());
grp.addActor(ActualPieceArray[i]);
//System.out.println("trace herer :::"+ grp.getY());
}
stage.addActor(grp);
Mask=new Image(AssetsHelper.Mask);
Mask.setPosition(AssetsHelper.convertWidth(656/2),AssetsHelper.convertHeight((552)/2));
Mask1=new Image(AssetsHelper.Mask1);
Mask1.setPosition(AssetsHelper.convertWidth(656/2),0);
stage.addActor(Mask);
stage.addActor(Mask1);
ArrowUper = new Image(AssetsHelper.ArrowUp);
ArrowUper.setPosition(AssetsHelper.convertWidth(368),AssetsHelper.convertHeight(280));
stage.addActor(ArrowUper);
//ImageCliked(ArrowUper);
ArrowDowner = new Image(AssetsHelper.ArrowDown);
ArrowDowner.setPosition(AssetsHelper.convertWidth(368),AssetsHelper.convertHeight(14));
stage.addActor(ArrowDowner);
ImageCliked1(ArrowDowner);
ImageCliked(ArrowUper);
}
public void ImageCliked(Actor actor)
{
actor.addListener(new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
grp.addAction(Actions.sequence(
Actions.fadeOut(0),
Actions.parallel(
Actions.fadeIn(1.0f),
Actions.moveTo(grp.getX(), grp.getY()+AssetsHelper.convertHeight(100/2)))));
Counter--;
EnableAndDisable(Counter);
System.out.println("Please ImageClikedS Counter grp.getY() :::"+Counter+ grp.getY());
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
// return false;
}
});
}
Added a brief code,where FillTableValueGrid is used to add the images and imageclicked used for moving up/down.I am new to java libgdx;i want rotating effect of that group,when i clicked down button after last image;first image should come.Can anyone help to resolve .
i have 5 buttons i want to put on the main menu but when i have all 5 actors it doesn't work and when i take them all out and leave one in it works.
How do get around having multiple buttons?
Here is my code now
public class MainMenu implements Screen {
CrazyZombies game;
Stage stage;
TextureAtlas atlas;
Skin skin;
SpriteBatch batch;
Button play, option, quit, custom, store, menu;
public MainMenu(CrazyZombies game) {
this.game = game;
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0.09f, 0.28f, 0.2f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
batch.begin();
batch.end();
}
#Override
public void resize(int width, int height) {
if (stage == null)
stage = new Stage(width, height, true);
stage.clear();
Gdx.input.setInputProcessor(stage);
/**
* quit Button
*/
TextButtonStyle styleQuit = new TextButtonStyle();
styleQuit.up = skin.getDrawable("8layer");
styleQuit.down = skin.getDrawable("8layer");
quit = new Button(styleQuit);
quit.setWidth(854);
quit.setHeight(480);
quit.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
return true;
}
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
}
});
/**
* End quit Button
*/
/**
* store Button
*/
TextButtonStyle styleStore = new TextButtonStyle();
styleStore.up = skin.getDrawable("9layer");
styleStore.down = skin.getDrawable("9layer");
store = new Button(styleStore);
store.setWidth(854);
store.setHeight(480);
store.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
return true;
}
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
game.setScreen(new StoreScreen(game));
}
});
/**
* End store Button
*/
/**
* customs Button
*/
TextButtonStyle styleCustom = new TextButtonStyle();
styleCustom.up = skin.getDrawable("10layer");
styleCustom.down = skin.getDrawable("10layer");
custom = new Button(styleCustom);
custom.setWidth(854);
custom.setHeight(480);
custom.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
return true;
}
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
game.setScreen(new CustomScreen(game));
}
});
/**
* End customs Button
*/
/**
* Options Button
*/
TextButtonStyle styleOptions = new TextButtonStyle();
styleOptions.up = skin.getDrawable("11layer");
styleOptions.down = skin.getDrawable("11layer");
option = new Button(styleOptions);
option.setWidth(854);
option.setHeight(480);
option.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
return true;
}
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
game.setScreen(new OptionScreen(game));
}
});
/**
* End Options Button
*/
/**
* Play Button
*/
TextButtonStyle stylePlay = new TextButtonStyle();
stylePlay.up = skin.getDrawable("7layer");
stylePlay.down = skin.getDrawable("7layer");
play = new Button(stylePlay);
play.setWidth(854);
play.setHeight(480);
play.addListener(new InputListener(){
});
play.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
return true;
}
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
Gdx.app.log(CrazyZombies.LOG, "un-touched");
game.setScreen(new GameScreen(game));
}
});
/**
* End Play Button
*/
/**
* start Background
*/
TextButtonStyle styleMenu = new TextButtonStyle();
styleMenu.up = skin.getDrawable("background");
menu = new Button(styleMenu);
menu.setWidth(854);
menu.setHeight(480);
/**
* End Background
*/
//stage.addActor(menu);
stage.addActor(play);
//stage.addActor(option);
//stage.addActor(store);
//stage.addActor(custom);
//stage.addActor(quit);
}
#Override
public void show() {
Audio.playMusic(true);
batch = new SpriteBatch();
atlas = new TextureAtlas("data/mainmenu/mainmenu.pack");
skin = new Skin();
skin.addRegions(atlas);
}
#Override
public void hide() {
dispose();
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
batch.dispose();
skin.dispose();
atlas.dispose();
stage.dispose();
Audio.dispose();
}
}
Create a Table object.
Add all buttons into it.
Add that Table object to the stage.
Hope it helps. Good luck.
I defined new actor this way:
class MyActor extends Image
{
Texture texMyActor;
public MyActor()
{
super.setTouchable(Touchable.enabled);
texMyActor = new Texture("data/myActor.png");
addListener(new InputListener()
{
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button)
{
System.out.println("down");
return true;
}
public void touchUp(InputEvent event, float x, float y,
int pointer, int button)
{
System.out.println("up");
}
});
}
#Override
public void act(float delta)
{
super.act(delta);
}
#Override
public void draw(SpriteBatch batch, float parentAlpha)
{
batch.draw(texMyActor, 200, 200);
}
}
I also added actor to stage, registered stage as current input processor.
Then I call stage.act(deltaTime) and stage.draw().
I see my actor but it doesn't respont to input
What's wrong with my code? :?:
You have to setBounds of your actor as well
add to your constructor :
texMyActor = new Texture("data/myActor.png");
//setting width and height according to the texture
setWidth(texMyActor.getWidth());
setHeight(texMyActor.getHeight());
//setting bound of the actor
setBounds(200, 200, getWidth(), getHeight());
notice i set the bounds position to 200 , 200 since you draw there