Facebook Connect is another way to post on the wall feed by using the native Facebook UI features. We'll be creating a different way to post messages and scores to the newsfeed. In order to see how Facebook Connect operates, you need to load the build to a device to view the results. It will not run in the simulator.
Chapter 9
folder, copy the Facebook Connect
project folder to your desktop. All the configuration, libraries, and assets needed are already included. You can download the project files that accompany this book from the Packt Publishing website.main.lua
file and save it to the project folder.display.setStatusBar( display.HiddenStatusBar ) local ui = require("ui") local facebook = require "facebook" local fbBtn local score = 100
onFBTouch()
with an event parameter. Add an if
statement that contains event.phase == release
. Also, include your Facebook app ID in a string format:local onFBTouch = function( event )
if event.phase == "release" then
local fbAppID = "0123456789" -- Your FB App ID from facebook developer's panel
onFBTouch(event)
called facebookListener()
with an event parameter as well. Include an if
statement that refers to "session" == event.type
:local facebookListener = function( event ) if ( "session" == event.type ) then
if
statement where "login"
equals to event.phase
. Include a local variable called theMessage
to display the message you want to share with other Facebook users:if ( "login" == event.phase ) then local theMessage = "Got a score of " .. score .. " on Your App Name Here!"
facebook.request()
function that will post the following messages to the user's Facebook wall. Close any remaining if
statements with end
in the facebookListener(event)
function:facebook.request( "me/feed", "POST", { message=theMessage, name="Your App Name Here", caption="Download and compete with me!", link="http://itunes.apple.com/us/app/your-app-name/id382456881?mt=8", picture="http://www.yoursite.com/yourimage.png"} ) end end end
facebook.login()
function that includes your App ID, listener, and permissions to post on a user's Facebook wall. Close the remainder of the onFBTouch(event)
function:facebook.login(fbAppID, facebookListener, {"publish_actions"}) end end
fbBtn
UI function and save your file:fbBtn = ui.newButton{ defaultSrc = "facebookbtn.png", defaultX = 100, defaultY = 100, overSrc = "facebookbtn-over.png", overX = 100, overY = 100, onEvent = onFBTouch, } fbBtn.x = 160; fbBtn.y = 160
One of the most important things that need to be done is require "facebook"
in order to have the Facebook API to work. We also created a local variable called score
with the value of 100.
The onFBTouch(event)
function will initiate the event parameter on "release"
of fbBtn
. Within the function, fbAppID
is included with characters in a string format. This will be a unique set of numbers that you must obtain from the Facebook Developers website. The App ID will be created for you when you make an App page on the site.
Another function, facebookListener(event)
, is created, and it will initiate all fbConnect
events. The if
statement that contains ("login" == event.phase )
will request to post a message to your feed through "me/feed, "POST"
. The feed contains the following:
message=theMessage
: This refers to the string that belongs to the variable. It also concatenates scores, so it displays the value as well.name
: This is a message that includes your app name or subject matter.caption
: This is a short persuasive message to catch other users' attention about playing the game.link
: This provides the URL to download the game from either the App Store or Google Play Store.picture
: This is a URL that contains your image that displays your app icon or a visual representation of the game.After the parameters are set, facebook.login()
will refer to fbAppID
and facebookListener()
to see if a valid application ID is being used to post on Facebook. On success, the post is published through "publish_actions"
.
See if you can figure out how to display a dialog box using Facebook Connect and using the same setup as shown in the preceding example. The following line will display this as:
facebook.showDialog( {action="stream.publish"} )
Now, see where in the code facebook.showDialog()
can be accessed. This is another way of posting messages to Facebook.
Q1. What is the specific API that scales down high-resolution sprite sheets?
object.xScale
display.contentScaleX
object.xReference
Q2. What are the publishing permissions called that allow posting on a user's wall on Facebook?
"publish_stream"
"publish_actions"
"post"
"post_listener"
Q3. Which parameter(s) is required for facebook.login()
?
appId
listener
permissions