新游戏开始时改变背景c

时间:2016-01-05 14:57:26

标签: objective-c sprite-kit

我用Sprite Kit做了一个简单的游戏,我希望每次游戏开始时改变背景,例如,当新游戏开始时,背景为蓝色,但当英雄死亡并开始新游戏背景为黄色时。在此之前,我制作了img的SKTextureAtlas,当英雄死亡时闪现,也许我可以改变一些代码,所以它符合我的需要? 这里是我的代码示例:

    SKTextureAtlas *atlas =[SKTextureAtlas atlasNamed:@"back"];
    SKTexture *back0 = [atlas textureNamed:@"back0@2x"];
    SKTexture *back1 = [atlas textureNamed:@"back1@2x"];
    SKTexture *back2 = [atlas textureNamed:@"back2@2x"];
    SKTexture *back3 = [atlas textureNamed:@"back3@2x"];
  NSArray *backatlas = @[back0, back1, back2, back3];
    SKAction *backatlasanimation = [SKAction animateWithTextures:backatlas timePerFrame:0.1];
    SKAction *wait = [SKAction waitForDuration:0.1];
    SKAction *backaction = [SKAction sequence:@[wait, backatlasanimation]];
    [background runAction:backaction];
    [self addChild:background];

好吧,我做了我的研究并想出了这个:

-(NSarray *)background{
if(!_background){ 
_background = @[[SKColor redColor], [SKColor yellowColor], [SKColor grayColor]];
}
return _background ;
}
//// Add some random, so color change.
-(SKColor *) randomColorBack {
int index = arc4random() % [self.backgrounds count];
return self.backgrounds[index];
} 
......
background.color = [self randomColor];
background.colorBlendFactor = 1.0;

但如何使用xcode在游戏中提供的原始颜色来处理图像,这是一个好主意吗?

1 个答案:

答案 0 :(得分:1)

如果您有预定义的颜色集,则必须使用您喜欢的图像编辑器找出所需颜色的每个组件(红色,绿色,蓝色,alpha)。因此,如果您打开Photoshop,并使用其颜色选择工具,您最终会看到类似

的内容

R :123, B :234, G :125

这些代表所选颜色的每个颜色分量的值。

为了制作具有指定颜色成分的颜色,您可以使用一些UIColor类方法并根据文档, + colorWithRed:green:blue:alpha:对此非常完美。它接受从0到1的值,所以这就是你应该如何创建颜色:

Update DBA.user_data                                                     
set user_data.date_Sent = '12/16/2015'                                                      
where user_data.caseid in (select caseid                                                     
                       from DBA.cases
                       where cases.caseid=user_data.caseid
                       And cases.caseid in (select * FROM    'C:\\example\\test.csv' WITH (FIELDTERMINATOR = ',',ROWTERMINATOR = '\n')));

不是真的需要,但here你可以看到一个方便的方法,接受0到255之间的值,所以一切都可能更容易使用:

那就是它。只需使用这些预定义的颜色填充一些数组,然后选择一个随机颜如果您想要随机化颜色(没有预定义的颜色集),请使用:

UIColor* customColor = [UIColor colorWithRed:(120/255.0f) green:(23/255.0f) blue:(32/255.0f) alpha:1.0f];