Goal c - Animate SKScene Background Color

How could you animate SKScene's background color? I tried the UIView animation, but it is not surprising that this did not work. Is there an equivalent for this in the Sprite-Kit?

I am looking for something similar, but for the Sprite-Kit:

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

At the moment, when I am working on a UIView on SKView, I would like something more flexible.

I'm relatively new to the Sprite-Kit, so I apologize if this is very easy to do!

At the moment I have:

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

Also, after initializing SKView, I set the color of sprite bg depending on the value of NSUserDefault.

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

Thanks!

+2
source share
1 answer

Well, I came up with a completely redesigned solution! I have an array of background sprites, and I clone the original sprite and change its color, and then animate it.

Here is my 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]; } } 
+1
source

All Articles