您好我在其didMoveToView函数中设置我的SKScene的backgroundColor属性,如下所示:
self.backgroundColor = [SKColor colorWithRed:0.2 green:0.2 blue:1.0 alpha:1.0];
但背景颜色始终是默认灰色。我有另一个简单的SpriteKit测试项目,并以相同的方式设置背景颜色。
我已从场景中删除了所有节点,以排除背景被叠加的可能性。
你知道什么可能阻止我的背景颜色出现吗?
感谢。
答案 0 :(得分:0)
我刚测试了这个,它对我来说是预期的。
这是我的初始场景,HelloScene.m
#import "HelloScene.h"
#import "NewScene.h"
@interface HelloScene ()
@property BOOL contentCreated;
@end
@implementation HelloScene
- (void)didMoveToView:(SKView *)view
{
if (!self.contentCreated) {
[self createSceneContents];
self.contentCreated = YES;
}
}
- (void)createSceneContents
{
self.backgroundColor = [SKColor colorWithRed:0.333 green:0.780 blue:0.961 alpha:1];
self.scaleMode = SKSceneScaleModeAspectFit;
[self addChild:[self newHelloNode]];
}
- (SKLabelNode *)newHelloNode
{
SKLabelNode *helloNode = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
helloNode.name = @"helloNode";
helloNode.text = @"Hello !";
helloNode.fontSize = 42;
helloNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
return helloNode;
}
#if TARGET_OS_IPHONE
#pragma mark - Event Handling - iOS
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self runHelloNodeAction];
}
#else
#pragma mark - Event Handling - OS X
- (void)mouseDown:(NSEvent *)event
{
[self runHelloNodeAction];
}
#endif
- (void)runHelloNodeAction
{
SKNode *helloNode = [self childNodeWithName:@"helloNode"];
if (helloNode != nil) {
helloNode.name = nil;
SKAction *moveUpAndZoom = [SKAction group:@[[SKAction moveByX:0.0 y:100.0 duration:0.5],
[SKAction scaleTo:1.5 duration:0.5]]];
SKAction *fadeAway = [SKAction fadeOutWithDuration:0.25];
SKAction *remove = [SKAction removeFromParent];
SKAction *moveSequence = [SKAction sequence:@[moveUpAndZoom, fadeAway, remove]];
[helloNode runAction:moveSequence completion:^{
SKScene *newScene = [[NewScene alloc] initWithSize:self.size];
SKTransition *doors = [SKTransition doorsOpenVerticalWithDuration:0.5];
[self.view presentScene:newScene transition:doors];
}];
}
}
@end
这是我的第二个场景,NewScene.m
#import "NewScene.h"
@interface NewScene ()
@property BOOL contentCreated;
@end
@implementation NewScene
- (void)didMoveToView:(SKView *)view
{
if (!self.contentCreated) {
[self createSceneContents];
self.contentCreated = YES;
}
}
- (void)createSceneContents
{
self.backgroundColor = [SKColor purpleColor];
self.scaleMode = SKSceneScaleModeAspectFit;
}
@end