ViewControllerと、NSObjectからサブクラス化されたGameControllerクラスがあります。ビューコントローラには1つのボタンがリンクされており、GameControllerクラスを開始するIBActionを起動します。GameControllerクラスのinitには、scoreという名前のintに1を追加するCADisplayLinkがあります。デバッガーではスコアが上がりますが、ラベルには0しか表示されません。
ViewControllerで
-(void)setScore:(NSInteger)gameScore{
NSString *string = [NSString stringWithFormat:@"%i", gameScore];
scoreLabel.text = string;
NSLog(@"the score is %i", gameScore);
}
-(IBAction)play:(id)sender{
gameController = [[GameController alloc] init];
//in my header is GameController* gameController;
}
GameControllerで
-(id)init{
self = [super init];
if (self) {
viewController = [[ViewController alloc] init];
//in header ViewController* viewController;
displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(gameLoop:)];
[displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode: NSRunLoopCommonModes];
//in the header CADisplayLink* displayLink;
}
return self;
}
-(void)gameLoop:(CADisplayLink *)sender{
deltaScore++;
if (deltaScore >= 20) {
deltaScore -= 20;
score++;
//deltaScore and score are NSIntegers
[viewController setScore:score];
}
}