My plan of releasing 12 games in 12 months is going great. If by great you mean absolutely terrible. Finally found some time today to fiddle with Cocos2D and added the achievements notification during the gameplay.
I think that the only part that still needs to be done is the Highscores/Achievements page, the game screen is pretty much complete, one thing that still remains is properly saving the achievements in UserDefaults or on disk in the Documents directory.
How do the achievements work?
An achievement is supposed to get the user to continue playing with the side-goal of unlocking more achievements. People seem to be more involved in the game that they are playing if they are given a reward, be it only a text feedback of what is going on.
In Hangman, unlocking an achievement gives the user a points bonus based on the current score and the achievement that was unlocked. These bonuses are given for two types on achievements: correct consecutive key presses and games won in a row.
Code-wise, we keep track of the user’s consecutive correct keypresses in a variable (creatively called
correctKeysPressed). We have two conditions to be able to give the user an achievement:
- the number of consecutive key presses needs to be > 0
- the number of consecutive key presses needs to be divisible by 10
If these two conditions are met the user receives a bonus based on the following formula:
bonus = score * correctKeysPressed/200
This approach gives us a somewhat scalable way of handing out achievements: the bonus is calculated based on the current score, multiplied with the number of correct keys pressed divided by 200. 200 is a “magic number” in this case, but it seems like a sensible value that results in a fair bonus allocation.
The achievements for consecutive won games is very similar:
Achievements are shown using a sprite and a CCLabelTTF. We need to compute the width of the achievement text before displaying it on screen because we need to know just how much do we need to move the achievements badge to the left. To do that, we use the NSString’s
So what does it look like?
One thing that is bugging me is the fact that when transitioning from one screen to another there is a noticeable delay, even on an iPhone4. Of course, this doesn’t happen on the simulator and my jaw dropped on the floor when I ran the game on the device.
I isolated the area where most of the stalling takes place and that’s in the keyboard generation routine. I think that Cocos2D chokes itself to near-death when trying to load the sprites that make up the keyboard. I will investigate further after I create the Achievements page.
All the code I write for Hangman is open source and can be found on GitHub.