2017-11-20 2 views
0

InputListener에 문제가 있습니다. 테두리 및 하나의 기본 InputListener가있는 TextButton을 만들기 위해 TextButton을 확장하는 클래스를 만들었습니다. 그리고 내 주 코드에서 그것을 눌렀을 때 새 화면을 설정하려면 InputListener를 한 개 더 추가하고 싶습니다 (내 코드에서는 작동하는 경우 인쇄하기 만하면됩니다). 그러나 touchDown 만 작동합니다 ...Actor에 다중 InputListener 추가 libgdx

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를 추가하는 방법은?

답변

0

두 InputListener가 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를 추가하는 대신 Button 프레스에 응답하기 위해 ChangeListener를 추가해야합니다.