【问题标题】:Change background colours of a SKscene after it has been presented using a timer使用计时器呈现后更改 SKscene 的背景颜色
【发布时间】:2014-02-02 02:48:22
【问题描述】:

所以我在我正在测试的应用程序的 Skview 中设置了一个 skscene。我像这样设置背景颜色等。

    SKView *skBG = [[SKView alloc] initWithFrame:self.view.bounds];
    SKScene *scene = [SKScene sceneWithSize:self.view.bounds.size];

    // Set the scale mode to scale to fit the window
    scene.backgroundColor = [SKColor colorWithRed:0.12f green:0.2f blue:0.27f alpha:1.0f];
    scene.scaleMode = SKSceneScaleModeAspectFit;
    [skBG presentScene:scene];

    NSString *myParticlePath = [[NSBundle mainBundle] pathForResource:@"MyParticle" ofType:@"sks"];
    SKEmitterNode *snowParticle = [NSKeyedUnarchiver unarchiveObjectWithFile:myParticlePath];
    snowParticle.particlePosition = CGPointMake(160, 284);

    scene addChild:snowParticle];
    [self.view addSubview:skBG];

然后我想在已经呈现的场景上随机更改背景背景颜色。 我附加了一个标准计时器,在 5 秒后被调用,并在其中执行如下操作:

self.scene.backgroundColor = [SKColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1.0f];

但它不更新?我尝试用“[skView presentScene:scene];”来表示场景但不起作用。

【问题讨论】:

  • 发布定时器设置代码
  • 你能展示一下改变颜色的完整方法吗?它指的是property .scene,但您上面发布的代码仅指的是同名的本地SKScene 对象...
  • 您希望它每 5 秒随机更改一次,还是在它首次出现后仅 5 秒随机更改一次?
  • 每 5 秒 @macshome

标签: ios sprite-kit skscene skview


【解决方案1】:

嗯,没什么好说的,但从您实际分享的内容来看,您可能实际上并没有显示 self.scene 属性。在您设置 SKView 的代码中:

SKScene *scene = [SKScene sceneWithSize:self.view.bounds.size];

但这与 .scene 属性不同(除非您在方法内的某处执行self.scene = scene;)。根据我们得到的少量信息,我猜你要么没有显示 self.scene 属性,要么是用新的瞬间覆盖它。如果情况确实如此,那么它应该像改变你所要做的一样简单:

SKView *skBG = [[SKView alloc] initWithFrame:self.view.bounds];
_scene = [SKScene sceneWithSize:self.view.bounds.size]; 

// Set the scale mode to scale to fit the window
self.scene.backgroundColor = [SKColor colorWithRed:0.12f green:0.2f blue:0.27f alpha:1.0f];
self.scene.scaleMode = SKSceneScaleModeAspectFit;
[skBG presentScene:self.scene];

NSString *myParticlePath = [[NSBundle mainBundle] pathForResource:@"MyParticle" ofType:@"sks"];
SKEmitterNode *snowParticle = [NSKeyedUnarchiver unarchiveObjectWithFile:myParticlePath];
snowParticle.particlePosition = CGPointMake(160, 284);

self.scene addChild:snowParticle];
[self.view addSubview:skBG];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-30
    • 2014-08-21
    • 2018-10-13
    • 2011-06-22
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多