Libgdx Actor doesn't respond to input - java

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

Related

Libgdx Touch image interaction

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

Libgdx - Action on actor

First, thank you for reading this post, any help is welcome.
My actor is correctly rendered on the stage, but with the Actions.moveTo, it leaves a trail ? I just don't get it. It's like the texture is rendered at a new position on every new frame.
Here is my code for my class :
public class SlidingCap extends Actor {
private Texture capOver;
private float xPosition;
private float yPosition;
public SlidingCap(float x, float y) {
this.xPosition = x;
this.yPosition = y;
this.capOver = new Texture(Gdx.files.internal("images/cappingPlate.png"));
setBounds(x, y, 288, 180);
}
#Override
public void act(float delta) {
super.act(delta);
}
#Override
public void draw(Batch batch, float parentAlpha) {
batch.draw(capOver, getX(), getY(), 288, 180);
this.addAction(Actions.moveTo(xPosition+10, yPosition+10, 5f));
}
}
And the ScreenGame render method:
#Override
public void render(float delta) {
gameStage.act(delta);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
this.capMiniGame = new SlidingCap(100, 100);
this.gameStage.addActor(capMiniGame);
gameStage.draw();
}
You're adding a new SlidingCap every frame with this code
this.capMiniGame = new SlidingCap(100, 100);
this.gameStage.addActor(capMiniGame);
Try adding the SlidingCap once in the Create or show method instead of every frame

Scene2d: InputListener does not call touchUp

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

how to check if background is touched and not the Actor in libgdx?

i have random boxes coming on the screen over and over, when the box is touch it switches positions. I am not sure how to check if the screen is touched and not the box. anywhere but the box.
private Stage stage;
private Texture boxImage;
private Image pop;
#Override
public void show() {
stage = new Stage();
Gdx.input.setInputProcessor(stage);
boxImage = new Texture(Gdx.files.internal("box.png"));
pop = new Image(boxImage);
pop.setPosition(20,20);
pop.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y)
{
int num1 = (int)MathUtils.random(50,500);
int num2 = (int)MathUtils.random(50,500);
pop.setPosition(num1,num2);
}
});
stage.addActor(pop);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0 , 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
How about just adding a ClickListener to your stage?
stage.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
//check if box is hit
if (pop.equals(stage.hit(x, y, false))) {
//box hit
} else {
//box was not hit
}
}
});

LIBGDX background for main menu is showing up white

My background image is not showing up, it shows up as a white square in the corner like this.
https://dl.dropboxusercontent.com/u/45938379/menu.png
I need to know how to fix this, my actors are showing behind it as you can see.
Here is my code
public class MainMenu implements Screen {
CrazyZombies game;
Stage stage;
TextureAtlas atlas;
Skin skin;
SpriteBatch batch;
Button play, option, quit, custom, store;
TextureRegion backGround;
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);
batch.begin();
stage.draw();
drawBackGround();
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.setX(Gdx.graphics.getWidth() / 2 - quit.getWidth() / 2);
quit.setY(Gdx.graphics.getHeight() / 2 - quit.getHeight() / 2);
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.setX(Gdx.graphics.getWidth() / 2 - store.getWidth() / 2);
store.setY(Gdx.graphics.getHeight() / 2 - store.getHeight() / 2);
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.setX(Gdx.graphics.getWidth() / 2 - custom.getWidth() / 2);
custom.setY(Gdx.graphics.getHeight() / 2 - custom.getHeight() / 2);
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);
custom.setX(Gdx.graphics.getWidth() / 2 - custom.getWidth() / 2);
custom.setY(Gdx.graphics.getHeight() / 2 - custom.getHeight() / 2);
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.setX(Gdx.graphics.getWidth() / 2 - play.getWidth() / 2);
play.setY(Gdx.graphics.getHeight() / 2 - play.getHeight() / 2);
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) {
game.setScreen(new GameScreen(game));
}
});
/**
* End Play Button
*/
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);
backGround = atlas.findRegion("background");
backGround.getRegionHeight();
backGround.getRegionWidth();
}
public void drawBackGround() {
float w = 854;
float h = 480;
float y = 0;
float x = 0;
batch.draw(backGround, x, y, w, h);
}
#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();
}
}
What i have also noticed is that if i get rid of
stage.draw();
The image shows up.
Take out your stage.draw() out of the batch.begin() and batch.end(). The stage does have it's own Spritebatchso you do have concurenting ones at the moment. I think that does cause the troubles. So best way would be this:
#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();
drawBackGround();
batch.end();
}
I would recommend that you put your background inside the stage. Image is also an Actor so you can add it to your stage and than call the .toBack() (Back) to have it in the background.
Libgdx Image

Categories