0
votes

how can i reset a view controller in xcode to its state when i started the app

@interface Game ()

@end

@implementation Game

-(void)Restart{
Mx = 0;
Mx2 = 0;
My = 0;
My2 = 0;
WizX = 1;
WizY = 0;


}
-(void)MonsterMovment2{
float screen_width2;
float screen_height2;
float vers = [[[UIDevice currentDevice] systemVersion] floatValue];
UIInterfaceOrientation orient = [UIApplication                             sharedApplication].statusBarOrientation;
if (vers < 8 && UIInterfaceOrientationIsLandscape(orient))
 {
    screen_width2 = [[UIScreen mainScreen] bounds].size.width;
    screen_height2 = [[UIScreen mainScreen] bounds].size.height;
}
else
{
    screen_width2 = [[UIScreen mainScreen] bounds].size.width;
    screen_height2 = [[UIScreen mainScreen] bounds].size.height;
}


Monster2.center = CGPointMake(Monster2.center.x + Mx2,             Monster2.center.y + My2);

if (CGRectIntersectsRect(Monster2.frame, Wiz.frame)) {
  [self GameOver];

  }

if (Monster2.center.x < 10) {
    Mx2 = 1;
}

if (Monster2.center.x > screen_width2) {
    Mx2 = -1;
}

if (Monster2.center.y > screen_height2) {
    My2 = -1;

}

if (Monster2.center.y < 10) {

    My2 = 1;

}




}


-(void)MonsterMovment{
 float screen_width;
float screen_height;
float vers = [[[UIDevice currentDevice] systemVersion] floatValue];
UIInterfaceOrientation orient = [UIApplication     sharedApplication].statusBarOrientation;
if (vers < 8 && UIInterfaceOrientationIsLandscape(orient))
{
    screen_width = [[UIScreen mainScreen] bounds].size.width;
    screen_height = [[UIScreen mainScreen] bounds].size.height;
}
else
{
    screen_width = [[UIScreen mainScreen] bounds].size.width;
    screen_height = [[UIScreen mainScreen] bounds].size.height;
}


Monster.center = CGPointMake(Monster.center.x + Mx, Monster.center.y +     My);

if (CGRectIntersectsRect(Monster2.frame, Monster.frame)) {
    Mx = Mx;
    My=My;

}

if (CGRectIntersectsRect(Monster.frame, Wiz.frame)) {
    [self GameOver];

}


if (Monster.center.x < 10) {
    Mx = 1;
}

if (Monster.center.x > screen_width) {
    Mx = -1;
}

if (Monster.center.y > screen_height) {
    My = -1;

}

if (Monster.center.y < 10) {

    My = 1;

}


 }

-(void)GameOver{
StartGame.hidden = YES;
[WizMovment invalidate ];
Wiz.hidden = YES;
Exit.hidden = NO;
 StartGame.hidden = NO;


if (ScoreNumber > HighScoreNumber) {
    [[NSUserDefaults standardUserDefaults] setInteger:ScoreNumber   forKey:@"HighScoreSaved"];
    HighScore.text = [NSString stringWithFormat:@"High Score: %i",   ScoreNumber];
}

[self viewDidLoad];


 }

 -(void)Score{
ScoreNumber = ScoreNumber + 1;
ScoreLabel.text = [NSString stringWithFormat:@"Score: %i", ScoreNumber];

  }

-(void)PlaceFood{
float screen_width;
float screen_height;
float vers = [[[UIDevice currentDevice] systemVersion] floatValue];
UIInterfaceOrientation orient = [UIApplication     sharedApplication].statusBarOrientation;
 if (vers < 8 && UIInterfaceOrientationIsLandscape(orient))
{
    screen_height = [[UIScreen mainScreen] bounds].size.width;
    screen_width = [[UIScreen mainScreen] bounds].size.height;
}
else
{
    screen_width = [[UIScreen mainScreen] bounds].size.width;
    screen_height = [[UIScreen mainScreen] bounds].size.height;
}
int height = (int) screen_height;
int widht = (int) screen_width;

FoodX = arc4random() %height;
FoodX = FoodX - 25;
if (25 < FoodX) {
    FoodX = 25;
}
FoodY = arc4random() %widht;
FoodY = FoodY -25;
if (25 < FoodY) {
    FoodY = 25;
}

Food.center = CGPointMake(FoodX, FoodY);

[Food setFrame:CGRectMake(FoodX, FoodY, Food.frame.size.width,     Food.frame.size.height)];

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib
}

-(void)MoveLeft{
if (WizSideMovment == NO) {
    WizX = -1;
    WizY = 0;
    WizSideMovment = YES;
}
}

-(void)MoveRight{
if (WizSideMovment == NO) {
    WizX = 1;
    WizY = 0;
    WizSideMovment = YES;
}
}

-(void)MoveUp{
if (WizSideMovment == YES) {
    WizX = 0;
    WizY = -1;
    WizSideMovment = NO;
}
}

-(void)MoveDown{
if (WizSideMovment == YES) {
    WizX = 0;
    WizY = 1;
    WizSideMovment = NO;
}
}
-(void)WizMovment{
float screen_width;
float screen_height;
float vers = [[[UIDevice currentDevice] systemVersion] floatValue];
UIInterfaceOrientation orient = [UIApplication sharedApplication].statusBarOrientation;
if (vers < 8 && UIInterfaceOrientationIsLandscape(orient))
{
    screen_height = [[UIScreen mainScreen] bounds].size.width;
    screen_width = [[UIScreen mainScreen] bounds].size.height;
}
else
{
    screen_width = [[UIScreen mainScreen] bounds].size.width;
    screen_height = [[UIScreen mainScreen] bounds].size.height;
}



            Wiz.center = CGPointMake(Wiz.center.x + WizX, Wiz.center.y     + WizY);

            if (CGRectIntersectsRect(Wiz.frame, Food.frame)) {
                [self PlaceFood];
                [self Score];



}

if (Wiz.center.x < 1) {
    [WizMovment invalidate];
    [self performSelector:@selector(GameOver) withObject:self    afterDelay:0.5];
}

if (Wiz.center.x > screen_width - 25) {
    [WizMovment invalidate];
    [self performSelector:@selector(GameOver) withObject:self   afterDelay:0.5];
}

if (Wiz.center.y > screen_height - 25) {
    [WizMovment invalidate];
    [self performSelector:@selector(GameOver) withObject:self afterDelay:0.5];
}

if (Wiz.center.y < 25) {
    [WizMovment invalidate];
    [self performSelector:@selector(GameOver) withObject:self afterDelay:0.5];
}
}

-(IBAction)Exit:(id)sender{

[self reloadInputViews];
[self Restart];

}
-(IBAction)Start:(id)sender{
 int i = rand()%2+1;
NSLog(@"Random Number: %i", i);
if ( (i = 2)) {
    i = -1;
}
int w = rand()%2+1;
NSLog(@"Random Number: %i", w);
if ( (w = 2)) {
    w = -1;
}
int e = rand()%2+1;
NSLog(@"Random Number: %i", e);
if ( (e = 2)) {
    e = -1;
}
int r = rand()%2+1;
NSLog(@"Random Number: %i", r);
if ( (r = 2)) {
    r = -1;
}

Mx=i;
Mx2 =w;
My=e;
My2=r;
StartGame.hidden = YES;

Wiz.hidden= NO;
WizX = 1;
WizY = 0;

 Food.hidden = NO;

 WizMovment = [NSTimer scheduledTimerWithTimeInterval:0.007 target:self    selector:@selector(WizMovment) userInfo:nil repeats:YES];

MonsterMovment = [NSTimer scheduledTimerWithTimeInterval:0.007 target:self selector:@selector(MonsterMovment) userInfo:nil repeats:YES];

 MonsterMovment2 = [NSTimer scheduledTimerWithTimeInterval:0.007    target:self selector:@selector(MonsterMovment2) userInfo:nil repeats:YES];

[self PlaceFood];

/* int i = rand()%2+1;
NSLog(@"Random Number: %i", i);
if ( (i = 2)) {
    i = -1;
}
int w = rand()%2+1;
NSLog(@"Random Number: %i", w);
if ( (w = 2)) {
    w = -1;
}
int e = rand()%2+1;
NSLog(@"Random Number: %i", e);
if ( (e = 2)) {
    e = -1;
}
int r = rand()%2+1;
NSLog(@"Random Number: %i", r);
if ( (r = 2)) {
    r = -1;
}

Mx=i;
Mx2 =w;
My=e;
My2=r;

*/


}

- (void)viewDidLoad {

//[[UIScreen mainScreen] bounds];
Exit.hidden = YES;
HighScoreNumber = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScoreSaved"];
HighScore.text = [NSString stringWithFormat:@"High Score: %li", (long)HighScoreNumber];
[super viewDidLoad];
// Do any additional setup after loading the view.
Food.hidden = YES;
WizX = 1;
WizY = 0;
WizSideMovment = YES;


UISwipeGestureRecognizer *SwipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(MoveLeft)];
[SwipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:SwipeLeft];

UISwipeGestureRecognizer *SwipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(MoveRight)];
[SwipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:SwipeRight];

UISwipeGestureRecognizer *SwipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(MoveUp)];
[SwipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
[self.view addGestureRecognizer:SwipeUp];

UISwipeGestureRecognizer *SwipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(MoveDown)];
[SwipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
[self.view addGestureRecognizer:SwipeDown];

}

  • (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }

/*

pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */

@end

2

2 Answers

0
votes

Simply remove it, create new instance and add again.

0
votes

Just remove it and re-add it again from the parent will create it brand new, otherwise it's just a case of keeping defaults and reapplying them all on game over.