我有一个按钮可以在屏幕上随机生成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中。请帮忙。
答案 0 :(得分:0)
您正在使用 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);
答案 1 :(得分:0)
还记得previous answer吗? :-)这个想法完全一样,但不是这个:
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())
}
});