Come ottenere uno sklabelnode eseguire una patta di attesa per l'aggiornamento dopo l'aggiornamento dopo che viene eseguita un'azione?

StackOverflow https://stackoverflow.com//questions/22051089

Domanda

Sto cercando di avere una ruota a colori che aggiorna lo sklabelnode al colore selezionato dopo che la ruota è filata.Lo Sklabelnode sta aggiornando a destra quando l'utente fa clic sulla ruota e ho provato a posizionare una pattina di attesa di attesa in un paio di punti diversi ma nulla è che lo sklabelnode attende fino a quando la ruota è finita.

#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
.

È stato utile?

Soluzione

Sembri chiamare l'azione di rotazione e il cambiamento del colore simultaneamente - solo perché queste due linee di codice vengono posizionate ed eseguite una dopo l'altra, non significa che il secondo aspetterà fino al termine del primo. .

prova ad aggiornare il colore in un blocco di completamento - questo garantirà che si chiama dopo aver effettuato la rotazione:

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

Ciò rende anche l'azione di attesa sul tuo SKLabelNode superfluo - puoi rimuoverlo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top