如何每隔半秒更改一次雪碧图像?

时间:2014-07-24 01:21:49

标签: ios objective-c sprite-kit skspritenode

我一直在努力从今天早上实现这一目标,但还没有弄明白。我有一个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]];
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

以下是使用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];