Xcode Scoring System

I was wondering if anyone could tell me how to set up a scoring system where you tap something and you get points.

Thanks ahead of time.

Mac OS X (10.5.5)

Posted on Jun 24, 2009 7:02 AM

Reply
12 replies

Jun 24, 2009 3:27 PM in response to sad and mad

It's hard to provide advice without knowing more about what you're trying to do. Here are some very general hints:

First of all, if you just want to count taps, all you need is an action method hooked up to a button and an instance variable to increment. For example:

@interface MyViewController : UIViewController {
int score;
UILabel *label;
}
@property (nonatomic, assign) int score;
@property (nonatomic, retain) IBOutlet UILabel *label;
- (IBAction)didTap;
@end
@implementation MyViewController
@synthesize score, label;
- (IBAction)didTap {
score++;
label.text = [NSString stringWithFormat:@"%d", score];
}
- (void)dealloc {
[label release];
[super dealloc];
}
@end

Next, if you want the score to be saved after the program exits, you'll probably want to use NSUserDefaults. But I'll stop at this point in case I'm answering the wrong question, ok?

- Ray

Jun 25, 2009 3:05 AM in response to sad and mad

This app was made from the View-Based Application template. One label and one button are placed on the view in ScoreViewController.xib. These are connected to the label IBOutlet and the doTap IBAction method respectively.

@interface ScoreAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ScoreViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet ScoreViewController *viewController;
@end
@implementation ScoreAppDelegate
@synthesize window;
@synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[viewController readState];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)applicationWillTerminate:(UIApplication *)application {
// NSLog(@"applicationWillTerminate");
[viewController saveState];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
@end
// dictionary keys
extern NSString *const scScoreKey;
@interface ScoreViewController : UIViewController {
int score;
UILabel *label;
}
@property (nonatomic, assign) int score;
@property (nonatomic, retain) IBOutlet UILabel *label;
- (void)readState;
- (void)saveState;
- (IBAction)didTap;
@end
// dictionary keys
NSString *const scScoreKey = @"State";
@implementation ScoreViewController
@synthesize score, label;
- (IBAction)didTap {
score++;
label.text = [NSString stringWithFormat:@"%d", score];
}
- (void)readState {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
score = [defaults integerForKey:scScoreKey]; // zero if not found
NSLog(@"readState: score=%d", score);
}
- (void)saveState {
NSLog(@"saveState: score=%d", score);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:score forKey:scScoreKey];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"viewDidLoad: score=%d", score);
label.text = [NSString stringWithFormat:@"%d", score];
}
- (void)dealloc {
[label release];
[super dealloc];
}
@end

- Ray

Jul 11, 2009 7:52 PM in response to srpgamer

I'm not sure I understand which step(s) you're stuck on. Maybe we could break the question down to these categories:

1) Deciding which events are scorable (collisions and/or intersections?);
2) Detecting those events;
3) Counting the events;
4) Displaying the count;
5) Saving the count (e.g. top ten, etc.).

Can you say which of the above is stopping you? Can you describe something that you imagine happening but don't know how to implement? Don't worry about writing something that doesn't make sense (TM). Just try to name as many pieces of the puzzle as you can (Why worry that every word is being published in an international forum? I never do.).

Btw, you might get the best results by starting your own thread in this case. You can then write a more descriptive Subject line, and other developers are more likely to join the thread.

\- Ray

Jul 11, 2009 5:26 PM in response to RayNewbie

Hi Ray I was reading your posts here and you seem to know what you are doing. I am new to what I am trying to do. I am trying to make a 2D game in View Based. I have all my art and UIImages ready to go. Only thing I am missing is the scoring on collision or intersect. I'm not sure which to do. Also having the issue with the scoring all together. I want the score to be visible starting at 0000000, then increasing upon hits, like pinball. I am just stuck here. I have been trying and trying I can't figure it out and I can't get the right help, anywhere yet. Can you help?

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Xcode Scoring System

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.