This tutorial covers adding OpenFeint achievements to your iPhone app using Lua and Corona.
*The rest of part 2 can be ignored entirely or you can come back to it later if you don’t want a leaderboard at this time; just ensure you have added yourself as a test user or this will not work.
Alright, let’s get some OpenFeint achievements into your app!
STEP ONE: We’re going to need to create at least one OpenFeint achievement in order to test this, so head on over to the OpenFeint site now and throw something together. It can be a real achievement or a test achievement as while it will not be able to be deleted once you have achieved it on your OpenFeint account, all details can be edited. Minimize the window, but don’t close it.
STEP TWO: Open up your main game file, this will be main.lua if you are not using director, or something like game.lua if you are, and paste the following code somewhere below where you’ve done your usual vital set up of a scene/screen and performed a check to see if we are on our device or the Corona simulator as explained in previous parts of this tutorial;
function Ach1000 (event)
if onDevice and score.getScore() > 999 then
print("calling ach 1000")
timerAch1000 = timer.performWithDelay(2000, Ach1000, 0)
Now, I am using a timer – but I’ll get to that in a moment. First, check whether you are using the term “OnDevice” or “OnDeviceGO” and make sure this matches up with that. (I have used both in previous tutorials.)
As you can see, I’ve used a big label to identify what I’m doing – you don’t need it but if you have a lot of achievements (Happy Doodle Sushi Smile has 35 currently) you will find it helps to comment them clearly and keep them in a nice order.
I am using a timer here because this was my first real game with Corona and it made sense to me; my app runs fine. If you feel like you’re suffering though, or have a general aversion to timers, you could instead add this as a function when you change your score. For example, I could have said every time my hero eats a piece of sushi, (the point of HDSS,) if the score is >999 then unlock the OpenFeint achievement. It’s a matter of preference.
STEP THREE: Change the “123456″ above to your achievement ID, which you can see by opening the OpenFeint window that we minimized a moment ago, then change your function’s name (and the timer’s if you are using one) to whatever you feel appropriate. You may also want to change what is printed, or remove it entirely, again, a matter of preference.
STEP FOUR: Go to whichever event triggers your “game over” and add the following line of code as part of the function;
Of course, you’ll want to use your own timer’s name if you decided to change it, or if you didn’t need a timer you can skip this step entirely.
STEP FIVE: Compile it and test it on your device!
And we’re done!
This is not a complicated thing to do and it shouldn’t cause you any headaches, particularly if you have successfully completed parts 1 and/or 2 of my OpenFeint tutorials.
If you have problems, as usual, I implore you to get in touch – I’m no where near as advanced as a lot of people when it comes to Corona or Lua, however I do love to help whenever I can.
*As a point of interest for those wondering, with 35 achievements all using timers I haven’t had any issues – I mentioned the figure above, but wanted to clarify.
I look forward to my next tutorial, although I currently have no idea what exactly it will be about!
PS – A belated happy Thanksgiving to those of you in America. I am jealous of the copious amounts of turkey that were undoubtedly consumed and that I myself only get to eat once a year at Christmas and am looking forward to immensely in *checks app made with Corona exclusively for this purpose* 26 days, 22 hours and 51 minutes!