ios - record game score on a tap me game -


ios game creating game there button , there aim of game see how taps can before time runs out

but have problem need recored high score, how can that.

.h code

#import <uikit/uikit.h>  @interface errrviewcontroller : uiviewcontroller{  iboutlet uilabel *label; iboutlet uilabel *timerlabel;  nsinteger count; nsinteger seconds;  nstimer *timer; //add this!! }  - (ibaction) buttonpressed; - (void)setupgame; - (void)subtracttime;    @end 

m. code

#import "errrviewcontroller.h"  @interface errrviewcontroller ()  @end  @implementation errrviewcontroller   - (void)viewdidload {     [super viewdidload];     // additional setup after loading view, typically nib.     [self setupgame]; }  - (void)didreceivememorywarning {     [super didreceivememorywarning];     // dispose of resources can recreated. }    //implementing our method - (ibaction)buttonpressed{     count++;      label.text = [nsstring stringwithformat:@"score\n%i",count]; }  - (void)setupgame{     seconds = 30;     count = 0;      timerlabel.text = [nsstring stringwithformat:@"time: %i",seconds];     label.text = [nsstring stringwithformat:@"score\n%i",count];      timer = [nstimer scheduledtimerwithtimeinterval:1.0f target:self selector:@selector(subtracttime) userinfo:nil repeats:yes];  }  - (void)subtracttime{     seconds--;     timerlabel.text = [nsstring stringwithformat:@"time: %i",seconds];      if(seconds == 0){         [timer invalidate];          uialertview *alert = [[uialertview alloc] initwithtitle:@"time up!" message:[nsstring stringwithformat:@"you scored %i points",count] delegate:self cancelbuttontitle:@"play again" otherbuttontitles:nil];         [alert show];     } }  - (void)alertview:(uialertview *)alertview clickedbuttonatindex:(nsinteger)buttonindex{     [self setupgame]; }  @end  #import "errrviewcontroller.h"  @interface errrviewcontroller ()  @end  @implementation errrviewcontroller   - (void)viewdidload {     [super viewdidload];     // additional setup after loading view, typically nib.     [self setupgame]; }  - (void)didreceivememorywarning {     [super didreceivememorywarning];     // dispose of resources can recreated. }    //implementing our method - (ibaction)buttonpressed{     count++;      label.text = [nsstring stringwithformat:@"score\n%i",count]; }  - (void)setupgame{     seconds = 30;     count = 0;      timerlabel.text = [nsstring stringwithformat:@"time: %i",seconds];     label.text = [nsstring stringwithformat:@"score\n%i",count];      timer = [nstimer scheduledtimerwithtimeinterval:1.0f target:self selector:@selector(subtracttime) userinfo:nil repeats:yes];  }  - (void)subtracttime{     seconds--;     timerlabel.text = [nsstring stringwithformat:@"time: %i",seconds];      if(seconds == 0){         [timer invalidate];          uialertview *alert = [[uialertview alloc] initwithtitle:@"time up!" message:[nsstring stringwithformat:@"you scored %i points",count] delegate:self cancelbuttontitle:@"play again" otherbuttontitles:nil];         [alert show];     } }  - (void)alertview:(uialertview *)alertview clickedbuttonatindex:(nsinteger)buttonindex{     [self setupgame]; }     @end 

please explain how in steps

please provide links if possible can me

you can saving highscore plist file, in form of nsdictionary. example:

int highscore; //your highscore variable, believe can calculate 

then can have save function like:

-(void) savehighscore{     nsdictionary* highscoredict = [[nsdictionary alloc] initwithobjectsandkeys: [nsstring stringwithformat:@"%d", highscore], @"highscore",nil];     nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);     //get path documents directory     nsstring* filepath = [[paths objectatindex:0] stringbyappendingpathcomponent: @"highscore.plist"];     //check if file exists     if([[nsfilemanager defaultmanager]fileexistsatpath:filepath]){         //delete if does, because we're going replace anyway         [[nsfilemanager defaultmanager]removeitematpath:filepath error:nil];     }     [highscoredict writetofile:filepath atomically:yes]; // write file } 

and can have load highscore function like:

-(int) loadhighscore{     nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);     nsstring* filepath = [[paths objectatindex:0] stringbyappendingpathcomponent: @"highscore.plist"];     //check if file exists     if([[nsfilemanager defaultmanager]fileexistsatpath:filepath]){         nsdictionary* highscoredict = [nsdictionary dictionarywithcontentsoffile:filepath];         //retrieve value of highscore file         return [[highscoredict valueforkey: @"highscore"]intvalue];     }     //otherwise return 0 highscore     return 0; } 

and add [self loadhighscore]; in viewdidload function.

hope helps.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -