The drag & drop mechanism is nearing its completion. Dragging a card and rebounding it back to its original position and z index can now be performed as well as dragging the whole deck of cards.
DeckView
A new class named DeckView is created to hold CardView objects. This class is responsible for arranging and shuffling the cards. The size of a DeckView is set to 320×480 by default. The ideal size should be only the space the cards occupy but this will be implemented later on. A DeckView can also be dragged.
The shuffle has also been improved. Instead of maintaining a separate NSMutableArray that holds CardView objects, the exchangeSubviewAtIndex:withSubviewAtIndex: method of UIView is used. This is demonstrated in the screencast when the iPhone screen is double tapped.
-(void)shuffle {for(int i =0; i < 52; i++){int j = arc4random()%52;
[self exchangeSubviewAtIndex:i withSubviewAtIndex:j];
}}
Here’s the screencast of the output from the previous article with some minor improvements including scaled down cards and shuffling. I suggest you view the video in HD.
The dimensions of the cards have been reduced although I did not do any physical resize on the actual PNG images yet. I simply used drawInRect: to scale down the cards. The more optimized way is to resize the actual PNG images using your photo editing software and not do the scaling programmatically.
Here’s the code for the drawRect: method in CardView.m:
The TRIM_SIZE constant is the value you want the card to be reduced by.
Shuffle
The arc4random() function should be able to provide a decent random number. The algorithm is a standard naive shuffle but the seed value from arc4random() should be sufficient for our needs. According to this article, the seed is what’s really important and not the choice of algorithm.
-(void)shuffle {for(int i =0; i < 52; i++){int j = arc4random()%52;
[cardPack exchangeObjectAtIndex:i withObjectAtIndex:j];
}}
Sorry for this late post. I encountered some technical issues with the blog that prevented me from accessing the Admin. Anyway on to the post.
So far I’ve managed to display a deck of cards at random locations within the iPhone simulator’s screen and perform a simple animated scattering effect. I’ll be discussing some of the more important code that I implemented, majority of which are based on the MoveMe sample app provided by Apple.
Let’s start with a very simple data structure to represent a Card object. An enum type is more appropriate for the suits instead of an int so go ahead and implement it that way in your own code, I’ll be fixing mine later on.
You still need a little bit of project management even if you’re just an individual working on a small game. In my earlier personal projects, I simply used plain text files to track everything. It worked fine for me and I was able to finish and deploy those projects. It might work for you too. But there are already online tools specializing in task tracking and project management, most are free and very straightforward to use. I now prefer them over plain text files although I still maintain a “brainstorm” file where I dump unorganized thoughts. I use Google Docs for this.
Another very important practice is to use source code version control. Yes even if you’re working alone. I’m sure everyone is aware by now of how important version control is but if you’re an exception, I urge you to apply it in all your non trivial projects. I’m a big fan particularly of the comparison and diff features.
I’m using Unfuddle to manage my project and host the source code and other project files. I’m using the free package for now but will upgrade once the project grows. I suggest you use it as well or something similar.