目标C - 动画SKScene的背景颜色 [英] Objective c - Animate background colour of SKScene

查看:186
本文介绍了目标C - 动画SKScene的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您会如何去动画的SKScene的背景颜色?我试过一个UIView动画,但并不令人惊讶它没有工作。是否有一个相当于雪碧-Kit的做到这一点?

How would you go about animating the background colour of an SKScene? I've tried a UIView animate, but not surprisingly it didn't work. Is there an equivalent to do this in Sprite-Kit?

我在寻找这样的事情,但对于雪碧套件:

I'm looking for something like this, but for Sprite-Kit:

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

目前,由于周围的工作我都叠加在SKView一个UIView,但我喜欢的东西更灵活。

At the moment, as a work around I have overlayed a UIView over the SKView, but I would like something more flexible.

我是比较新的Sprite的套件,所以appologies如果这是做非常简单!

I am relatively new to Sprite-Kit, so appologies if this is extremely simple to do!

目前我有:

-(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精灵的颜色。

Also after initialising the SKView, I'm setting the color of the bg sprite depending on an NSUserDefault Value.

 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];}

谢谢!

推荐答案

好吧,我想出了一个完全过去设计的解决方案!我有背景精灵数组,我克隆原来精灵和改变它的颜色,然后在制作动画。

Well, I came up with a completely over-engineered solution! I have an array of background sprites and I clone the original sprite and change it's color and then animate it in.

下面是我的code:

-(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];
    }
}

这篇关于目标C - 动画SKScene的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆