Libgdx,Java。触摸并打印arrayList图像的索引

时间:2016-06-07 14:35:43

标签: java android arraylist libgdx

我有一个名为Badge的课程(扩展Image)。它是4个元素的Arraylist(卡通树的图像)。我把它们放在舞台上,用touchDown监听器打印出来"触摸"通过Gdx.input.setInputProcessor(stage)。它工作正常。然后我想点击每棵树,然后使用System.out.println("Badge name is: " + event.getRelatedActor().getName());

返回树的索引

该应用程序崩溃了。知道为什么会这样吗?

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;



    // Create a constructor
    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);
        badges = new ArrayList<Badge>();
        int numBadges = 4;
        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);
//
        for (int i = 0; i < numBadges; i ++){
            stage.addActor(badges.get(i));
        }

        Gdx.input.setInputProcessor(stage);

        for (int i = 0; i < numBadges; i++) {
            badges.get(i).addListener(new InputListener() {
                @Override
                public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                   System.out.println("Badge name is: " + ((Badge)event.getRelatedActor()).getName());
                    return true;
                }
            });
        }


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

    }


    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1,1,1,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.end();
        stage.act();
        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() {

    }
}

徽章课程是:

public class Badge extends Image {

    Vector2 position, size;
    Texture badge;
    Rectangle bounds;

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

0 个答案:

没有答案