2014-12-20 3 views
1

간단한 SpriteKit 게임에 타이머를 추가하고 싶습니다. 기존 답변 ("Spritekit - 타이머 만들기")에서 제안 된 코드를 사용하면 5 개의 오류가 발생합니다 (아래 주석 참조). 어쩌면 다른 아이디어도있을 것입니다!SpriteKit - 타이머 추가하기

고맙습니다.

이 내하는 .m 파일입니다

다음 코드는 나에게 문제가 제공
@implementation GameScene { 
    BOOL updateLabel;  
    SKLabelNode *timerLabel; 
} 

int timer; 

- (id)initWithSize:(CGSize)size {  
    if (self = [super initWithSize:size]) { 

    self.backgroundColor = [SKColor whiteColor]; 

    timer = 0; 

    updateLabel = false; 

    timerLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"]; 
    timerLabel.text = @"0"; 
    timerLabel.fontSize = 48; 
    timerLabel.fontColor = [SKColor greenColor]; 
    timerLabel.position = CGPointMake(700,50); 
    [self addChild: timerLabel]; 

    } 
    return self; 
} 


-(void) didMoveToView:(SKView *)view { 

} 

:

id wait = [SKAction waitForDuration:1.0];//Redefinition of 'wait' as different kind of symbol 
id run = [SKAction runBlock:^{//Initializer element is not a compile-time constant 
    timer ++; 
    updateLabel = true; 
    }]; 
[node runAction:[SKAction repeatActionForever:[SKAction sequence:@[wait, run]]];//1. Expected identifier or '('; 2. Use of undeclared identifier 'node'; 3. Collection element of type 'pid_t (int *)' is not an Objective-C object 


-(void)update:(CFTimeInterval)currentTime { 

    if(updateLabel == true){ 
    timerLabel.text = [NSString stringWithFormat:@"%i",timer]; 
    updateLabel = false; 
    } 
} 

@end 

답변

0
int timer = 0; 
SKLabelNode timerLabel; 

- (id)initWithSize:(CGSize)size {  
if (self = [super initWithSize:size]) { 

SKAction *wait = [SKAction waitForDuration:1]; 
SKAction *performSelector = [SKAction performSelector:@selector(timerLabel) onTarget:self]; 
SKAction *removeChild = [SKAction runBlock:^{ 
    [timerLabel removeFromParent]; 
}]; 
SKAction *increaseTimer = [SKAction runBlock:^{ 
    timer++; 
}]; 
SKAction *sequence = [SKAction sequence:@[removeChild, performSelector, wait, increaseTimer]]; 
SKAction *repeat = [SKAction repeatActionForever:sequence]; 
[self runAction:repeat]; 

} 
return self; 
} 

-(void)timerLabel 
{ 
    timerLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"]; 
    timerLabel.text = [NSString stringWithFormat:@"%i", timer]; 
    timerLabel.fontSize = 48; 
    timerLabel.fontColor = [SKColor greenColor]; 
    timerLabel.position = CGPointMake(700,50); 
    [self addChild: timerLabel]; 
} 

시도 선택을 수행하여 전화를. 이게 효과가 있기를 바란다. 나는 시험하지 않았다.

+0

감사합니다. 그것은 작동하지만 다음 timerLabel은 이전을 덮어 씁니다. 끝에 녹색 자리가 있습니다. 시간을 읽을 수 없습니다. – George

+0

나는 나의 대답을 편집하고 그것이 작동하는지 테스트했다. –