2014-02-26 11 views
0

바퀴가 회전 된 후 선택한 색으로 SKLabelNode를 업데이트하는 컬러 휠이 있어야합니다. 사용자가 휠을 클릭 할 때 SKLabelNode가 오른쪽으로 업데이트되고 몇 개의 다른 지점에 SKAction waitForDuration을 놓으려고 시도했지만 휠 회전이 끝날 때까지 SKLabelNode가 대기하지 않습니다.SKLabelNode를 실행하는 방법 waitForDuration SKAction을 실행하여 작업 수행 후 업데이트 하시겠습니까?

#import "MyScene.h" 

@interface MyScene() 

@property (strong, nonatomic) SKSpriteNode *wheel; 
@property (strong, nonatomic) SKLabelNode *colorSelected; 
@property (strong, nonatomic) SKLabelNode *colorIndicator; 

@end 

@implementation MyScene 
{ 
BOOL *_wheelSpun; 
NSMutableArray *_colorNumberArray; 
NSMutableArray *_durationNumberArray; 
int randomSpinNumber; 
} 

-(id)initWithSize:(CGSize)size {  
if (self = [super initWithSize:size]) { 
    /* Setup your scene here */ 

    _wheel = [SKSpriteNode spriteNodeWithImageNamed:@"colorWheel"]; 
    _wheel.position = CGPointMake(self.size.width/2, self.size.height/2 + 70); 
    _wheel.xScale = .5; 
    _wheel.yScale = .5; 
    _wheel.name = @"wheel"; 

    [self addChild:_wheel]; 

    _colorSelected = [SKLabelNode labelNodeWithFontNamed:@"Apple Chancery"]; 
    _colorSelected.text = @"Click the wheel to spin the colors!"; 
    _colorSelected.position = CGPointMake(self.size.width/2, self.size.height/2 - 60); 
    _colorSelected.fontColor = [SKColor whiteColor]; 
    _colorSelected.fontSize = 16; 

    [self addChild:_colorSelected]; 

    _colorIndicator = [SKLabelNode labelNodeWithFontNamed:@"Apple Color Emoji"]; 
    _colorIndicator.text = @"⬇"; 
    _colorIndicator.position = CGPointMake(self.size.width/2, self.size.height/2 + 175); 
    _colorIndicator.fontSize = 16; 

    [self addChild:_colorIndicator]; 

    _wheelSpun = NO; 

    randomSpinNumber = arc4random()%4; 
    NSLog(@"%i spun", randomSpinNumber); 
} 
return self; 
} 

-(void)colorTextUpdate 
{ 
SKAction *wait = [SKAction waitForDuration:5.2]; 
[self runAction:wait]; 

if (randomSpinNumber == 0) { 
    _colorSelected.text = @"Blue Color Spun"; 
} 

if (randomSpinNumber == 1) { 
    _colorSelected.text = @"Orange Color Spun"; 
} 

if (randomSpinNumber == 2) { 
    _colorSelected.text = @"Yellow Color Spun"; 
} 

if (randomSpinNumber == 3) { 
    SKAction *wait = [SKAction waitForDuration:5.1]; 
    [self.colorSelected runAction:wait]; 
    _colorSelected.text = @"Red Color Spun"; 
} 
} 

-(void)wheelSpin 
{ 

_wheelSpun = YES; 

if (randomSpinNumber == 0) 
{ 
    SKAction *rotateWheel1 = [SKAction rotateByAngle:350 duration:5]; 
    [self.wheel runAction:rotateWheel1]; 
    [self colorTextUpdate]; 
} 

if (randomSpinNumber == 1) { 
    SKAction *rotateWheel2 = [SKAction rotateByAngle:204 duration:5]; 
    [self.wheel runAction:rotateWheel2]; 
    [self colorTextUpdate]; 
} 

if (randomSpinNumber == 2) { 
    SKAction *rotateWheel3 = [SKAction rotateByAngle:108 duration:5]; 
    [self.wheel runAction:rotateWheel3]; 
    [self colorTextUpdate]; 
} 

if (randomSpinNumber == 3) { 
    SKAction *rotateWheel4 = [SKAction rotateByAngle:420 duration:5]; 
    [self.wheel runAction:rotateWheel4]; 
    [self colorTextUpdate]; 
} 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
/* Called when a touch begins */ 

UITouch *touch = [touches anyObject]; 
CGPoint touchLocation = [touch locationInNode:self]; 

SKNode *node = [self nodeAtPoint:touchLocation]; 
if ([node.name isEqualToString:@"wheel"] && !_wheelSpun) { 
    [self wheelSpin]; 

} 
} 

@end 

답변

2

당신은 회전 동작과 동시에 색의 변화를 호출 할 것 - 코드의 두 라인을 배치하고 다른 후 하나를 실행해서, 그것은 두 번째는 첫 번째 때까지 기다리는 것을 의미하지 않는다 은 끝났어.

이 완료 블록에 색을 업데이트 시도 -이 회전이 완료된 후이 호출되도록합니다

[self.wheel runAction:rotateWheel1 completion:^{ 
    [self colorTextUpdate]; 
}]; 

이 또한 SKLabelNode 불필요한에서 대기 작업을 만든다 - 당신은 그것을 제거 할 수 있습니다.