[SOLVED] [libGdx] friendly group example

Hello everyone,

Can any of you please demonstrate a simple example on how to use groups in libGdx ? so if i move the parent, then all the children will move with it and same goes for rotation and scaling.

Thank you

Well, all you would do is create a “Member” Class and have different instances of that (like the Component classes in Swing):

Very rough code:


public class Member{
    private ArrayList<Member> children=new ArrayList<Member>();//only needed if the parent ever needs to access any children
    Member parent;
    Vector2 position,scale; // just a simple vector class
    public Member(Member parent, Vector2 position, Vector2 scale){
        this.parent=parent;
        this.position=Vector2.combine(parent.getPosition(), position);// "combine" method just combines the x and y values and returns a new Vector2. also, position based on parent position
        this.scale=parent.getScale();// notice this is scale, not size so  the children will scale with the parent and not just be the same size as it
    }
    public Member(Vector2 position, Vector2 scale){//this would be and un-parented "Member"
        this.parent=parent;
        this.position=position;
        this.scale=scale;
    }

    public void update(){
       if(parent==null){
            //update position based on world coordinates
       }else{
            //update position based on parent coordinates
       }
   }

/**put getters and setters here**/

}


And there you have it, now every class that “extends” that class will be able to be parented to one another

and now for the Vector2 class:



public class Vector2{
  float x,y;//doesn't just have to be used for coordinates
float velx,vely;// velocity variables
  public Vector2(float x,float y){
    this.x=x;
    this.y=y;
  }
  public void update(){
      //update stuff here
     x+=velx;
     y+=vely;
  }
/*getters and setters here*/
/*Also, to make life easier, put in some tween (is that the word?) functions (translate,rotate,moveTo,moveOnAngle,moveToVector)*/
  public static Vector2 combine(Vector2 first,Vector2 second){

       return new Vector2(first.getX()+second.getX(), first.getY()+second.getY());


  }
}


Hope this helps in any way

Wait what, he’s talking about libgdx Group. It’s a specific class. And libgdx has its own Vector2 class too…]

@OP http://www.gamefromscratch.com/post/2013/12/11/LibGDX-Tutorial-3C-Scene-management.aspx

@kingroka123
thank’s for the input but as Jimmt said, am talking about libGdx Group.

@Jimmt
i already checked that tutorial before posting, and it seems that it’s a bit old.
this gave me an error :

stage = new Stage(Gdx.graphics.getWidth(),Gdx.graphics.getHeight(),true);

because according to the docs a Stage take a viewport as a parameter and there is also other things that seemed outdated in that tutorial, that’s why i asked for a quick example here :slight_smile:

thank you

I have no Idea but maybe try out this:


stage = new Stage(new Viewport());

oh ._. I thought he just meant hierarchical grouping. oh well, at least i tried ;D

ps: I wrote that code at about 12 am and I don’t think I even thought about it being in LibGDX :stuck_out_tongue:

The part with stage and viewport has been introduced sometime before the 1.0 release of libgdx. Viewport is just an interface as far as I know, so you can’t create it directly. Check the libgdx docs which viewport would suit you best (I tend to use the ScalingViewport): https://github.com/libgdx/libgdx/wiki/Viewports

Thnx everyone, here is exactly what i needed to have ;D


public class LearnGdx extends ApplicationAdapter {



	public static final int WIDTH = 800;
	public static final int HEIGHT = 480;

	private Stage stage;
	private Group group;

	private float rotSpeed = 5;

	@Override
	public void create() {
		

		// Create a stage
		stage = new Stage(new StretchViewport(WIDTH, HEIGHT));

		// Create a group and add it to the stage.
		group = new Group();
		stage.addActor(group);

		// Create images and add them to the group.
		final Texture region = new Texture(Gdx.files.internal("circle.png"));
		Image img = new Image(region);
		Image img2 = new Image(region);
		Image img3 = new Image(region);

		img2.setColor(new Color(1, 0, 0, 1));
		img3.setColor(new Color(0, 0, 1, 1));

		
		group.addActor(img2);
		group.addActor(img3);
		group.addActor(img);

		// Images are positioned relative to the group...

		img.setPosition(0, 0);
		img2.setPosition(img.getWidth()/2, 0);
		img3.setPosition(-img.getWidth()/2, 0);
		// Group is positioned relative to the stage...
		group.setPosition(WIDTH / 2 - img.getWidth() / 2,
				HEIGHT / 2 - img.getHeight() / 2);
		group.setOrigin(img.getWidth()/2,img.getHeight()/2);

	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		stage.act(Gdx.graphics.getDeltaTime());
		stage.draw();

		if (Gdx.input.isKeyPressed(Keys.LEFT)) {
			group.rotateBy(rotSpeed);
		}
		if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
			group.rotateBy(-rotSpeed);
		}
	}
}