Question

I'm trying do make a game where you have to fly through a cave, and pick up an object that gives you extra points. I want the bonus image to repeat several times while playing. I have tried using a code who tell me to hide the object, but then I can't see it anymore, but I can still pick it up while it's hidden...

I thought about repeating the "corona", but how?

Please help me :)

Here is my .m file:

#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
Was it helpful?

Solution

To repeat some code after delay you can use NSTimer.
Create the viewDidLoad method and place your NSTimer initialize code there:

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

What do you need to edit is:
yourDelay - delay between timer firings, in seconds
repeats:YES - defines whether timer repeats or not
@selector(yourSelector:) - method to place your code

Then

-(void)yourSelector:(NSTimer*)timer
{
//your code here
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top