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を追加する方法ですか?
回答ありがとうございますが、リスナーのすべてのthouchDownとtouchUpを実行したいと思います。私がfalseを返した場合、touchUpは実行されません。 – Gaetan
InputListenersの仕組みを間違えてしまったので、私はいくつかのテストコードで答えを編集しました。 – Tejay
ありがとう、私はこれをテストし、それは動作します!ありがとう! – Gaetan