Thursday, 15 March 2012

iphone - Alternative Level Difficulty -



iphone - Alternative Level Difficulty -

i in process of creating simple game, have run code issues. way game works there 2 balls. 1 enemy , 1 player. every 5 seconds game increases difficulty next level. current code here, , replicated below:

// // viewcontroller.m // bakerbattle // // created cole bergmann on 2/6/13. // copyright (c) 2013 cole bergmann. rights reserved. // #import "viewcontroller.h" #import "secondview.h" @interface viewcontroller () @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; //(x speed, y speed) vvv pos = cgpointmake(6,5); [three sethidden:yes]; [two sethidden:yes]; [one sethidden:yes]; [go sethidden:yes]; [level1 sethidden:yes]; [level2 sethidden:yes]; [level3 sethidden:yes]; } -(ibaction)start { [startbutton sethidden:yes]; [logo sethidden:yes]; [instructions sethidden:yes]; [self performselector:@selector(three) withobject:nil afterdelay:0.5]; } -(void)three { [three sethidden:no]; [self performselector:@selector(two) withobject:nil afterdelay:0.5]; } -(void)two { [two sethidden:no]; [self performselector:@selector(one) withobject:nil afterdelay:0.5]; } -(void)one { [one sethidden:no]; [self performselector:@selector(go) withobject:nil afterdelay:0.5]; } -(void)go { [go sethidden:no]; [self performselector:@selector(gamestart) withobject:nil afterdelay:0.5]; } -(void)gamestart { [level1 sethidden:no]; [three sethidden:yes]; [two sethidden:yes]; [one sethidden:yes]; [go sethidden:yes]; [logo sethidden:yes]; pos = cgpointmake(6,5); randommain = [nstimer scheduledtimerwithtimeinterval:(0.03) target:self selector:@selector(ontimer) userinfo:nil repeats:yes]; [self performselector:@selector(difficulty2) withobject:nil afterdelay:5.0]; } -(void)ontimer { [self checkcollision]; enemy.center = cgpointmake(enemy.center.x+pos.x,enemy.center.y+pos.y); if (enemy.center.x > 320 || enemy.center.x < 0) pos.x = -pos.x; if (enemy.center.y > 480 || enemy.center.y < 0) pos.y = -pos.y; } -(void)checkcollision { if( cgrectintersectsrect(player.frame,enemy.frame)) { [randommain invalidate]; [startbutton sethidden:no]; [logo sethidden:no]; [three sethidden:yes]; [two sethidden:yes]; [one sethidden:yes]; [go sethidden:yes]; [level1 sethidden:yes]; [level2 sethidden:yes]; [level3 sethidden:yes]; [instructions sethidden:no]; cgrect frame = [player frame]; frame.origin.x = 137.0f; frame.origin.y = 326.0; [player setframe:frame]; cgrect frame2 = [enemy frame]; frame2.origin.x = 137.0f; frame2.origin.y = 20.0; [enemy setframe:frame2]; uialertview *alert = [[uialertview alloc] initwithtitle:@"you lost!" message:[nsstring stringwithformat:@"baker cat stole dough!"] delegate:nil cancelbuttontitle:@"i want rematch!" otherbuttontitles:nil]; [alert show]; } } - (void)touchesmoved:(nsset *)touches withevent:(uievent *)event { uitouch *mytouch = [[event alltouches] anyobject]; player.center = [mytouch locationinview:self.view]; } //##################################-[difficulty levels]-##############################################\\ -(void)difficulty2 { [level1 sethidden:yes]; pos = cgpointmake(10,8); [self performselector:@selector(difficulty3) withobject:nil afterdelay:5.0]; [level2 sethidden:no]; } -(void)difficulty3 { [level2 sethidden:yes]; pos = cgpointmake(14,10); [self performselector:@selector(win) withobject:nil afterdelay:5.0]; [level3 sethidden:no]; } -(void)win { uialertview *alert = [[uialertview alloc] initwithtitle:@"you won!" message:[nsstring stringwithformat:@"you defeated baker cat!"] delegate:nil cancelbuttontitle:@"play again" otherbuttontitles:nil]; [alert show]; [randommain invalidate]; [startbutton sethidden:no]; [logo sethidden:no]; [instructions sethidden:no]; [three sethidden:yes]; [two sethidden:yes]; [one sethidden:yes]; [go sethidden:yes]; [level1 sethidden:yes]; [level2 sethidden:yes]; [level3 sethidden:yes]; cgrect frame = [player frame]; frame.origin.x = 137.0f; frame.origin.y = 326.0; [player setframe:frame]; cgrect frame2 = [enemy frame]; frame2.origin.x = 137.0f; frame2.origin.y = 20.0; [enemy setframe:frame2]; } @end

i wondering if knew alternative method define levels. basically, thing different levels have pos = cgpointmake(10,8); , alter leveltext level game on.

the problem using if player loses, level not reset. example, if dies on level 2, death message , go title screen, shortly after displays "level 3" @ top of screen, , sends message saying player won. help appreciated.

you perform selectors after delay never cancel selectors. have lot of different method names canceling selector may not feasible, quicker solution (which may have more side effects) add together next code method terminates game state (and transitions new level):

[nsobject cancelpreviousperformrequestswithtarget:self];

if need cancel selector +cancelpreviousperformrequestswithtarget:(id)atarget selector:(sel)aselector object:(id)anargument].

iphone

No comments:

Post a Comment