ios - 如何让 SKLabelNode 在执行操作后运行 waitForDuration SKAction 进行更新?

标签 ios sprite-kit skaction sklabelnode

我正在尝试使用色轮将 SKLabelNode 更新为色轮旋转后选择的颜色。当用户单击滚轮时,SKLabelNode 正在更新,我尝试在几个不同的位置放置 SKAction waitForDuration,但 SKLabelNode 没有等待直到滚轮完成旋转。

#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

最佳答案

你好像是在同时调用旋转 Action 和颜色变化——只是因为这两行代码是依次放置和执行的,并不意味着第二行会等到第一行完成。

尝试更新完成 block 中的颜色 - 这将确保在旋转完成后调用它:

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

这也使您的 SKLabelNode 上的等待操作变得多余 - 您可以将其删除。

关于ios - 如何让 SKLabelNode 在执行操作后运行 waitForDuration SKAction 进行更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22051089/

相关文章:

ios - UIView 部分在 superView 之外没有接收到触摸

sprite-kit - 当仅需要接触检测时,我可以使用 SKAction 移动动态物理体吗?

ios - 如何让UITableView快速加载数据

javascript - 将通知导航到它属于 react-native 的屏幕

ios - 如何在 MKMapView iOS 中显示多个注释?

sprite-kit - 如何让SKSpriteNode在崎岖不平的地形上顺利行走?

swift - 如何在SpriteKit中检测远程点击并传递节点信息

swift - 为什么我的 SKSpriteNodes 不会出现在场景中?

iOS Sprite 套件 : How to make Sprite follow Touches

ios - 在应用程序状态保存期间解决 SKAction 代码块编码限制的好方法是什么?