0

我是 Libgdx 的新手。我有一个要求,我在一个游戏中有多个球,并且我想在每次用户触摸球时增加球的直径。我使用方法 pixmap.drawCircle() 来创建球,每次用户触摸球时,我都会调用方法 increaseBallDia() 来增加球的大小。由于没有增加现有像素图的高度和宽度的方法,因此在 increaseBallDia() 方法中,我创建了一个增加了宽度和高度的新像素图,然后绘制了一个直径增加的球。

问题是我的 increaseBallDia() 工作不正常,并且像素图的高度和宽度没有增量。因此,随着球直径的增加,它会覆盖整个像素图区域,并且看起来像矩形而不是圆形。

任何有关如何解决此问题的指示将不胜感激。

以下是相关的代码片段:

public class MyActor extends Actor {
int actorBallDia = 90;
Pixmap pixmap = new Pixmap(actorBallDia, actorBallDia,Pixmap.Format.RGBA8888);
float actorX, actorY;
Texture texture;

public void increaseBallDia() {
actorBallDia = actorBallDia + 5;
int radius = actorBallDia / 2 - 1;
Pixmap pixmap = new Pixmap(actorBallDia, actorBallDia,
Pixmap.Format.RGBA8888);
pixmap.drawCircle(pixmap.getWidth() / 2 , pixmap.getHeight() / 2,
radius);
pixmap.fillCircle(pixmap.getWidth() / 2 , pixmap.getHeight() / 2,
radius);
texture = new Texture(pixmap);
}
public void createYellowBall() {
pixmap.setColor(Color.YELLOW);
int radius = actorBallDia / 2 - 1;
pixmap.drawCircle(pixmap.getWidth() / 2, pixmap.getHeight() / 2,
radius);
pixmap.setColor(Color.YELLOW);
pixmap.fillCircle(pixmap.getWidth() / 2, pixmap.getHeight() / 2,
radius);
texture = new Texture(pixmap);
}


@Override
public void draw(Batch batch, float alpha) {
if (texture != null) {
batch.draw(texture, actorX, actorY);
}
}

public void addTouchListener() {
addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
MyActor touchedActor = (MyActor) event.getTarget();
touchedActor.actorTouched = true;
return true;
}

@Override
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
MyActor touchedActor = (MyActor) event.getTarget();
touchedActor.actorTouched = false;
}
});
}

@Override
public void act(float delta) {
if (actorTouched) {
increaseBallDia();
}
}

问候, RG

4

1 回答 1

0

You should create the pixmap with its biggest possible dimensions. When the ball is meant to be small, just scale it down as you wish.

于 2014-11-12T07:01:41.173 回答