Question

I would like to setup my Game if the button was pressed for the first time. Or a other option is, to setup the Game if the points are above 0. How ever, I want to do this, but I don´t know how. Can you help me out?

BTW: I´ve just began programming with this app and I would love it if you could already integrate the solution in my code..

XCode Project -> http://techstern.de/app/KlickMich.zip
Screenshot -> http://techstern.de/app/screenshot2.png

Translation of german words:
Zeit -> Time
Punktestand -> Score
KlickMich -> TapMe
Die Zeit ist um -> Time is up
Du hast 12 Punkte erzielt -> You´ve scored 12 points
Auf Facebook teilen -> Share on Facebook
Auf Twitter teilen -> Share on Twitter

ViewController.h

#import <UIKit/UIKit.h>
#import "ViewController.m"

@interface ViewController : UIViewController<UIAlertViewDelegate> {
IBOutlet UILabel *scoreLabel;
IBOutlet UILabel *timerLabel;

NSInteger count;
NSInteger seconds;
NSTimer *timer;
}

- (IBAction)buttonPressed;
- (void)setupGame;
- (void)subtractTime;
- (void)postToTwitter;
- (void)postToFacebook;

@end

ViewController.m

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setupGame];
}

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

- (IBAction)buttonPressed{
count++;

scoreLabel.text = [NSString stringWithFormat:@"Punktestand: %li",(long)count];

}

- (void)setupGame{
seconds = 15;
count = 0;

timerLabel.text = [NSString stringWithFormat:@"Zeit: %li",(long)seconds];
scoreLabel.text = [NSString stringWithFormat:@"Punktestand: %li",(long)count];

timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                         target:self
                                       selector:@selector(subtractTime)
                                       userInfo:nil
                                        repeats:YES];

}

- (void)subtractTime{
seconds--;
timerLabel.text = [NSString stringWithFormat:@"Zeit: %li",(long)seconds];

if (seconds == 0) {
[timer invalidate];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Die Zeit ist um"
                                                    message:[NSString stringWithFormat:@"Du hast %li Punkte erzielt",(long)count]
                                                   delegate:self
                                          cancelButtonTitle:@"Nochmal spielen"
                                          otherButtonTitles:@"Auf Facebook teilen", @"Auf Twitter teilen", nil];

[alert show];
} 

}

[…]

Yours faithfully
Robin

Was it helpful?

Solution

Try this one.

Declare your [self setupGame]; method in buttonPressed action instead of - (void)viewDidLoad like below.

 - (IBAction)buttonPressed
 {
   if (count == 0)
   {
      [self setupGame];
   }
   count++;
   scoreLabel.text = [NSString stringWithFormat:@"Punktestand: %li",(long)count];
 }

And if your alertview is coming and you cancel alertview or after share in twitter, your game will reset not waiting for button click. If you want you can change also. no problem :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top