简体   繁体   中英

Add multiple InputListener to Actor libgdx

I have a problem with InputListener. I have create class who extend TextButton to make TextButtons with borders and one default Inputlistener. And in my main code i want to add one more Inputlistener to set a new screen when it is pressed (in my code it's just a print to watch if that works) but only the touchDown works...

My TextButtonWithBorder class :

public class TextButtonWithBorder extends TextButton {
ShapeRenderer shapeRenderer = new ShapeRenderer();
public TextButtonWithBorder(String text, Skin skin) {
    super(text, skin);
    this.setTransform(true);
    this.addReduceClickListener();
}

public TextButtonWithBorder(String text, Skin skin, String styleName) {
    super(text, skin, styleName);
    this.setTransform(true);
    this.addReduceClickListener();
}

public TextButtonWithBorder(String text, TextButtonStyle style) {
    super(text, style);
    this.setTransform(true);
    this.addReduceClickListener();
}

@Override
public void draw(Batch batch, float parentAlpha) {
    batch.end();
    shapeRenderer.setProjectionMatrix(batch.getProjectionMatrix());
    shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
    shapeRenderer.setColor(Color.WHITE);
    shapeRenderer.rect(getX(),getY(),getWidth()*getScaleX(),getHeight()*getScaleY());
    shapeRenderer.end();
    batch.begin();
    super.draw(batch, parentAlpha);
}

public void setCenter(float x,float y)
{
    setPosition(x-getWidth()/2*getScaleX(),y-getHeight()/2*getScaleY());
}
public void setCenter(Vector2 center)
{
    setPosition(center.x-getWidth()/2*getScaleX(),center.y-getHeight()/2*getScaleY());
}
public Vector2 getCenter()
{
    return new Vector2(getX()+getWidth()/2*getScaleX(),getY()+getHeight()/2*getScaleY());
}
public void addReduceClickListener()
{
    addListener((new InputListener(){
        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
            Vector2 center = getCenter();
            setScale(0.9F);
            setCenter(center);
            return true;
        }
        public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
            Vector2 center = getCenter();
            setScale(1F);
            setCenter(center);
        }
    }));
}
public void dispose()
{
    shapeRenderer.dispose();
}

}

And My main code :

public class MainMenuScreen implements Screen {
final PongAndroid game;
Stage stage;
BitmapFont font;
TextButtonWithBorder buttonOnePlayer;
TextButtonWithBorder buttonTwoPlayers;
TextButtonWithBorder buttonAbout;
TextButtonWithBorder buttonExit;
Label title;
ImageButton options;

public MainMenuScreen(final PongAndroid game) {
    this.game=game;
    stage = new Stage(game.viewport,game.batch);
    Gdx.input.setInputProcessor(stage);

    // Styles
    font = new BitmapFont(Gdx.files.internal("MVboli50.fnt"));
    TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
    textButtonStyle.font = font;
    Label.LabelStyle labelStyle = new Label.LabelStyle();
    labelStyle.font = font;
    ImageButton.ImageButtonStyle imageButtonStyle = new ImageButton.ImageButtonStyle();

    // Configure Actors
    // title
    title = new Label("Pong Android",labelStyle);
    title.setFontScale(2f);
    title.setPosition(game.WIDTH/2-title.getWidth()/2*title.getFontScaleX(),game.HEIGHT-title.getHeight()-(game.HEIGHT*0.15f));
    // buttonOnePlayer
    buttonOnePlayer = new TextButtonWithBorder("1 Player",textButtonStyle);
    buttonOnePlayer.setWidth(game.WIDTH*0.70f);
    buttonOnePlayer.setHeight(buttonOnePlayer.getHeight()*1.2f);
    buttonOnePlayer.setPosition(game.WIDTH/2-buttonOnePlayer.getWidth()/2,title.getY()-title.getHeight()/2-buttonOnePlayer.getHeight()-game.HEIGHT*0.05f);
    //buttonTwoPlayer
    buttonTwoPlayers = new TextButtonWithBorder("2 Players",textButtonStyle);
    buttonOnePlayer.setTransform(true);
    buttonTwoPlayers.setWidth(buttonOnePlayer.getWidth());
    buttonTwoPlayers.setHeight(buttonTwoPlayers.getHeight()*1.2f);
    buttonTwoPlayers.setPosition(buttonOnePlayer.getX(),buttonOnePlayer.getY()-buttonOnePlayer.getHeight()-game.HEIGHT*0.05f);
    //buttonAbout
    buttonAbout = new TextButtonWithBorder("About",textButtonStyle);
    buttonOnePlayer.setTransform(true);
    buttonAbout.setWidth(buttonTwoPlayers.getWidth()/2-game.WIDTH*0.05f);
    buttonAbout.setHeight(buttonAbout.getHeight()*1.2f);
    buttonAbout.setPosition(buttonTwoPlayers.getX(),buttonTwoPlayers.getY()-buttonAbout.getHeight()-game.HEIGHT*0.05f);
    //buttonExit
    buttonExit = new TextButtonWithBorder("Exit",textButtonStyle);
    buttonOnePlayer.setTransform(true);
    buttonExit.setWidth(buttonAbout.getWidth());
    buttonExit.setHeight(buttonExit.getHeight()*1.2f);
    buttonExit.setPosition(buttonAbout.getX()+buttonAbout.getWidth()+game.WIDTH*0.1f, buttonAbout.getY());

    // Add listeners to Actors
    buttonExit.addListener(new InputListener(){

        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            System.out.println("down");
            return super.touchDown(event, x, y, pointer, button);
        }

        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            System.out.println("up");
            super.touchUp(event, x, y, pointer, button);
            System.out.println("up");
        }
    });



    // Add Actors to stage
    stage.addActor(title);
    stage.addActor(buttonOnePlayer);
    stage.addActor(buttonTwoPlayers);
    stage.addActor(buttonAbout);
    stage.addActor(buttonExit);
    //stage.addActor();

}


@Override
public void show() {

}

@Override
public void render(float delta) {
    stage.act(delta);
    stage.draw();


}

@Override
public void resize(int width, int height) {

}


@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {
    stage.dispose();
    buttonOnePlayer.dispose();
    buttonTwoPlayers.dispose();
    buttonAbout.dispose();
    buttonExit.dispose();

}
}

My question is how to add multiple InputListener to an Actor ?

Return true for both InputListeners to handle the touchUp event. This is my output when clicking once:

listener A: touchDown listener B: touchDown listener A: touchUp listener B: touchUp

The code:

private SpriteBatch batch;
private Viewport viewport;
private Stage stage;
private Texture texture;
private BitmapFont font;

@Override
public void create() {

    batch = new SpriteBatch();
    viewport = new StretchViewport( Gdx.graphics.getWidth(), Gdx.graphics.getHeight() );
    stage = new Stage( viewport, batch );
    texture = new Texture( "badlogic.jpg" );
    font = new BitmapFont();

    Gdx.input.setInputProcessor( stage );

    TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle(
            new TextureRegionDrawable( new TextureRegion( texture ) ), null, null, font );
    TextButton textButton = new TextButton( "text", textButtonStyle );
    textButton.setPosition(
            0.5f * Gdx.graphics.getWidth() - 0.5f * textButton.getWidth(),
            0.5f * Gdx.graphics.getHeight() - 0.5f * textButton.getHeight()
            );
    textButton.addListener( new InputListener(){

        @Override
        public boolean touchDown( InputEvent e, float x, float y, int pointer, int button ) {

            Gdx.app.log( "listener A", "touchDown" );

            return true;
        }

        @Override
        public void touchUp( InputEvent e, float x, float y, int pointer, int button ) {

            Gdx.app.log( "listener A", "touchUp" );
        }
    } );
        textButton.addListener( new InputListener(){

        @Override
        public boolean touchDown( InputEvent e, float x, float y, int pointer, int button ) {

            Gdx.app.log( "listener B", "touchDown" );

            return true;
        }

        @Override
        public void touchUp( InputEvent e, float x, float y, int pointer, int button ) {

            Gdx.app.log( "listener B", "touchUp" );
        }
    } );

    stage.addActor( textButton );
}

@Override
public void dispose() {

    batch.dispose();
    stage.dispose();
    texture.dispose();
    font.dispose();
}

@Override
public void resize( int width, int height ) {

    viewport.update( width, height, true );
}

@Override
public void render() {

    Gdx.gl.glClearColor( 0, 0, 0, 1 );
    Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );

    stage.act( Gdx.graphics.getDeltaTime() );
    stage.draw();
}

Buttons in LibGDX already have built-in InputListeners that robustly handle different button and hover states. Instead of adding your own InputListener, you should add a ChangeListener to respond to button presses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM