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)

card game


1 Answer 1


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:

fullScreenTable.setSize(Gdx.graphics.getWidth, Gdx.graphics.getHeight());
fullScreenTable.bottom(); // remember about this, it will force adding components to the bottom of the table.

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;
        playersTable.add(card).size(card.getWidth(), card.getHeight()).padLeft(-cardPadding);
        // or if it doesn't work call .padRight(cardPadding);

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.

This way, there will be no spaces between cards if you play one and you can draw more cards now cause they'll stick to each other with every draw.

If it's still not enought - you could set a MAX_CARDS_IN_HAND variable or make a new row() in the Table if your cards is too big and place next cards beneath them (two rows).

Read about Table UI here: https://github.com/libgdx/libgdx/wiki/Table

  • \$\begingroup\$ That wouldn't work for me because my cards need to overlap and the Table doesn't support Widget overlapping.But I might use this later to make the GUI so thank you. \$\endgroup\$ Commented Nov 7, 2016 at 23:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .