简体   繁体   中英

libgdx - How can I create Circle class objects using for loop?

I'm creating board game and I need to draw circles for players (2-5 players). I can draw them using ShapeRenderer but then I don't have control over circles (change position, radius, etc.). So I need to create 2-5 Circle class object using for loop (I want to do it with for loop). How can I do it?

Thanks!

You can create a circle class and iterate through them with a for-loop. For example:

public class MyCirlce{

    private float radius;
    private Vector2 position;

    public MyCircle(float xPos, float yPos, float radius){
         position = new Vector2(xPos, yPos);
         this.radius = radius;   
    }

    public void translate(float xAmount, float yAmount){
         position.x += xAmount;
         position.y += yAmount;
    }

    public void changeSizeBy(float changeAmount){
         radius += changeAmount;
    }

    public void render(ShapeRenderer render){
            render.circle(position.x, position.y, radius);
    }


}

This will allow you to dynamically change the position of named circles and the size. Also, if you didn't mind using built in things, you can go to their wiki and see that they have a Circle object similar to this with extra functionality like a 'Overlaps' method.

您可以使用ShapeRenderer绘制圆形白色,可以绘制另一个对象而不是移动它们,例如,我使蛇形白色变得比您可以移动它们

Is your Circle is required in view only or it required in model (eg. Collision detection between circles) too.

If view only then take a circular .png image. Create Sprite or Image object and use that otherwise you can draw Circle using ShapeRenderer.

you can change position using ShapeRender object Use this Actor with scene2d

https://github.com/itsabhiaryan/gdx-utils/blob/master/gdx-utils/src/com/ng/gdxutils/actor/ShapeRendererActor.java

EDIT

public static Pixmap getPixmapCircle(int radius, Color color, boolean isFilled) {
        Pixmap pixmap=new Pixmap(2*radius+1, 2*radius+1, Pixmap.Format.RGBA8888);
        pixmap.setColor(color);
        if(isFilled)
            pixmap.fillCircle(radius, radius, radius);
        else
            pixmap.drawCircle(radius, radius, radius);
        pixmap.drawLine(radius, radius, 2*radius, radius);
        Pixmap.setFilter(Pixmap.Filter.NearestNeighbour);
        return pixmap;
}

Texture texture=new Texture(getPixmapCircle(10, Color.RED, true));

Image image=new Image(texture);
or
Sprite sprite=new Sprite(texture);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM