I am new to Libgdx. I have a requirement where I have multiple balls in a game and I want to increase the ball diameter every time the user touches the ball. I used the method pixmap.drawCircle() to create the ball and every time a user touches the ball, I call a method increaseBallDia() to increase the ball size. Since there is no method to increase the height and width of an existing Pixmap, in increaseBallDia() method, I create a new Pixmap with increased width and height and then draw a ball with incremented diameter.
The problem is that my increaseBallDia() is not working properly and there is no increment to the height and width of the Pixmap. Due to this as the ball diameter increases it covers the entire Pixmap area and appears like a rectangle instead of a circle.
Any pointers on how to solve this would be greatly appreciated.
Following is the relevant code snippet:
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();
}
}
Regards, RG
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.