- 浏览: 162138 次
- 性别:
- 来自: 大连
最新评论
-
xueyw:
http://www.devdiv.com/forum-iph ...
iPhone开发网站、论坛、博客 -
Meggie_love:
受教了
游戏算法整理 算法七 无限大地图的实现 -
xueyw:
http://www.devdiv.net/bbs/forum ...
iPhone开发网站、论坛、博客 -
junlas:
APE 物理引擎与 Box2D 物理引擎对比 -
ha397666:
5、扩展的点已经被扩展过了。当扩展节点的时候,每个节点都是向四 ...
游戏算法整理 算法六:关于SLG中人物可到达范围计算的想法
iPhone Coding Tutorial – Creating an Online Leaderboard For Your Games4
- 博客分类:
- iphone
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.
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.- (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];
}
[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!
发表评论
-
UIPickerView – spinning multiple components
2010-08-24 16:24 1622I found an interesting issues w ... -
UITableView效果收集
2010-07-10 12:28 2929某些打不开需翻()墙 Customizing the bac ... -
日期处理
2010-03-17 22:58 1182NSDateFormatter *dateFor ... -
uninstall xcode
2010-02-08 02:35 1733How to uninstall Xcode and ... -
从一个url中获得文本信息(转)
2010-01-21 11:37 1393有时候你可能需要从一个url中获取一个文本文件中的信息。 下面 ... -
将图片保存在iPhone的相册中(转)
2010-01-21 11:28 2931有时候你的应用需要将应用中的图片保存到用户iPhone或者iT ... -
一些遊戲製作有關的博客(转)
2010-01-20 14:57 1200站長在收集站內朋友的博客,然後把它們列出來方便大家看,這的確是 ... -
Beautiful Snowflakes
2010-01-14 18:51 1474It is a application that displa ... -
Layer Programming with Quartz Core
2010-01-08 14:11 3526《转载》 2009/6/25 ... -
如何移除Three20中private API
2010-01-07 22:04 1837Three20 是一个非常有 ... -
iphone下Three20库(From Facebook)的设置使用方法
2010-01-07 22:03 4035Three20是一个编译的静态类库 ,在Xcode中的项目实 ... -
Opera Masks
2010-01-07 01:13 1689It is a application that introd ... -
Java读取星际译王(StarDict)词库
2010-01-06 18:08 2638下面的文件是StarDict的词库格式说明文件: Form ... -
iPhone Coding Tutorial – Creating an Online Leaderboard For Your Games3
2010-01-06 11:02 1076Displaying The Scores Table ... -
iPhone Coding Tutorial – Creating an Online Leaderboard For Your Games2
2010-01-06 11:01 738Inserting Scores Into The Dat ... -
iPhone Coding Tutorial – Creating an Online Leaderboard For Your Games1
2010-01-06 10:59 1105As you may have seen, there a ... -
iPhone Coding Tutorial – Inserting A UITextField In A UIAlertView
2010-01-06 10:44 1618This will be a simple tutorial ... -
iPhone Coding Tutorial – In Application Emailing
2010-01-06 10:36 1209A lot of applications you see ... -
Objective-C内存管理总结〜CC专版
2009-12-28 11:09 3019之前写过类似的文章,这篇以做总结,希望能帮助刚上船的兄弟。^_ ... -
tell user the status
2009-12-17 17:42 1218+(UIView *)waitingView { ...
相关推荐
这篇“iPhone Coding Tutorial – In Application Emailing”教程将指导开发者如何在iPhone应用程序中集成电子邮件功能,无需离开应用就能发送邮件。这篇博客文章的链接虽然不可用(已替换为示例文本),但我们可以...
这篇教程“iPhone Coding Tutorial – Inserting A UITextField In A UIAlertViewController”将会教你如何在现代iOS环境中,在警告视图控制器中添加一个文本字段。 首先,我们需要了解`UIAlertController`的基本...
online coding单表表单模板上传使用说明online coding单表表单模板上传使用说明online coding单表表单模板上传使用说明online coding单表表单模板上传使用说明online coding单表表单模板上传使用说明online coding单...
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, Draft Version, 2001. by B H Marcus, R M Roth, P H Siegel
### 一、里德-所罗门编码(Reed-Solomon Coding)基础 里德-所罗门编码是一种非二进制线性错误校正码,广泛应用于数据存储和通信系统中,以提供强大的错误检测和纠正能力。它由Irving S. Reed和Gustave Solomon于...
百度的余凯老师在CVPR2012上的Tutorial
### JEECG Online Coding开发手册 v3.6 知识点总结 #### 1. 单表智能表单 [Online 表单开发] ##### 1.1 创建数据表单(界面如下图) - **创建单表**: 在创建单表时需要注意的是必须包含一个主键字段,并且该主键...
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 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》是一本面向初学者的编程教材,主要使用Scratch编程语言来教授游戏开发的基础知识。Scratch是由麻省理工学院(MIT)的“终身幼儿园团队”开发的一款图形化编程工具,旨在帮助孩子们学习编程...
cvpr12_tutorial_sparse_coding.ppt
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表单的开发和配置。 知识点一:单表智能表单开发 在单表智能表单开发中,首先需要创建数据表单,必须为数据表单设置主键,并确保主键为ID,...
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 of 2 Part 2 -> http://download.csdn.net/source/2283657 源代码 -> http://apress.com/book/view/9781430225997 iPhone games are hot! Just look at ...
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 ...