繁体   English   中英

当我启动应用程序时,如何将xcode中的视图控制器重置为其状态

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

当我启动应用程序时,如何将xcode中的视图控制器重置为其状态

@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]; //处理可以重新创建的任何资源。 }

/ *

pragma mark - 导航

//在基于故事板的应用程序中,您经常需要在导航之前做一些准备 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {//使用[segue destinationViewController]获取新的视图控制器。 //将所选对象传递给新视图控制器。 } * /

@结束

只需删除它,创建新实例并再次添加。

只需删除它并从父母那里重新添加它将创建它全新,否则它只是保持默认值并在游戏结束时重新应用它们的情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM