Reputation: 2630
I am writing a card game with multiple cards that will stack up on the unplayed deck, and also on player hands.
Currently I add all 100 card actors to the screen at the start to make it easier so i don't have to manage adding them when they are needed. They all start on top of each other and some get dealt out to players hands, which are also just piles of cards. So in theory with a 5 player game there are only 6 cards visible, the deck and the top off each player's hand.
I'm hoping someone can tell me off a clever method to mean that only 6 draw methods get run on the actors that are visible, or do i have to manage all this myself?
If i do, would you suggest adding the actors only when i need them, or adding them invisible and then setting them visible when i need to?
Or another method?
Upvotes: 0
Views: 494
Reputation: 113
To save a lot of computation you could just render the image of the cards 6 times. If a card is being dealt just render a 7th being moved across the screen. This avoids calling the draw method of a large number of objects.
Upvotes: 1
Reputation: 1005
If only one card in each pile is visible you could use stacks. A stack is like an array where you access the elements from one end and only one at a time. You then only call draw methods on the top card on the stack (peek). Then dispose the top card and make the next card the top (pop).
Some tutorials and examples:
Or just google "Java Stack" Its very simple to use.
Upvotes: 0