How do I call this method? (Objective C) - methods

I have this method which will report a score to game center:
- (void)reportScore: (int64_t) forCategory: (NSString*) category
{
GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:category] autorelease];
scoreReporter.value = passedint;
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
if (error != nil)
{
// handle the reporting error
}
}];
}
Now I have an ibaction to try to call that method but I am not able to figure it out..
- (IBAction)uploadscore {
passedint = [[NSUserDefaults standardUserDefaults] integerForKey:#"Scoreoneplayer"];
NSLog(#"%i", passedint);
[self reportScore:(passedint)forCategory :(NSString *) category];
}
passedint is the int I want to upload to Game Center. If anyone can help, that would be greatly appreciated! :)

This should do it:
[self reportScore: passedint forCategory: #"Put your category here"];
You shouldn't need to provide types in the method call.

Related

Expected identifier or "(" error

EDITED: Thanks to your help I am closing in on the solution to my problem. I have fixed the missing "#end error", and am left with just one "Expected identifier or "("" error.
This is the code, the error is noted:
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] \
compare:v options:NSNumericSearch] == NSOrderedAscending)
#import "WViewController.h"
#import <SkillzSDK-iOS/Skillz.h>
#interface WViewController ()
#end
#implementation WViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//create backdrop image
NSMutableString *imgFile = [[NSMutableString alloc] init];
if (![globalBackgroundImage isEqualToString:#""]) [imgFile setString:globalBackgroundImage]; //falls back to first image setting for all devices;
if ((IS_WIDESCREEN_PHONE)&&(![widescreenBackgroundImage isEqualToString:#""])) [imgFile setString:widescreenBackgroundImage]; //you can specify a different image for
if ((IS_IPAD)&&(![iPadBackgroundImage isEqualToString:#""])) [imgFile setString:iPadBackgroundImage]; //widescreen phones & for iPad
if (![imgFile isEqualToString:#""]) {
UIImage *img = [[UIImage imageNamed:imgFile] retain];
CGSize imgSz = [img size];
CGSize screenSz = [[UIScreen mainScreen] bounds].size;
float imgWH = imgSz.width/imgSz.height;
float screenWH = screenSz.width/screenSz.height;
CGRect backdropFrame;
if (imgWH>=screenWH) backdropFrame = CGRectMake((screenSz.width/2)-((screenSz.height*imgWH)/2), 0, screenSz.height*imgWH, screenSz.height); //image wider than screen
else backdropFrame = CGRectMake(0, ((screenSz.height/2)-((screenSz.width/imgWH)/2)), screenSz.width, screenSz.width/imgWH);
UIImageView *backdropImageView = [[UIImageView alloc] initWithFrame:backdropFrame];
[backdropImageView setImage:img];
[backdropImageView setAlpha:backgroundImageOpacity];
[self.view addSubview:backdropImageView];
}
[self.view setBackgroundColor:globalBackgroundColor];
//init GameCenter
[[GameCenterManager sharedManager] setupManager];
[[GameCenterManager sharedManager] setDelegate:self];
//initialize the view for when the player is in the game
gameView = [[WgameView alloc] initWithFrame:[[UIScreen mainScreen] bounds] fromViewController:self];
[gameView setHidden:YES];
[self.view addSubview:gameView];
//initialize the view for then the player is on the home screen
homeView = [[WhomeView alloc] initWithFrame:[[UIScreen mainScreen] bounds] fromViewController:self];
[homeView setHidden:YES];
[self.view addSubview:homeView];
}
- (void) viewDidAppear:(BOOL)animated {
//go to home screen right away
[self goHome];
//show a RevMob fullscreen ad if we're supposed to
if (revMobActive) {
if (showRevMobFullscreenOnLaunch) {
[[RevMobAds session] showFullscreen];
}
}
//show a Chartboost ad if we're supposed to
if (chartboostActive) {
if (showChartboostOnLaunch) {
[[Chartboost sharedChartboost] showInterstitial:CBLocationHomeScreen];
}
}
}
#pragma mark game flow
-(void) multiplayerButtonPressed:(id)sender
{
NSLog(#"Multiplayer button pressed, launching Skillz!");
// Launching Skillz in landscape mode
[[Skillz skillzInstance] launchSkillzForOrientation:SkillzLandscape
launchHasCompleted:^{
// This code is called after the Skillz UI launches.
NSLog(#"Skillz just launched.");
} tournamentWillBegin:^(NSDictionary *gameRules) {
// This code is called when a player starts a game in the Skillz portal.
NSLog(#"Tournament with rules: %#", gameRules);
NSLog(#"Now starting a game…");
// INCLUDE CODE HERE TO START YOUR GAME
// …..
// …..
// …..
// END OF CODE TO START GAME
} skillzWillExit:^{
// This code is called when exiting the Skillz portal
//back to the normal game.
NSLog(#"Skillz exited.");
}];
}
- (void) startGame:(UIButton*)sender {
//hide RevMob banner ad if we're supposed to
if (revMobActive) {
if (showRevMobBannerOnHomeScreen) {
[[RevMobAds session] hideBanner];
}
}
//starts game in the mode corresponding to which button was tapped
[[WGameModeEngine sharedInstance] setCurrentGameMode:sender.titleLabel.text];
[gameView startGame];
[homeView setHidden:YES];
[gameView setHidden:NO];
//init timer if timed game
if ([[WGameModeEngine sharedInstance] isTimedGame]) {
timedGameTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(decrementTime) userInfo:nil repeats:YES] retain];
}
//notify game engine to play sound if configured
[[WGameModeEngine sharedInstance] soundEventDidHappen:#"BeginGame"];
}
- (void) goHomeButtonPressed {
[self stopGame];
[self goHome];
}
- (void) stopGame {
//stop timer if this was a timed game
if (timedGameTimer) {
[timedGameTimer invalidate];
[timedGameTimer release];
timedGameTimer=nil;
}
}
- (void) goHome {
[gameView setHidden:YES];
[homeView setHidden:NO];
//show a RevMob banner ad if we're supposed to
if (revMobActive) {
if (showRevMobBannerOnHomeScreen) {
[[RevMobAds session] showBanner];
}
}
}
- (void) decrementTime {
[[WGameModeEngine sharedInstance] timeDecreased]; //report to our game model that time has decreased
if ([[WGameModeEngine sharedInstance] timeLeft]<=0) { //if 0 seconds left,
[self timedGameEnded]; //game has ended
}
if (([[WGameModeEngine sharedInstance] timeLeft]<6)&&([[WGameModeEngine sharedInstance] timeLeft]>0)) {
//notify game engine to play sound if configured
[[WGameModeEngine sharedInstance] soundEventDidHappen:#"FiveSecondCountdown"];
}
[gameView updateLabels]; //update gameView's score and time labels
}
- (void) timedGameEnded {
//game over!
[self stopGame];
//notify game engine to play sound if configured
[[WGameModeEngine sharedInstance] soundEventDidHappen:#"GameOver"];
//show an alert with score and list of words found (if you want, you can add a whole separate screen for this instead of simple alert!)
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Game Over" message:[NSString stringWithFormat:#"You scored %d points!\n\nWords found:\n%#",[[WGameModeEngine sharedInstance] getScore],[[[WGameModeEngine sharedInstance] getWordsFound] componentsJoinedByString:#" "]] delegate: nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert setDelegate:self];
[alert show];
[alert release];
//report score to GameCenter
int sc = [[WGameModeEngine sharedInstance] getScore];
if (sc>0) [self reportScore:sc forCategory:[[[[WGameModeEngine sharedInstance] getCurrentGameMode] componentsSeparatedByString:#" "] componentsJoinedByString:#"_"]];
[#"com.bundle.appname" stringByAppendingString:[[[[[WGameModeEngine sharedInstance] getCurrentGameMode] lowercaseString] componentsSeparatedByString:#" "] componentsJoinedByString:#""]];
}
- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
//share latest score on Facebook if we're supposed to
if (FacebookShareEnabled) {[self facebookShare];}
//go to home screen
[self goHome];
//show a RevMob fullscreen ad if we're supposed to
if (revMobActive) {
if (showRevMobFullscreenWhenGameOver) {
[[RevMobAds session] showFullscreen];
}
}
//show a Chartboost ad if we're supposed to
if (chartboostActive) {
if (showChartboostWhenGameOver) {
[[Chartboost sharedChartboost] showInterstitial:CBLocationHomeScreen];
}
}
}
#pragma mark GameCenter
- (void)gameCenterManager:(GameCenterManager *)manager authenticateUser:(UIViewController *)gameCenterLoginController {
if (revMobActive) {
if (showRevMobBannerOnHomeScreen) {
[[RevMobAds session] hideBanner];
}
}
[self presentViewController:gameCenterLoginController animated:YES completion:^(void)
{if (revMobActive) {
if (showRevMobBannerOnHomeScreen) {
[[RevMobAds session] showBanner];
}}}];
}
if (isGameOver) {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<ERROR IS HERE>>>>>>>>>>>>>>>
if ([[Skillz skillzInstance] tournamentIsInProgress]) {
// The game ended and it was in a Skillz tournament,
// so report the score and go back to Skillz.
[[Skillz skillzInstance] completeTurnWithGameData:gameData
playerScore:playerScore
playerCurrentTotalScore:playerCurrentTotalScore
opponentCurrentTotalScore:opponentCurrentTotalScore
roundOutcome:turnOutcome
matchOutcome:matchOutcome
withCompletion:^{
// Code in this block is called when exiting to Skillz
// and reporting the score.
NSLog(#"Reporting score to Skillz…");
}];
} else {
// Otherwise single player game, so take the normal action
}
}
- (void) reportScore: (int64_t) score forCategory: (NSString*) category
ORIGINAL QUESTION:
I am really new to coding and hoping that someone can help me with what I am sure is a very simple problem. I have looked at a lot of other answers regarding this error, but they don't seem to apply to my situation.
The following code is part of a game app I am working on using Xcode, trying to integrate it with a third party system called SKILLZ. I did not write any of the code and am trying to understand it as I proceed with the integration.
I have noted within the code where I am getting the error:
#import "WAppDelegate.h"
#import "WViewController.h"
#import <SkillzSDK-iOS/Skillz.h>
#implementation WAppDelegate
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//initialize language engine
[WLanguageEngine sharedInstance];
//initialize game mode engine
[WGameModeEngine sharedInstance];
//initialize RevMob
if (revMobActive) {
[RevMobAds startSessionWithAppID:RevMobAppID];
}
if (revMobActive&&revMobTestingMode) {
[RevMobAds session].testingMode = RevMobAdsTestingModeWithAds;
// or
//[RevMobAds session].testingMode = RevMobAdsTestingModeWithoutAds;
}
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[WViewController alloc] initWithNibName:#"WViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
{<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<**<<EXPECTED IDENTIFIER OR "(">> ERROR OCCURS HERE**
// INITIALIZE SKILLZ HERE
// 940 is the game ID that was given to us by the Skillz Developer Portal.
// SkillzSandbox specifies that we will use the sandbox server since we
// are still developing the game.
// SkillzProduction specifies that we will use the production server since
// the game is ready for release to the AppStore
[[Skillz skillzInstance] skillzInitForGameId:#"940"
environment:SkillzSandbox];
I am getting a second occurrence of this error in a different part of the app code, but am hoping that if I can sort this one out that it will help me to understand the other.
Hoping that this is not off-topic or too specific, and that someone might be able to help.
Cheers
Jen
{<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<> ERROR OCCURS HERE
// INITIALIZE SKILLZ HERE
// 940 is the game ID that was given to us by the Skillz Developer Portal.
// SkillzSandbox specifies that we will use the sandbox server since we
// are still developing the game.
// SkillzProduction specifies that we will use the production server since
// the game is ready for release to the AppStore
[[Skillz skillzInstance] skillzInitForGameId:#"940"
environment:SkillzSandbox];
return YES;
}
This block is outside of - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Perhaps you forgot a declaration or added an extraneous } somewhere

Retrieving Int Value from Parse for Xcode and displaying it in a label

bit of a beginner here and I've been stuck on something all day!
I've got 2 Parse Tables: a User (which works perfectly for logging in etc) and a UserStats that holds variables (in this case the User's Level as the first column)
All I want to do is retrieve the User's current level and display it in a label in Xcode. I've done lots of research but I cannot work out where I've gone wrong.
I'm using this Query in my .m:
- (void) retrieveFromParse {
PFQuery query = [PFQuery queryWithClassName:#"UserStats"];
[query whereKey:#"User" equalTo:[PFUser currentUser]];
[query getFirstObjectInBackgroundWithBlock:(PFObjectobject, NSError *error)
{
if (!object)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Oh No!" message:#"Could not locate value!" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[alert show];
}
else
{
int Level = [[object objectForKey:#"Level"] intValue];
[_newlabel setText:[NSString stringWithFormat:#"%d", Level]];
}
}];
}
and my .h looks like this:
import <UIKit/UIKit.h>
import <Parse/Parse.h>
#interface ShowLevel : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *newlabel;
#end
Edit: I've turned on exception breakpoints and it's highlighting:
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
What is the actual error that you are getting? Also you have to do anything that has to do with UI on the main thread. The query is actually running on the background thread and you are either showing alertView or updating a label which you have to do on the main thread. Add dispatch_async(dispatch_get_main_queue(), ^{ }); to your code where you are doing UI
if (!object)
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Oh No!" message:#"Could not locate value!" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[alert show];
});
}
else
{
int Level = [[object objectForKey:#"Level"] intValue];
dispatch_async(dispatch_get_main_queue(), ^{
[_newlabel setText:[NSString stringWithFormat:#"%d", Level]];
});
}
This has since been resolved. I used the .m code:
PFQuery *query = [PFQuery queryWithClassName:#"UserStats"];
PFObject *Object = [query getObjectWithId:#"fghsd7fddhf"];
level = [[Object objectForKey:#"Level"] intValue];
[levelLabel setText:[NSString stringWithFormat:#"%i", level]];
While it's only temporary since I'll need to change it to point to values without specifying objectId, this works nicely for testing. Thanks to everyone who provided help :)

NSView Drag Operation Error

I'm coding in Cocoa, on OS X.
I'm trying to receive a file that is dragged and dropped onto my NSView subclass - which I can do; and get its contents and filename and display them - which I can do for any type of file first time, but on the second time, when I try and drag another file on, I can only set the title setTitle:, but not the main body text setText:
The errors I am getting are:
Canceling drag because exception 'NSInternalInconsistencyException' (reason 'Invalid parameter not satisfying: aString != nil') was raised during a dragging session
and
Assertion failure in -[NSTextFieldCell _objectValue:forString:errorDescription:], /SourceCache/AppKit/AppKit-1187/AppKit.subproj/NSCell.m:1532
My code (sorry, it's quite long!):
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
NSPasteboard *pboard;
NSDragOperation sourceDragMask;
sourceDragMask = [sender draggingSourceOperationMask];
pboard = [sender draggingPasteboard];
if ([[pboard types] containsObject:NSFilenamesPboardType]) {
NSURL *file = [NSURL URLFromPasteboard:pboard];
//NSData *data = [NSData dataWithContentsOfURL:file];
NSError *error;
NSStringEncoding encoding;
NSString *contentString = [[NSString alloc] initWithContentsOfURL:file usedEncoding:&encoding error:&error];
NSLog(#"Error: %#",error);
NSString *last = [[file path] lastPathComponent];
NSArray *parts = [last componentsSeparatedByString:#"."];
NSString *filename = [parts objectAtIndex:0];
NSString *fileType = [parts objectAtIndex:1];
NSLog(#"FILETYPE: %#", fileType);
if ([fileType isEqualToString:#"txt"] || [fileType isEqualToString:#"md"]) {
[self setTitle:filename];
if (self.textViewString == (id)[NSNull null] || self.textViewString.length == 0) {
[self setText:contentString];
} else {
BOOL whatToDo = (NSRunCriticalAlertPanel(#"What do you want to do?", nil, #"Append", #"Replace", nil) == NSAlertDefaultReturn);
if (whatToDo) {
//Append
[self setText:[NSString stringWithFormat:#"%#\n%#",self.textViewString,contentString]];
} else {
//Replace
[self setText:contentString];
}
}
return YES;
} else {
return NO;
}
} else if ([[pboard types] containsObject:NSPasteboardTypeString]) {
NSString *draggedString = [pboard stringForType:NSPasteboardTypeString];
if (self.textViewString == (id)[NSNull null] || self.textViewString.length == 0) {
[self setText:draggedString];
} else {
[self setText:[NSString stringWithFormat:#"%#\n%#",self.textViewString,draggedString]];
}
return YES;
}
else {
return NO;
}
}
Thanks in advance! :)
Sounds like Cocoa is canceling the drag when any exception is raised, and an exception is getting raised when something internally is expecting a string and is getting a nil value instead.
It's just a guess without having more information, but I would predict that stringWithFormat: is raising the exception, as it looks like the only really potentially fragile bit of what you have written.
You are doing a couple ill-advised things. First, you are assuming that -initWithContentsOfURL:usedEncoding:error: is succeeding. You should not do this. Instead, you need to pass an NSError ** that can be filled in on error, test whether contentString is nil, and, if so, check the error accordingly. I have a feeling that you will find you are getting nil, and the error will explain why.
Possibly unrelated, but your if (whatToDo) is not doing what you probably think it is. Since whatToDo is a pointer to an autoreleased NSNumber instance your conditional will always evaluate to true, since the pointer is non-zero. What you likely meant to do was something like the following:
BOOL whatToDo = (NSRunCriticalAlertPanel(#"What do you want to do?", nil, #"Append", #"Replace", nil) == NSAlertDefaultReturn);
if (whatToDo) {
//Append
[self setText:[NSString stringWithFormat:#"%#\n%#",self.textViewString,contentString]];
} else {
//Replace
[self setText:contentString];
}
Lots of thanks for the many tips and pieces of advice from this answer by Conrad Shultz! I've followed the advice and tips there.
However, my problem turned out to be very, very basic. It lied in the line BOOL whatToDo = (NSRunCriticalAlertPanel(#"What do you want to do?", nil, #"Append", #"Replace", nil) == NSAlertDefaultReturn);
It turns out a string must be passed to the second parameter, but I was passing nil. Fixed!

Game Center? Xcode

I have been working ver hard on Game center. I have tested so many codes I've lost count.
I would love to know how to automatically submit score as well
here are some codes i have used but i am not sure if this will help
-(IBAction)showleaderboard:(id)sender{
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc]init];
if (leaderboardController !=NULL) {
leaderboardController.category = self.currentLeaderboard;
leaderboardController.timeScope = GKLeaderboardTimeScopeAllTime;
leaderboardController.leaderboardDelegate = self;
[self presentModalViewController:leaderboardController animated:YES];
}
}
-(void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController{
[self dismissModalViewControllerAnimated:YES];
[viewController release];
}
-(IBAction)showAchivementLeaderboard:(id)sender{
GKAchievementViewController *achivements = [[GKAchievementViewController alloc]init];
if (achivements !=NULL) {
achivements.achievementDelegate = self;
[self presentModalViewController:achivements animated:YES];
}
}
-(void)achievementViewControllerDidFinish:(GKAchievementViewController *)viewController{
[self dismissModalViewControllerAnimated:YES];
[viewController release];
}
self.currentLeaderboard= kEasyLeaderboardID;
if ([gameCenterManager isGameCenterAvailible]) {
self.gameCenterManager= [[[GameCenterManager alloc] init] autorelease];
[self.gameCenterManager setDelegate:self];
[self.gameCenterManager authenticateLocalUser];
}else{
UIAlertView *openURLAlert = [[ UIAlertView alloc] initWithTitle:#"Game Center turned off" message:#"You are not connected to game center." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[openURLAlert show];
[openURLAlert release];
}
To report a score you need to use GKScore as follows;
GKScore *scoreReporter = [[GKScore alloc] initWithCategory:self.gameCategory.leaderboardString];
scoreReporter.value = score;
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
if (error != nil) {
[[KYTGlobals instance] storeScore:score forCategory:self.gameCategory.leaderboardString];
}
}];
The above code allocates and inits a GKScore object using the identifier that you have already set up on game center for the category that you want to report a score for. You update the value for the score and then use reportScoreWithCompletionHandler making sure to test for error so that you can archive the score and report it later.

xcode "Done Button" GameCenter doesn´t do anything

this is my first app including the gamecenter, and i have a little problem with it.
i implemented some code i found in the net that should work. Everything works fine, except for "done button" :(
.m
#import <GameKit/GameKit.h>
- (void)viewDidLoad {
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
if (error == nil)
NSLog(#"Authentication Successful!");
else
NSLog(#"Authentication Failed!");
}];
-(IBAction)subscore{
GKScore *scoreReporter = [[GKScore alloc] initWithCategory:#"lrhseasy"];
scoreReporter.value = score;
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
if (error != nil) {
NSLog(#"Submitting a score failed!");
}
else {
NSLog(#"Submitting succeeded!");
}
}];
}
-(IBAction)showLeader{
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc]init];
if (leaderboardController != nil) {
//leaderboardController.leaderboardDelegate = self;
[self presentModalViewController:leaderboardController animated:YES];
}
}
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController {
NSLog(#"Close leaderboard");
[self dismissModalViewControllerAnimated:YES];
[viewController.view.superview removeFromSuperview];
}
The last function isn´t called, when the done button is pressed - i don´t get "close leaderboard" in the debug console.
After searching for 3 hours, the only thing i found was
"Did you add GKLeaderboardViewControllerDelegate to the list of protocols that this class implements? " but i do not know what this means or how its done :(
I am having a similar problem but to add GKLeaderBoardViewControllerDelegate to your protocol list do this:
#interface YourClass: NSObject <GKLeaderBoardViewControllerDelegate> {
}
#end

Resources