如何在延迟后重复代码

时间:2014-04-06 10:01:56

标签: objective-c hide repeat

我试图做一个你必须在洞穴中飞行的游戏,然后拿起一个能给你额外积分的物体。我希望奖金图像在播放时重复几次。 我曾尝试使用代码告诉我隐藏对象,但后来我再也看不到它了,但我仍然可以把它隐藏起来......

我想重复"电晕",但是如何?

请帮帮我:)。

这是我的.m文件:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


-(void)Collision{


if (CGRectIntersectsRect(Heli.frame, Obstacle.frame)) {
    [self EndGame];
}

if (CGRectIntersectsRect(Heli.frame, Obstacle2.frame)) {
    [self EndGame];
}

if (CGRectIntersectsRect(Heli.frame, Bottom1.frame)) {
    [self EndGame];
}

if (CGRectIntersectsRect(Heli.frame, Top1.frame)) {
    [self EndGame];
}





}


-(void)EndGame{


if (Scorenumber > HighScore) {
    HighScore = Scorenumber;
    [[NSUserDefaults standardUserDefaults] setInteger:HighScore    
forKey:@"HighScoreSaved"];
}


Heli.hidden = YES;
[timer invalidate];
[Scorer invalidate];


[self performSelector:@selector(NewGame) withObject: nil afterDelay:2];

}


-(void)NewGame{
if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
{
    Bottom1.hidden = YES;
    Top1.hidden = YES;
    Obstacle.hidden = YES;
    Obstacle2.hidden = YES;
    corona.hidden = YES;


    Intro1.hidden = NO;
    Intro2.hidden = NO;
    Intro3.hidden = NO;

    Heli.hidden = NO;
    Heli.center = CGPointMake(88, 286);
    Heli.image = [UIImage imageNamed:@"buss til app.png"];

    Start = YES;
    Scorenumber = 0;
    Score.text = [NSString stringWithFormat:@"Score: 0"];
    Intro3.text = [NSString stringWithFormat:@"HighScore: %i", HighScore];


}

else
{
    //ipad
    Bottom1.hidden = YES;
    Top1.hidden = YES;
    Obstacle.hidden = YES;
    Obstacle2.hidden = YES;
    corona.hidden = YES;

    Intro1.hidden = NO;
    Intro2.hidden = NO;
    Intro3.hidden = NO;

    Heli.hidden = NO;
    Heli.center = CGPointMake(153, 516);
    Heli.image = [UIImage imageNamed:@"buss til app.png"];

    Start = YES;
    Scorenumber = 0;
    Score.text = [NSString stringWithFormat:@"Score: 0"];
    Intro3.text = [NSString stringWithFormat:@"HighScore: %i", HighScore];
}
}






-(void)HeliMove{
if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
{

    [self Collision];

    Heli.center = CGPointMake(Heli.center.x, Heli.center.y + Y);

    Obstacle.center = CGPointMake(Obstacle.center.x -4, Obstacle.center.y);
    Obstacle2.center = CGPointMake(Obstacle2.center.x -4, Obstacle2.center.y);
    Bottom1.center = CGPointMake(Bottom1.center.x -4, Bottom1.center.y -0);
    Top1.center = CGPointMake(Top1.center.x -4, Top1.center.y -0);
    corona.center = CGPointMake(corona.center.x -4, corona.center.y );



    if (Obstacle.center.x < -90) {
        RandomPosition = arc4random() %200;
        RandomPosition = RandomPosition + 200;
        Obstacle.center = CGPointMake(400, RandomPosition);
    }


    if (Obstacle2.center.x < 0) {
        RandomPosition = arc4random() %165;
        RandomPosition = RandomPosition + 600;
        Obstacle.center = CGPointMake(300, RandomPosition);
    }


    if (Top1.center.x <-95) {
        RandomPosition = arc4random() %80;
        Top1.center = CGPointMake(410, 17);
        RandomPosition = RandomPosition + 495;
        Bottom1.center = CGPointMake(420, 540);
    }

    if (corona.center.x <-90) {
        corona.center = CGPointMake(2100, 325);
    }



}

else
{
    //ipad
    [self Collision];

    Heli.center = CGPointMake(Heli.center.x, Heli.center.y + Y);

    Obstacle.center = CGPointMake(Obstacle.center.x -10, Obstacle.center.y);
    Obstacle2.center = CGPointMake(Obstacle2.center.x -10, Obstacle2.center.y);
    Bottom1.center = CGPointMake(Bottom1.center.x -10, Bottom1.center.y -0);
    Top1.center = CGPointMake(Top1.center.x -10, Top1.center.y -0);
    corona.center = CGPointMake(corona.center.x -10, corona.center.y );


    if (Obstacle.center.x < -90) {
        RandomPosition = arc4random() %300;
        RandomPosition = RandomPosition + 400;
        Obstacle.center = CGPointMake(900, RandomPosition);
    }

    }


    if (Obstacle2.center.x < 0) {
        RandomPosition = arc4random() %300;
        RandomPosition = RandomPosition + 600;
        Obstacle.center = CGPointMake(678, RandomPosition);
    }


    if (Top1.center.x <-100) {
        RandomPosition = arc4random() %80;
        Top1.center = CGPointMake(400, 30);
        RandomPosition = RandomPosition + 495;
        Bottom1.center = CGPointMake(400, 990);
    }



    if (corona.center.x < -0) {
        corona.center = CGPointMake(900, 325);
    }



}




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


if (CGRectIntersectsRect(Heli.frame, corona.frame)) {
    Scorenumber += 20;
    corona.hidden = YES;

}
}










-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{



if (Start == YES) {

    Intro1.hidden = YES;
    Intro2.hidden = YES;
    Intro3.hidden = YES;

    timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self 
selector:@selector(HeliMove) userInfo:nil repeats:YES];

    Scorer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self   
selector:@selector(Scoring) userInfo:nil repeats:YES];


    Start = NO;

    Bottom1.hidden = NO;
    Top1.hidden = NO;
    Obstacle.hidden = NO;
    Obstacle2.hidden = NO;
    Obstacle3.hidden = NO;
    Obstacle4.hidden = NO;
    Obstacle5.hidden = NO;
    Obstacle6.hidden = NO;
    Obstacle7.hidden = NO;
    Obstacle8.hidden = NO;
    Obstacle9.hidden = NO;
    corona.hidden = NO;


    if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
    {


        RandomPosition = arc4random() %150;
        RandomPosition = RandomPosition + 397;
        Obstacle.center = CGPointMake(570, RandomPosition);


        RandomPosition = arc4random() %75;
        RandomPosition = RandomPosition + 259;
        Obstacle2.center = CGPointMake(855, RandomPosition);


        corona.center = CGPointMake(1400, 400);




    }

    else{
        //ipad
        RandomPosition = arc4random() %400;
        RandomPosition = RandomPosition + 1300;
        Obstacle.center = CGPointMake(570, RandomPosition);

        RandomPosition = arc4random() %75;
        RandomPosition = RandomPosition + 259;
        Obstacle2.center = CGPointMake(855, RandomPosition);


        RandomPosition = arc4random() %55;
        corona.center = CGPointMake(900, 470);

    }
}


if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
{
    Y = -7;
    Heli.image = [UIImage imageNamed:@"buss til app.png"];
}

else{
    //ipad
    Y = -12;
    Heli.image = [UIImage imageNamed:@"buss til app.png"];


}
}


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
{
    Y = 7;
    Heli.image = [UIImage imageNamed:@"buss til app.png"];
}

else{
    //ipad
    Y = 12;
    Heli.image = [UIImage imageNamed:@"buss til app.png"];

}

}





 - (void)viewDidLoad
{

// Get your highscore from the prefs.
HighScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"HighScoreSaved"]     
intValue ];
Intro3.text = [NSString stringWithFormat:@"HighScore: %i", HighScore];



Start = YES;
Bottom1.hidden = YES;
Top1.hidden = YES;
Obstacle.hidden = YES;
Obstacle2.hidden = YES;
Obstacle3.hidden = YES;
Obstacle4.hidden = YES;
Obstacle5.hidden = YES;
Obstacle6.hidden = YES;
Obstacle7.hidden = YES;
Obstacle8.hidden = YES;
Obstacle9.hidden = YES;
corona.hidden = YES;




[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

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

@end

1 个答案:

答案 0 :(得分:1)

要在延迟后重复一些代码,您可以使用NSTimer 创建viewDidLoad方法并将NSTimer初始化代码放在那里:

-(void)viewDidLoad
{
    [super viewDidLoad];
    //then initialize your timer
    NSTimer *myTimer=[NSTimer scheduledTimerWithTimeInterval:yourDelay
    target:self selector:@selector(yourSelector:) userInfo:nil repeats:YES];
}

您需要编辑的是:
yourDelay - 定时器点火之间的延迟,以秒为单位 repeats:YES - 定义计时器是否重复 @selector(yourSelector:) - 放置代码的方法

然后

-(void)yourSelector:(NSTimer*)timer
{
//your code here
}