目标c - 动画SKScene的背景颜色

时间:2014-02-09 18:59:05

标签: ios objective-c animation sprite-kit

您如何制作SKScene的背景色动画?我尝试了UIView动画,但不出意外它没有用。在Sprite-Kit中是否有相同的功能?

我正在寻找类似的东西,但对于Sprite-Kit:

[UIView animateWithDuration:0.25 animations:^{
   self.backgroundColor = [UIColor redColor];
}];

目前,作为一种解决方法,我已经在SKView上覆盖了UIView,但我希望更灵活。

我对Sprite-Kit比较陌生,所以请注意这是非常简单的事情!

目前我有:

-(id) initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
        _bg = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithRed:0.13 green:0.13 blue:0.13 alpha:1] size:self.size];
        _bg.position = CGPointMake(self.size.width/2, self.size.height/2);
        [self addChild:_bg];
    }
    return self;
}

-(void) colorise :(UIColor*)color {
    [_bg runAction:[SKAction colorizeWithColor:color colorBlendFactor:_bg.colorBlendFactor duration:1]];
}

在初始化SKView之后,我根据NSUserDefault值设置bg精灵的颜色。

 if ([[NSUserDefaults standardUserDefaults] integerForKey:@"currGameMode"] == 0) {
((bubbleAnimation2*)_bubbleEffectView.scene).bg.color = [UIColor colorWithRed:0.13 green:0.13 blue:0.13 alpha:1];}
else {((bubbleAnimation2*)_bubbleEffectView.scene).bg.color = [UIColor colorWithRed:0.25 green:0.13 blue:0.13 alpha:1];}

谢谢!

1 个答案:

答案 0 :(得分:1)

好吧,我提出了一个完全过度设计的解决方案!我有一系列背景精灵,我克隆原始精灵并改变它的颜色,然后将其设置为动画。

这是我的代码:

-(void) colorise :(UIColor*)color {
   // [_bg runAction:[SKAction colorizeWithColor:color colorBlendFactor:_bg.colorBlendFactor duration:1]];
    if ([_bgObjects count] != 0) {
        SKSpriteNode* newBg = [[_bgObjects objectAtIndex:0] copy];
        newBg.color = color;
        newBg.alpha = 0;
        [self insertChild:newBg atIndex:1];
        [newBg runAction:[SKAction fadeAlphaTo:1 duration:0.5]];
        [_bgObjects addObject:newBg];

        for (int i = 0; i < ([_bgObjects count]-1); i++) {
            [[_bgObjects objectAtIndex:i] runAction:[SKAction fadeAlphaTo:0 duration:0.5]];
        }

    }
}

-(void) update:(NSTimeInterval)currentTime {
    if ([_bgObjects count] > 1) {

    NSMutableArray* toDelete = [NSMutableArray arrayWithObjects: nil];

    for (SKSpriteNode* bg in _bgObjects) {
        if ((bg.alpha == 0) && !bg.hasActions) {
            [bg removeFromParent];
            [toDelete addObject:bg];
        }} [_bgObjects removeObjectsInArray:toDelete];
    }
}