【发布时间】:2016-01-05 14:57:26
【问题描述】:
我正在使用 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 在游戏中提供的原始颜色是个好主意吗?
【问题讨论】:
-
您是否尝试使用预定义的颜色集来随机化背景颜色,或者没有预定义的颜色并且在随机化时允许所有颜色?或者您正在尝试随机化背景图像(更改纹理)?如果您的图像只是单色,则无需为此使用纹理。只需更改 backgroundColor 属性。
-
@Whirlwind 那里只有预定义的一组颜色,但是是的,我想用图像而不是颜色设置背景,图像是单色的,但是 Sprite 套件不提供我需要的那种颜色跨度>
-
你错了。您可以创建任何您想要的颜色。您只需为红色、绿色、蓝色和 alpha 分量提供正确的值。让我为你写一个例子。
-
@Whirlwind 是的,如果你这样做就好了