`
cenphoenix
  • 浏览: 161485 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

iPhone Coding Tutorial – Creating an Online Leaderboard For Your Games1

阅读更多

As you may have seen, there are quite a few services out there offering free leaderboards.  These are great and all, but sometimes you want to have complete control over your data.  I have put together a complete tutorial detailing step by step how you can create your own online leaderboard for use in your iPhone games. For those of you who don’t know, a leaderboard is essentially a high scores list. This tutorial will also give you a very simple introduction to interfacing with web services.

The first part of this tutorial will discuss how to build the server.  We will be doing this using PHP and MYSQL.  I will also assume you have some working knowledge of PHP and MYSQL (if not, use the Google machine to get up to speed).

Since this tutorial is so long, I have decided to split it up into pages. You will notice the page number at the bottom. Please use them to navigate between parts of this post. I feel that I have to make this explicit as I will undoubtably get some dude in the comments saying “Where is the rest of the tutorial”.

Creating The Database

When creating a leaderboard, you will need some way of storing the data.  We will be using MYSQL for our storage.  You can also be lame and simply use flat files.  This would add quite a bit of code in the long run as you would have to write all of the sorting and pagination functionality yourself. Don’t do it.

One thing to note about my php server files. I know they could be cleaned up a little and optimized (you could create a config.php that contains all the db interface code), but the goal of this tutorial is not to show you how to code killer PHP. It’s to show you how to create code that you can connect your iPhone apps to.

I like to create one file that does a complete flash of the database.  That way, when I’m testing or switch from staging to production, it is a very simple process.  So, with that being said, here is my code for create_db.php.

<?php
        // create_db.php
	/** MySQL database name */
	define('DB_NAME', '');
	/** MySQL database username */
	define('DB_USER', '');
	/** MySQL database password */
	define('DB_PASSWORD', '');
	/** MySQL hostname */
	define('DB_HOST', $_ENV{DATABASE_SERVER});
 
	$table = "highscores";
 
	// Initialization
	$conn = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
	mysql_select_db(DB_NAME, $conn);
 
	// Error checking
	if(!$conn) {
		die('Could not connect ' . mysql_error());
	}
 
	// Drop existing table if exists
	mysql_query("DROP TABLE $table", $conn);
 
	$retval = mysql_query("CREATE TABLE $table(
		id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
		udid VARCHAR(45),
		name VARCHAR(25),
		score FLOAT(10,2),
		date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
	)",$conn);
 
	if($retval) {
		echo "Database created...";
	} else {
		echo "Could not create database " . mysql_error();
	}
 
	mysql_close($conn);
?>

 

I’m not going to go into too much detail about this code, however I will give a high level description of what’s going on. One thing to note about this code is it assumes you already have the database created.

First, we are defining variables that contain our database information. Make sure you replace my empty strings with your database info. After we connect to the database, we drop the table if it already exists. This is useful if you want to wipe out your test data. Finally, we create the scores table. Navigate to this file in your browser to run it and create the scores table. Pretty easy right?

You will want to make sure to remove this file from your server after running it to avoid people resetting your database.

Now that our database table has been created, it’s time to implement the web service code to publish new scores to our leaderboard.

from:http://icodeblog.com/2009/10/29/iphone-coding-tutorial-creating-an-online-leaderboard-for-your-games/

分享到:
评论

相关推荐

    iPhone Coding Tutorial – In Application Emailing

    这篇“iPhone Coding Tutorial – In Application Emailing”教程将指导开发者如何在iPhone应用程序中集成电子邮件功能,无需离开应用就能发送邮件。这篇博客文章的链接虽然不可用(已替换为示例文本),但我们可以...

    iPhone Coding Tutorial – Inserting A UITextField In A UIAlertView

    这篇教程“iPhone Coding Tutorial – Inserting A UITextField In A UIAlertViewController”将会教你如何在现代iOS环境中,在警告视图控制器中添加一个文本字段。 首先,我们需要了解`UIAlertController`的基本...

    online coding单表表单模板上传使用说明

    online coding单表表单模板上传使用说明online coding单表表单模板上传使用说明online coding单表表单模板上传使用说明online coding单表表单模板上传使用说明online coding单表表单模板上传使用说明online coding单...

    Coding Games in Python

    A visual step-by-step guide to writing code in Python. Beginners and experienced ...With coding theory interwoven into the instructions for building each game, learning coding is made effortless and fun.

    An Introduction to Coding for Constrained Systems

    An introduction to coding for constrained systems, Draft Version, 2001. by B H Marcus, R M Roth, P H Siegel

    A Tutorial on Reed-Solomon Coding for Fault-Tolerance in RAID-like Systems

    ### 一、里德-所罗门编码(Reed-Solomon Coding)基础 里德-所罗门编码是一种非二进制线性错误校正码,广泛应用于数据存储和通信系统中,以提供强大的错误检测和纠正能力。它由Irving S. Reed和Gustave Solomon于...

    CVPR12 Tutorial on Deep Learning Sparse Coding

    百度的余凯老师在CVPR2012上的Tutorial

    JEECG Online Coding开发手册v3.6.pdf

    ### JEECG Online Coding开发手册 v3.6 知识点总结 #### 1. 单表智能表单 [Online 表单开发] ##### 1.1 创建数据表单(界面如下图) - **创建单表**: 在创建单表时需要注意的是必须包含一个主键字段,并且该主键...

    Beginning iPhone Games Development (Mar 2010) Part 2

    Beginning iPhone Games Development (Mar 2010) Part 2 of 2 Part 1 -&gt; http://download.csdn.net/source/2283635 源代码 -&gt; http://apress.com/book/view/9781430225997 iPhone games are hot! Just look at ...

    Hands-On Game Development without Coding: Create 2D and 3D games.epub

    After reading this book you will be able to develop your own 2d and 3d videogames and use it on your presentations, to speed up your level design deliveries, test your game design ideas, work on your ...

    Coding_Games_in_Scratch(英文版)

    《Coding Games in Scratch》是一本面向初学者的编程教材,主要使用Scratch编程语言来教授游戏开发的基础知识。Scratch是由麻省理工学院(MIT)的“终身幼儿园团队”开发的一款图形化编程工具,旨在帮助孩子们学习编程...

    cvpr12_tutorial_sparse_coding.ppt

    cvpr12_tutorial_sparse_coding.ppt

    Beginning iPhone Games Development

    Beginning iPhone Games Development Apress, 2010年出品 You’ve probably already read and mastered Beginning iPhone 3 Development; Exploring the iPhone SDK, the best-selling, the second edition of ...

    Beginning iPhone Games Development (Mar 2010) Part 1

    Beginning iPhone Games Development (Mar 2010) Part 1 of 2 Part 2 -&gt; http://download.csdn.net/source/2283657 源代码 -&gt; http://apress.com/book/view/9781430225997 iPhone games are hot! Just look at ...

    JEECG Online Coding开发手册v3.6

    JEECG Online Coding开发手册v3.6主要介绍了如何使用JEECG平台进行Online表单的开发和配置。 知识点一:单表智能表单开发 在单表智能表单开发中,首先需要创建数据表单,必须为数据表单设置主键,并确保主键为ID,...

    Beginning iPhone Games Development (New!Hot!)

    How to give your users access to their iPhone libraries from within games The tools and techniques of 3D audio for creating even more realistic gaming experiences How to do networking right, including...

    Learning.Buildbox.2.Game.Development.1786460300

    Using an example as a tutorial, we will relate the driving principles and you'll see how you can implement these principles to develop any games on the platform. We begin by setting expectations and ...

Global site tag (gtag.js) - Google Analytics