gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:41 25 4
gpt4 key购买 nike

我有一个按钮可以在屏幕上随机生成 4 个 TreeMap 像(我称之为徽章)。现在我想让这些徽章变得可触摸。如果我触摸它,它就会打印出一条消息。目前代码如下所示。

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 部分(就像按钮一样)。我不知道如何修改 Badge 类并以不同的方式构造它,以便将其添加到舞台中。请帮忙。

最佳答案

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

Image 类本身继承自 Actor您可以将其添加到舞台,也可以添加监听器,这正是您想要做的。

如果您只想处理单击图像上的 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);

关于java - libgdx,如何使我的图像可触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37679492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com