Unlock Achievement - GameCenter iPhone - iphone

I am trying to unlock an achievement in the game i am making for the iPhone but being rather unsuccessful.
From Apples own GKTapper project sample demonstrating Game Center code I have copied the GameCenterManager.h and .m and the AppSpecificValues.h files into my project. I have successfully got loading the achievements and leaderboards for viewing.
However I can't work out or get right how to actually unlock an achievement. Could some point out how using this or without the GameCenterManager how can I unlock an achievement please?
Thanks.

- (void) reportAchievementIdentifier: (NSString*) identifier percentComplete: (float) percent
{
GKAchievement *achievement = [[[GKAchievement alloc] initWithIdentifier: identifier] autorelease];
if (achievement)
{
achievement.percentComplete = percent;
[achievement reportAchievementWithCompletionHandler:^(NSError *error)
{
if (error != nil)
{
// Retain the achievement object and try again later (not shown).
}
}];
}
}
Call this method like this:
[self reportAchievementIdentifier:indentifier percentComplete:percent];
If you want to just unlock the achievement call this:
[self reportAchievementIdentifier:indentifier percentComplete:100.0];
You can use the float for calculate the percent of the achievement, and if the user reaches the 100 the achievement gets unlocked.
You can also do this:
[self reportAchievementIdentifier:indentifier percentComplete:((actualpoints/neededPoints)*100.0)];
neededPoints means the points you need for unlock this achievement. For example:
actualPoints = 300;
neededPoints = 600;
It calculates: 300/600 = 0.5 * 100 = 50%

Btw, the "completed" property isn't always set to YES if you set percentComplete=100, at least not within the same session. I spent a while debugging why my game awarded achievements several times even when percentComplete got set to 100.

Related

Can't connect to [QBChat Instance] connect with User, in Quickblox

I am using Quickblox Api, for chat and video chat. iOS. And I am using the latest version of the API
When I try to Make a video call,
most of the times i don't get video, only audio.
i get video on both ends 1 out of 15 times.
3 out of 10 times video on one end.
very weird. I have good internet connection. connecting to chat users are receiving the call. Can seem to find out the issue.
After spending sometime to find the issue, I received and help from Quickblox Help Center.
If your face such Behavior on the API
1.Make Sure that you set Delegate Methods in viewDidLod, not view did appear or etc. For Ex:
- (void)viewDidLoad {
[super viewDidLoad];
[[QBChat instance] addDelegate:self];
[QBRTCClient.instance addDelegate:self];
[QBSettings setCarbonsEnabled:YES];
}
Use Breakpoints to find out if they are getting called, once you make or receive calls.
2.Make Sure that your Calling methods are correct. An array containing Users must not equal to currentUser.ID.
NSInteger currentUserID = [QBSession currentSession].currentUser.ID;
int count = 0;
NSNumber *currentUserIndex = nil;
for (NSNumber *opponentID in opponentsIDs) {
if ([opponentID integerValue] == currentUserID) {
currentUserIndex = #(count);
break;
}
count++;
}
if (currentUserIndex) [opponentsIDs removeObjectAtIndex:[currentUserIndex intValue]];
QBRTCSession *session = [QBRTCClient.instance createNewSessionWithOpponents:opponentsIDs
withConferenceType:QBRTCConferenceTypeVideo];
NSDictionary *userInfo = #{ #"key" : #"value" };
[session startCall:userInfo];
if (session) {
self.currentSession = session;
[self performSegueWithIdentifier:#"openDialogSeg" sender:self];
}
else {
[SVProgressHUD showErrorWithStatus:#"You should login to use chat API. Session hasn’t been created. Please try to relogin the chat."];
}
}
Check View Layout, size and width. make sure they are set correctly.

Game Center Integration for iPhone game?

Im a beginner for developing IOS applications mainly games. I have the game completed and have submitted it to the app store. In the mere future I want to submit an update which will include game center, primarily leader boards for scores (all time, monthly, weekly, and today). I'm having trouble understanding how to integrate the completed game with game-center. Another part that is unclear to me is what do I write in the code and how does gamekit framework know which number (score) to submit to game center. If anyone could provide detailed information I'd greatly appreciate it. Thanks!
here you have a sample project
http://developer.apple.com/library/ios/#samplecode/GKTapper/Introduction/Intro.html
To send the score you have this function, score is the score, category is the name of the leaderboard you configure on itunes connect.
- (void) reportScore: (int64_t) score forCategory: (NSString*) category {
GKScore *myScoreValue = [[[GKScore alloc] initWithCategory:category] autorelease];
myScoreValue.value = score;
[myScoreValue reportScoreWithCompletionHandler:^(NSError *error){
if(error != nil){
NSLog(#"Score Submission Failed");
} else {
NSLog(#"Score Submitted");
}
}];
}
You have to use this function to send the score when your player is killed, you don't have to track if it has been the highest, but you can track if it's greater than 0;
This tutorial uses the sample project functions in his own project, take a look, it includes sending points and achivements
http://maniacdev.com/2011/05/tutorial-game-center-basics-leaderboards-and-achievements/
Game Center is available since iOS SDK 4.1
1) Open the Xcode Help.
2) On the top you should see a navigation bar, which should say "Documentation" section and move your mouse to where it says "iOS 5.1 Library"(in my case).
3) Now, move your mouse over "Networking & Internet" and click on it.
4) You now should have a list of available APIs.
After that just look around for the APIs you want, like Leaderboards, and achievements.
According to your requirements you should look for things like GKLeaderboards, and anything else you are interested in. Those documentations should link to other documentations you would need. You can find the GKLeaderboards documentation on web
Edit: The game which you developed would be showing some score to the player after each instance of the Game. Post that score to the function - (void) reportScore: (int64_t) score forCategory: (NSString*) category eg. [self.gameCenterManager reportScore:yourscore forCategory: #"yourgamecategory"];
For the GameCenterManager.h and GameCenterManager.m from this link
update score to game center use this routine.
- (void) reportScore: (int64_t) score :(NSString*) YOUR_LeaderBoard_ID
{
GKScore *scoreReporter = [[GKScore alloc] initWithCategory:YOUR_LeaderBoard_ID];
scoreReporter.value = score;
scoreReporter.context = 0;
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
if (error != nil)
{
IsConnectFail = true;
}else{
IsConnectFail = false;
}
}];
}

Game Center achievement unlocking multiple times

I set up an achievement for passing the first level of my game and it works but when i replay the level and pass it it shows the notification banner again, how can i prevent this from happening?
Use this method to submit the achievement:
-(void) reportAchievementWithID:(NSString*) achievementID {
[GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements, NSError *error) {
if(error) NSLog(#"error reporting ach");
for (GKAchievement *ach in achievements) {
if([ach.identifier isEqualToString:achievementID]) { //already submitted
return ;
}
}
GKAchievement *achievementToSend = [[GKAchievement alloc] initWithIdentifier:achievementID];
achievementToSend.percentComplete = 100;
achievementToSend.showsCompletionBanner = YES;
[achievementToSend reportAchievementWithCompletionHandler:NULL];
}];
}
Save that the user has passed the level to NSUserDefaults, then when the user passes the level check NSUserDefaults for your key, if it is there then don't do the achievement code for Game Center.

Is it possible to do a running hi-score leaderboard in GameKit?

I want a leaderboard in GameKit that shows my total accumulated score over multiple plays, not just a score from a single play. Is there any way to do this?
In other words, a single player would never have more than one entry to their name in the leaderboard. Think total experienced earned, or lifetime number of headshots. You're not going to have an entry in that leaderboard for when you had 4 headshots, and then when you had 20 headshots. You would just have the one entry for 20 lifetime headshots.
You can do this but you have to manage it yourself. GameKit is oriented around a single-game high score, not cumulative scores. Note that a player won't appear more than once on the leader board because GKLeaderboard will only report back the highest score in the time range you specified.
Tracking a cumulative score isn't that hard. Here's some code I'm using to do this. Get the GKLeaderboard with the ID that is tracking the score, and then fetch the high score for the local user. Then, add the new total to the current high score and then report the new total.
- (void)updateLeaderboardWithID:(NSString *)identifier score:(int64_t)score {
GKLeaderboard* leaderBoard= [[GKLeaderboard alloc] init];
leaderBoard.category = identifier;
leaderBoard.timeScope = GKLeaderboardTimeScopeAllTime;
leaderBoard.range = NSMakeRange(1, 1);
[leaderBoard loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^(void) {
GKScore *localScore = leaderBoard.localPlayerScore;
int64_t newValue = localScore.value + score;
localScore = [[GKScore alloc] initWithCategory:identifier];
localScore.value = newValue;
[localScore reportScoreWithCompletionHandler:^(NSError *error){
// error handling goes here
}];
);
}];
}
Yes,It may be possible. But you have to read apple documentary.This is all about GKLeaderBoardViewController class.
You can use GKLeaderBoardViewController class for counting Highest Score.
http://developer.apple.com/library/ios/#documentation/GameKit/Reference/GKLeaderboardViewController_Ref/Reference/Reference.html

Game Center iPhone - Load Achievement Progress

I have my app give a notification when a Game Center achievement is achieved/reaches 100%, however it shows the notification every times the user completes it, but I only want it to notify the first time in actually completed.
I found this in the Apple docs:
http://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/Achievements/Achievements.html#//apple_ref/doc/uid/TP40008304-CH7-SW11
However didn't quite understand how it help fix my issue.
I only want to call this notification once, when the achievement is first achieved. So only show it if its not already achieved.
Edit
I have this to unlock the achievement:
- (void) reportAchievementIdentifier: (NSString*) identifier percentComplete: (float) percent
{
GKAchievement *achievement = [[[GKAchievement alloc] initWithIdentifier: identifier] autorelease];
if (achievement)
{
achievement.percentComplete = percent;
[achievement reportAchievementWithCompletionHandler:^(NSError *error)
{
if (error != nil)
{
// Retain the achievement object and try again later (not shown).
}
}];
}
}
And unlock with this:
[self reportAchievementIdentifier:identifier percentComplete:percent];
Then show the notification with this line:
[[GKAchievementHandler defaultHandler] notifyAchievementTitle:#"Title" andMessage:#"Message"];
So do I simply need something like this in that code chunk?
if (achievement.completed != TRUE) {
[[GKAchievementHandler defaultHandler] notifyAchievementTitle:#"Title" andMessage:#"Message"];
}
There's a "completed"-property in GKAchievement... Try to make a new GKAchievement and check if it's not completed, then unlock it.