Nov 242010
 

THIS IS NOW OUT-DATED. PART 2 AND 3 ARE UP TO DATE AND WORKING, BUT RATHER THAN USING THIS TUTORIAL PLEASE USE THE UPDATED VERSION.

In this tutorial I’m going to cover the basic of how to get OpenFeint working with your iPhone app. In part two, we’ll cover unlocking achievements and setting high scores and that should be posted within a day or two.

Let’s get started!

STEP ONE: Go to the OpenFeint Website and click the developer tab. (Top right.)
You will either need to create an account, or if you have one sign in.

When creating an account you’ll be prompted to name your game; you can add more games later so don’t stress on that side of things; add either your current app’s name or if you don’t have one come up with a unique name to use so that you can learn how to implement OpenFeint in a dummy project.

STEP TWO: Click on “App Home” – it’s at the top of the navigation menu on the left hand side of the page, then scroll down. You will see this information;

Status: Approved (Yours wont say approved yet, don’t worry about that; I’ll cover submission in part 2 – it will not effect this tutorial.)

Product Key
123456abcd

Product Secret
123456abcd

Take your product key and your product secret and save them to a text document, or alternatively, just minimize this window for now.

STEP THREE: Make sure your project folder is in order; it should have your Icon.png file in it at this stage; you’ll see why in a moment. You don’t need to add any additional files to make OpenFeint work, this is done automatically during the compile.

STEP FOUR: Time to add the code! Open your main.lua file, or, if you are using director for multiple scenes/screens, open your initial one; for me, this is menu.lua – we’re going to place the following code at the start of the file, or underneath the director code if you’re using director;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
local onDevice = true
if string.sub(system.pathForFile("Icon.png", system.ResourceDirectory),1,6)=='/Users' then
onDevice = false
end
--> This detects whether or not we're running our app on the simulator or on a device

local useOpenFeint = true

local openfeint={}
if onDevice and useOpenFeint then
openfeint = require "openfeint"
openfeint.init("Product Key", "Product Secret", "Your App's Name")
else
openfeint.launchDashboard = function() print("Hello OpenFeint") end
end
--> Launches OpenFeint on your device, or prints "of.LaunchDashboard" on the simulator

Now, obviously you are going to want to replace “Product Key”, “Product Secret” and “Your App’s Name” with the appropriate text. People often ask me when they are having difficulties with OpenFeint whether or not the quotes around these bits of information need to be in place; yes. Leave the quotes in.

This is literally all you need to do to integrate OpenFeint; however obviously we’ll want to add highscores and achievements as well, which I’ll be explaining in part 2 – because of this, we’re naturally going to want to have a button that will launch the dashboard as well, so let’s move on to….

STEP FIVE: Add your button using the following code, or your own code, really we’re just adding an image if you’re reading this you don’t need that explained to you – but in case you wish to copy and paste, here’s the code I used for that;

1
2
3
4
local openfeintbutton = display.newImage("openfeintbutton.png")
openfeintbutton.x = 160
openfeintbutton.y = 445
localGroup:insert(openfeintbutton)

Now, you might not be using director – if that’s the case you’re going to want to remove the last line, or your code will not function as intended.

STEP 6: Making the button launch the dashboard is easy; in this case hitting it on the Simulator might throw up an error; you can fix that by clarifying if onDevice is false to simply print the same text we used above, but I don’t bother with that – the only time you must do that is on the initiation, which we’ve done. Use the following code to have the button we just added open the dashboard when pressed;

1
2
3
4
5
6
local function opendash (event)
if event.phase == "ended" then
openfeint.launchDashboard()
end
end
openfeintbutton:addEventListener("touch", opendash)

And that’s it!

This is all you need to do to add OpenFeint to your application, launch it on start up and add a button which will open the dashboard; these buttons are awesome and I always put one on my main menu and one on my game over screen.

Check it out on your device, make sure it’s working fine and then stay tuned for part 2 in the coming days, which will see you adding a highscore table and some achievements!

Any questions, just ask – or if you’d like to show some love, as per usual you can friend/follow me by using the buttons up top, on the right.

Peach Pellen :)

Like this post? Subscribe to the monthly newsletter!

 

  2 Responses to “Adding OpenFeint To Your iPhone App – Part 1”

  1. [...] continuing, please ensure you have completed Part 1 of the OpenFeint Tutorial and that you have added yourself as a test user, as described in Part [...]

  2. [...] Adding OpenFeint To Your iPhone App – Part 1 [...]

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.