将多个InputListener添加到Actor libgdx

时间:2017-11-20 20:49:51

标签: java android libgdx

我的InputListener有问题。 我创建了一个扩展TextButton的类来创建带边框的TextButtons和一个默认的Inputlistener。在我的主要代码中,我想再添加一个Inputlistener来设置一个新的屏幕(在我的代码中它只是一个打印,如果有效),但只有touchDown工作...

My TextButtonWithBorder类:

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

}

我的主要代码:

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

}
}

我的问题是如何将多个InputListener添加到Actor?

2 个答案:

答案 0 :(得分:0)

为两个InputListeners返回true以处理touchUp事件。单击一次时,这是我的输出:

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

代码:

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

答案 1 :(得分:0)

LibGDX中的按钮已经内置了InputListener,可以稳健地处理不同的按钮和悬停状态。您应该添加ChangeListener来响应按下按钮,而不是添加自己的InputListener。