Ios 如何每半秒钟更改一次精灵图像?

Ios 如何每半秒钟更改一次精灵图像?,ios,objective-c,sprite-kit,skspritenode,Ios,Objective C,Sprite Kit,Skspritenode,从今天早上开始,我一直在努力实现这一目标,但还没有找到答案。我有一个SKSpriteNode,它是一个在主屏幕上显示我的游戏名称的图像。我试图实现的是每半秒钟更改一次这些图像。我已经用不同的颜色制作了多张这个名字的图片,所以我可以每半秒钟更改一次图片。这将产生像典型的街机游戏一样更改标题颜色的效果。这就是我到目前为止所做的 - (void)viewDidLoad { [super viewDidLoad]; _images = [NSArray arrayWithObjects

从今天早上开始,我一直在努力实现这一目标,但还没有找到答案。我有一个SKSpriteNode,它是一个在主屏幕上显示我的游戏名称的图像。我试图实现的是每半秒钟更改一次这些图像。我已经用不同的颜色制作了多张这个名字的图片,所以我可以每半秒钟更改一次图片。这将产生像典型的街机游戏一样更改标题颜色的效果。这就是我到目前为止所做的

- (void)viewDidLoad
{
    [super viewDidLoad];

    _images = [NSArray arrayWithObjects:@"YellowLabel.png", @"BlueLabel.png", @"GreenLabel.png", @"RedLabel.png", @"WhiteLabel.png", nil];

    NSTimer *myTimer = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(doAnimation) userInfo:nil repeats:YES];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;

    // Create and configure the scene.
    SKScene * scene = [TitleScene sceneWithSize:skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;

    SKSpriteNode *labelNode = [SKSpriteNode spriteNodeWithImageNamed:@"WhiteLabel.png"];
    labelNode.position = CGPointMake(160, 400);

    // Present the scene.
    [skView presentScene:scene];
    [self doAnimation];
    [scene addChild:labelNode];
}
我还有一个额外的方法:

-(void)doAnimation {
    SKSpriteNode *labelNode = [SKSpriteNode spriteNodeWithImageNamed:@"WhiteLabel.png"];
    static int counter = 0;
    if ([_images count] == counter+1) {
        counter = 0;
    }
    labelNode = [SKSpriteNode spriteNodeWithImageNamed:[_images objectAtIndex:counter]];
}

谢谢你的帮助

使用
SKAction
有一种方法可以做到这一点:

SKAction *actionWait = [SKAction waitForDuration:.5];
SKAction *actionBlock = [SKAction runBlock:^(void)
{
    // do whatever you want to do every half second here
}];

SKAction *actionSequence = [SKAction sequence:@[actionWait, actionBlock]];
SKAction *actionRepeat = [SKAction repeatActionForever:actionSequence]
[self runAction:actionRepeat];
另一种方式可能是这样的,因为我认为您只是在为纹理设置动画:

// create an NSArray called anim that includes all your SKATextures

SKAction *actionAnimate = [SKAction animateWithTextures:anim timePerFrame:.5 resize:YES restore:NO];
SKAction *actionRepeat = [SKAction repeatActionForever:actionAnimate];
[self runAction:actionRepeat];

为什么在粘贴代码时会立即显示使用未声明的标识符?哪一行?可能是输入错误,但这不是您可以在不考虑自己实现的情况下剪切/粘贴的内容。这是一个例子。你用你的纹理创建了一个NSArray吗?如果您没有按照我的代码注释所说的那样做,那么很可能会导致错误:“创建一个名为anim的NSArray,其中包含所有Skatexture”太棒了!正在取得进展!现在,在最后一行的第二个方法中,is表示[self-runAction:actionRepeat];它说没有可见的@接口。我是否应该将其更改为代码中可能包含的其他内容?代码在哪里?听起来您的ViewController中有它。代码需要出现在场景中。此外,您还应该查看SKAction的参考资料,了解如何使用它们的详细信息。它们是SpriteKit的一个重要方面,其用途远远不止于此。