繁体   English   中英

libgdx,如何使我的图像可触摸

[英]libgdx, How to make my image touchable

我有一个按钮,可以在屏幕上随机生成4张树图像(我称之为徽章)。 现在我想让这些徽章可以触摸。 如果我触摸它,它只是打印出一条消息。 目前代码看起来像这样。

public class PlayScreen implements Screen {
    int scWidth, scHeight;
    int playerWidth, playerHeight;

    Stage stage;
    Skin skin;
    BitmapFont font;
    private SpriteBatch batch; // This is in the render.
    ArrayList<Badge> badges;
    Iterator<Badge> badgeIterator;
    int badgeWidth = 160;
    int badgeHeight = 300;


    Game game;
    public PlayScreen(Game game){
        this.game = game;
    }


    // Create a button to randomise tree positions
    TextureAtlas buttonAtlas;
    TextButton.TextButtonStyle buttonStyle;
    TextButton playButton;

    int randomBadgePosition(String choice)
    {
        int min, max, range;
        if (choice == "x") {
            min = scWidth / 2 - scHeight / 2;
            max = scWidth / 2 + scHeight / 2 - badgeWidth;
            range = (max - min) + 1;
            return (int) (Math.random() * range) + min;
        } else{
            min = 0;
            max = scHeight - badgeHeight;
            range = (max - min) + 1;
            return (int)(Math.random() * range) + min;
        }
    }


    @Override
    public void show() {

        batch = new SpriteBatch();
        stage = new Stage();
        scWidth = Gdx.graphics.getWidth();
        scHeight = Gdx.graphics.getHeight();
        font = new BitmapFont();
        font.setColor(Color.BLACK);
        font.getData().setScale(5);

        int numBadges = 4;
        badges = new ArrayList<Badge>();

        for (int i = 0; i < numBadges; i ++){
            badges.add(new Badge(new Vector2(-400, -400),new Vector2(badgeWidth, badgeHeight) ));
        }

        skin = new Skin();
        buttonAtlas = new TextureAtlas("buttons/button1.txt");
        skin.addRegions(buttonAtlas); // You need to do that.
        buttonStyle = new TextButton.TextButtonStyle();
        buttonStyle.up = skin.getDrawable("button");
        buttonStyle.over = skin.getDrawable("buttonpressed"); // over is not necessary for android.
        buttonStyle.down = skin.getDrawable("buttonpressed");
        buttonStyle.font = font;
        playButton = new TextButton("play", buttonStyle);
        playButton.setPosition(50, 20);
        stage.addActor(playButton);


        Gdx.input.setInputProcessor(stage);

        // Input listener for buttons.
        playButton.addListener(new InputListener(){
            @Override
            public boolean touchDown (InputEvent event, float x, float y,
                                      int pointer, int button){
                // Randomise the locations of the badges.
                for (int i = 0; i < badges.size(); i++){
                    badges.get(i).setPosition(new Vector2(randomBadgePosition("x"), randomBadgePosition("y")));
                }
                return true;
            }
        });
    }

    @Override
    public void render(float delta) {

        Gdx.gl.glClearColor(1,1,1,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.act();
        stage.draw();
        batch.begin();

        badgeIterator = badges.iterator();
        // This is how to change the position of the badge.
        badges.get(1).setPosition(new Vector2(scWidth/2, scHeight/2));
        while(badgeIterator.hasNext()){
            Badge cur = badgeIterator.next();
            cur.draw(batch);
        }

        batch.end();
        player.update(); // Update the bound
    }
}

徽章课程是:

public class Badge {

    Vector2 position, size;
    Texture badge;
    Rectangle bounds;

    public Badge(Vector2 position, Vector2 size){
        this.position = position;
        this.size = size;
        bounds = new Rectangle(position.x, position.y, size.x, size.y);
        badge = new Texture(Gdx.files.internal("tree.png"));
    }

    public void update(){
        bounds.set(position.x, position.y, size.x, size.y);
    }

    public void draw(SpriteBatch batch){
        batch.draw(badge, position.x, position.y, size.x, size.y);
    }

    public Vector2 getPosition() {
        return position;
    }

    public void setPosition(Vector2 position) {
        this.position = position;
    }

    public Vector2 getSize() {
        return size;
    }

    public void setSize(Vector2 size) {
        this.size = size;
    }

    public Rectangle getBounds() {
        return bounds;
    }

    public void setBounds(Rectangle bounds) {
        this.bounds = bounds;
    }
}

我想我需要把它们放在舞台然后addlistener。 虽然我知道addlistener部分(就像按钮一样)。 我不知道如何以不同的方式修改Badge类和构造函数,以便可以将它添加到stage中。 请帮忙。

您正在使用Scene2D,因此您可以使用Image类而不是在Badge类中创建自己的实现。 当然,如果您需要Image无法提供的某些功能,您可以扩展此功能。

Image类本身继承自Actor ,您可以将其添加到stage中,并且可以向侦听器添加您想要执行的操作。

如果您只想处理单击图像, ClickListener将是最好的 - 如果您需要处理更多事件,您可以添加EventListener

示例代码可能看起来像

    Stage stage;

    //...creating stage etc...

    Image badge = new Image(new Texture(Gdx.files.internal("tree.png"))); //Remember that you need to dispose Texture like this!
    badge.addListener( new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            System.out.println("Clicked!");
        }
    });

    stage.addActor(badge);

另请注意,您需要使用将stage设置为输入处理器

    Gdx.input.setInputProcessor(stage);

还记得以前的答案吗? :-)这个想法是完全一样的,但不是这个:

addListener(new DragListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            @Override
            public void touchDragged(InputEvent event, float x, float y, int pointer) {
                moveBy(x - getWidth()/2, y - getHeight()/2);
            }
        });

你添加这个:

addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
               System.out.println("Badge name is: " + ((Badge)event.getRelatedActor()).getName())
            }
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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