Saturday, July 27, 2019

7-1 Creating the Card Movie

As you already know, a deck of cards consists of 52 cards (54 if you count the two jokers, but for now we won’t worry about those). Obviously, this means that we are going to need 52 images. In addition to the images for every card, we are also going to need an image for the back of the cards.

There are two ways we can create images for the cards. One way, if you already have a set of bitmaps for the cards, is to simply import the 52 bitmap images. The advantage of this is speed. The disadvantage is that the resulting images are not vectors and will not scale well. One way to get around the vector problem would be to convert every card into a vector image. This only takes a slight bit more time and you end up with better scalability.

The other way is to build the cards in Animate CC (or a vector drawing program that lets you export images to Animate CC). This can potentially reduce the overall size of the deck by a fair amount. Having the cards being proper vector images also enables very good scalability. If you don’t already have a set of bitmap card images, then creating the cards in Adobe Animate CC will take just as long as it would to create them in a bitmap based paint program.

For this book, we are going to build all the cards in Adobe Animate CC. One way would be to simply create 53 graphic images. This works, but when you look at a deck of cards you will quickly notice that the cards are only made up of a small subset of images. If we create a series of symbols for the components that make up a card, we will easily be able to create the entire deck by assembling our components. This helps reduce both the time it takes to build the deck of cards and the amount of memory required for the cards at the cost of taking a bit more time to draw the cards but as card games are not fast-paced games this is not a huge concern.

When you think about it, a set of cards can be broken into 34 components. Making this even better, most of these are small components that only take a few curves to represent. The components used in this are shown in figure below.



Now, the top line consists of “Blank Card”, “Jack”, “Queen”, King. The Blank Card symbol is the background symbol. Notice that we surround the card with a hairline box. The card is 100 by 140 as that is the resolution I had in the bitmap deck that I created for my Java card games. The cards could be any size you desired, and because they are vectors the cards will look good scaled up or down.

The second line Consists of the suits, named “Spade,” “Heart,” “Club,” and “Diamond”. The third and fourth lines consist of the card face value text. There are two versions, one for black cards and one for red cards. The symbols are named, “Blk A,” “Blk 2,” “Blk 3,” “Blk 4,” “Blk 5,” “Blk 6,” “Blk 7,” “Blk 8,” “Blk 9,” “Blk 10,” “Blk J,” “Blk Q,” “Blk K,” “Red A,” “Red 2,” “Red 3,” “Red 4,” “Red 5,” “Red 6,” “Red 7,” “Red 8,” “Red 9,” “Red 10,” “Red  J,” “Red Q,” and “Red K.”

Once we have created the components, we are going to need to create a card movie. The purpose of this movie is to hold the images for all the cards and go to the appropriate image based on what value is assigned to the card. I have decided to give each card a numerical ID, which is as follows. The ID of 0 represents no visible value (in other words the card back is shown). cards are then given ID in numerical order, with Aces being the first card and kings being the last. We go through the suits in alphabetical order, so we have the Clubs, Diamonds, Hearts, then Spades.

As the ID can be simply calculated based on the value of the card (Aces being valued as 1, Jacks valued as 11, Queens valued as 12 and Kings valued as 13), it makes sense to take advantage of this fact. We do this by mathematically determining the position of the card. How? Every card is a single frame. We have the card back on frame one and start the first card on frame 11. We then simply need to add 10 to the ID to find the correct frame for the card face. Note that Create.js uses 0 based indexing instead of the 1 based indexing that non-canvas animations use. This will mean that the frame numbers will be off by one so care needs to be taken when writing the JavaScript for going to specific frames using the frame number.

Now, a bit of code is going to be in order. We are going to need to get and set the ID of the card. We also want to be able to show the back or the front of a card. While neither of the games in this part of the book use this feature, it is simple enough to implement and will be of obvious use with many other card games. This will be covered next fortnight.

No comments: