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

iPhone Coding Tutorial – Creating an Online Leaderboard For Your Games4

阅读更多

Submitting High Scores To The Leaderboard

Submitting the high scores is very simple. I have created a method that you can drop right in your code and use to submit the scores to your server. Here is the code for this method.

- (void) submitScore:(float) theScore username:(NSString *) username {
 
	NSString * udid = [[UIDevice currentDevice] uniqueIdentifier];
	NSString * secret = @"some_secret";
	username = [username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 
	NSString *urlString = [NSString stringWithFormat:@"http://icodeblog.com/ws/put_score.php?secret=%@&udid=%@&name=%@&score=%f",
						   secret,udid,username,theScore];
	NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
	NSError * e;
	NSData	     *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&e];
	return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}

 

Let’s go over this code. The first bit of code initializes some of the variables that will be sent to the server. We get the user’s UDID and the secret for accessing our server. Also, we must encode the username to be passed to our server in case the user enters any special characters.

Following that, we build the URL that we will be making our request to and build an NSURLRequest from that. Make sure you replace icodeblog with the URL of your server. Now the magic…

We call the sendSynchronousRequest method of NSURLConnection to send the data to our server. Using Synchronous instead of Asynchronous tells the calling thread to block and wait for the request to complete before returning. I went this route in the sample code just to simply things. You could have also sent the data Asynchronously, but you would then have to implement all of the delegate methods. I will leave that up to you as a challenge.

Once the data returns from the request, we simply convert it to an NSString and return it. Here is a quick example of how you would call this method.

[self submitScore:score username:usernameTextbox.text];

 

This code assumes that you prompted the user for their username via some sort of text box name usernameTextbox. You can choose to implement this however you like.

The last part of this tutorial will show you a simple way to display the leaderboard in a UIWebview within your application.

Displaying the leaderboard In Your Application

This section of the tutorial will show you how to display the leaderboard data in a UIWebView within your application. One assumption I am going to make is that you will be displaying the leaderboard from inside a view controller.

Here are the steps involved in displaying the leaderboard data.

1. Add a new view controller to your project and name it HighScoresViewController. Make sure you check to have it create the .h and .xib interface file.

2. We need to write the code to display this view controller. Go to the method where you want to display the high scores table. This could be in response to a button click or done automatically at the end of the game. Some simple code for doing this is:

HighScoresView * hsv = [[HighScoresView alloc] initWithNibName:@"HighScoresView"
 	bundle:[NSBundle mainBundle]];
[self presentModalViewController:hsv animated:YES];
[hsv release];

 

This will cause the high scores view controller to slide up from the bottom and display on top of the current view.

3. Now open up HighScoresViewController.h. Add a declaration for an IBOutlet UIWebview in the header file as well as an IBAction named done. Done will get called when the user presses a done button to hide the high scores table. Your header file should now look something like this.

@interface HighScoresViewController : UIViewController {
	IBOutlet UIWebView * webview;
}
 
@property(nonatomic, retain) UIWebView *webview;
-(IBAction) done:(id) sender;
 
@end

 

4. Open up Interface Builder and add a Toolbar with a button that says “Done” and a UIWebView. The interface should look like this:



 

5. Connect the UIWebView to your webview outlet and connect the touchUpInside method of the done button to your done IBAction and close Interface Builder.

6. Implement the viewDidLoad method of the HighScoresViewController to load your high scores webpage into the webview. Here is some code for doing this:

- (void)viewDidLoad {
        [super viewDidLoad];
	NSString *address = @"http://www.icodeblog.com/ws/get_scores.php";
	NSURL *url = [NSURL URLWithString:address];
	NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
	[self.webview loadRequest:requestObj];
}

 

One thing to note here is I am calling get_scores.php without any parameters. If you wanted to change the data displayed (or paginate) you might add something like ?type=name&name=brandontreb to show scores for a specific user. You really have a lot of freedom here to design this how you want.

5. Implement the done method. Since we displayed this view controller using presentModalViewControler, we can simply do the opposite by calling dismissModalViewController on the parent view controller. Here is the code for the done method.

-(IBAction) done:(id) sender {
	[self.parentViewController dismissModalViewControllerAnimated:YES];
}

 

And that’s it! You now have a fully featured leaderboard that you can use in all of your iPhone games.
If you have any questions, feel free to leave them in the comments section of this post or write me on Twitter. Thanks for reading and happy iCoding!

  • 大小: 16.8 KB
分享到:
评论

相关推荐

    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 创建数据表单(界面如下图) - **创建单表**: 在创建单表时需要注意的是必须包含一个主键字段,并且该主键...

    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 ...

    Beginning iPhone Games Development (Mar 2010) Part 2

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

    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 ...

    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...

    Beginning iPhone Games Development (Mar 2010) Part 1

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

    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