Tips For Developing Facebook Games

The Facebook Platform has created a whole new market for online games and start ups such as the Social Games Network (SGN) and Zynga. I wrote about developing Facebook Applications a while ago as a overview of my experiences. Some of my apps are games and some are not – I'm a game developer so I prefer making games.

While going through my blog traffic data, I saw search terms specifically about developing games on Facebook. That leads me to believe that someone is looking for information about that. I have some of that knowledge to share!

I generally make games for Facebook using Flash and ActionScript. There really is not a good way to make really interactive games using web languages ​​and Flash is fairly powerful for 2D casual game experiences. You can find a list of the Facebook games that I've made or had a hand in from my developing Facebook Applications article.

There are basically three real games on that list: FlipCup Challenge, Sam's Solitaire, and Sheep Tycoon. Sheep Tycoon is by far the prettiest because there was a real artist on that project – the other two I just slapped some art together to get the game out quickly. I'm not really an artist.

Using Facebook API From Flash

Figuring out how to make make calls to the Facebook API from Flash was one of the first things we had to deal with. In Sheep Tycoon, the high score table shows up in the Flash part of the game opposed to FlipCup or Sam's Solitaire where I just did it in PHP. There are a whole bunch of Flash Facebook API's – I did not like any of them.

The solution we came up with is to make a 0x0 iframe – basically invisible – on the canvas page and load a PHP script that would do all the Facebook API calls. So, if we needed to add a new high score to our database, we would load the script in the invisible iframe. This works for sending notifications or doing any sort of Facebook API call.

Getting the Friends

Although I did not find doing this all that complicated, I did see a search term for it in my traffic data. The easiest way to accomplish this is to just get the user's friend ID's in PHP and then passing it into the game using flashvars. I do not see many cases where the user's friend list will change while the game is being played. Once you have the friend ID's in the game you can do whatever you want with it. We used it to show the list of your friend's high scores in Sheep Tycoon.

The game takes the friend ID's from flashvars. When we need to get the high scores, we pass the same ID's to our PHP script that does all the database work and then returns the data in XML form for Flash to read.

Dealing with High Scores

You can display the high scores inside the game or you can display as a PHP page. I will generally opt to do the litter because there are less steps involved. PHP will get the data and display the data while Flash has to call a PHP script to get the data and then display it. However, displaying the high scores as part of the game itself is a better experience.

The semi-tricky aspects with high scores is how to record them and assign global ranks to each player. I've used time as a tie-breaker for scores that are identical. If you got the score first, you will stay above the person who got the score second. MySQL has been our database of choice and you can use ORDER BY with more than one field so it would be: ORDER BY score, time.

In FlipCup Challenge, each user has a global rank that is determined by their high score. This rank changes as players get new high scores. Updating everyone's global rank every time someone managed to get a new high score did not seem like a good idea.

Firstly, to calculate the global rank, I figured out the number of rows that had a score higher than the score we are looking to get a new rank for. With that information, we'll know that the rank will be something lower than the row count. We then find all the scores that are the same as the score we are using and then determine where it fits in there. With those two pieces of information we can calculate the right rank.

In order to make sure the scores are correct when anyone goes to look at the Scoreboard in FlipCup, we also update the rankings of all your friends when your rank changes. Global rankings are also recalculated every time you view the Scoreboard page. This will ensure that all the global ranks will be correct when someone is viewing it.

I'm sure there are better ways for determining global ranks than this but, for FlipCup's purposes, it works fine. The main objective was to make sure the pages loaded fast so the code had to do the work quickly.

Experiment

Game development on Facebook is still in infancy. I'm not aware of many sites dedicated to providing information about games and Facebook. The games themselves are still rather primitive as most do not really tap into the potential of social networks. I believe social networks are a powerful tool that can help spread new games and game ideas.

I have not looked much into development on the "new" Facebook even though I have switched to it and my links above point to the new Facebook. My apps are still working fine so I am not too concerned. The marketing and growth of apps are going to be affected. The chance of a Zombies-esque app proliferating is very unlicensed in this new environment.

I will continue to create games for Facebook and other social networks. There are some ideas I have floating around – some of which I am working on. Experimenting with different ideas is the best any of us can do as this is a new road to travel.