# What is a good approach to dynamically change actor Position?

In my card game there are 2 players with each player having a hand(a ListArray of of Cards.In this case its handPlayer1). The Card class extends Actor.This is the code I use to deal the cards to the first player(I call this in the show() method) :

 for (int i = 0; i < 6; i++) {
handPlayer1.get(i).setPosition(20 + i * 35, TABLE_HEIGHT - CARD_HEIGHT - 30);
}


For now whenever a player plays a card it is removed from their hand and moved to the table.If the player draws a card it is added to the hand of the player like so :

deck.get(deck.size() - 1).addAction(Actions.moveTo(handPlayer1.get(handPlayer1.size() - 1).getX() + 20, 30, 0.1f));


But the problem is that sometimes the cards go out of screen bounds when the player has too much cards and are not visible.

.What is a good way to manipulate the positioning of Actors so that the hand stays in the center and all cards are a set distance from one another? I have read up on Groups but I don't know if adding the cards to a Group would change anything.(Picture below for reference)

Warning - this is not the best approach, but it's for UI learning purposes.

As I can see you keep your 'deck' in a Table.

First of all, make a wrapper Table which will fill the whole screen (it will be invisible don't worry):

final Table fullScreenTable = new Table();

Set it's size to full screen w/h and add it to the stage:

stage.add(fullScreenTable);
fullScreenTable.setSize(Gdx.graphics.getWidth, Gdx.graphics.getHeight());


Now for the player, also make a Table, like this:

final Table playersTable = new Table();

now, add the table to the screen Table like this:

fullScreenTable.add(playersTable).size(Gdx.graphics.getWidth(), 250);
playersTable.debug(); // calling debug will enable debug lines so you'll see where your table is (cause it's invisible D:)


Your playersTable is now on the bottom of the screen. If you add an element to it, it will be placed on the center of the table and the next element will be placed on the right of the previous.

Now, when you have your list with the cards:

public void rebuildHand(){
playersTable.clear(); // it will clear the table from previous cards
for (int i = 0; i < handPlayer1.size(); i++) {
Card card = handPlayer1.get(i);
// we will have to 'pad' the card to the left, cause otherwise they'll stick to each other
final float cardPadding = card.getWidth() - 10;

The code above clears the table (it doesn't remove your cards from the memory, don't worry). If you 'draw a card' or 'play a card' you need to call the function above and it will rebuild the whole table, bringing all cards together (but remember that when you draw a card, you need to add it to the handPlayer1 list and if you play a card, you have to remove it from the list.