2017-11-20 11 views
0

InputListenerに問題があります。 私はTextButtonを拡張して、ボーダーとデフォルトのInputListenerを持つTextButtonを作成するクラスを作成しています。そして、私のメインコードでは、もう1つのInputListenerを追加して、それが押されたときに新しいスクリーンを設定したいと思っています。 :複数のInputListenerをActorに追加するlibgdx

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を追加する方法ですか?

答えて

0

両方のInputListenersがtouchUpイベントを処理するためにtrueを返します。

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

コード:すでに組み込まれている頑強に異なるボタンを処理し、状態をホバーInputListeners LibGDXで

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

回答ありがとうございますが、リスナーのすべてのthouchDownとtouchUpを実行したいと思います。私がfalseを返した場合、touchUpは実行されません。 – Gaetan

+0

InputListenersの仕組みを間違えてしまったので、私はいくつかのテストコードで答えを編集しました。 – Tejay

+0

ありがとう、私はこれをテストし、それは動作します!ありがとう! – Gaetan

0

ボタンを一度クリックしたときにこれが私の出力です。独自のInputListenerを追加する代わりに、ボタンの押下に応答するためにChangeListenerを追加する必要があります。

関連する問題