SpriteKit:你如何突出场景的一部分,就像在教程中一样?

SpriteKit: How do you highlight a section of the scene, like in a tutorial?

我正在尝试在游戏中创建一个教程,它会遍历 UI 的部分内容并突出显示它们,同时调暗场景的其余部分。

我想我可以用 Sprites 和 SKBlendMode 来做,但是这些在苹果参考指南中解释得不好。

有什么想法吗?

实现此目的的一种方法是将所需的 'cookie' 与 SKSpriteNode 组合,然后创建纹理以将其渲染 'as is' 到新的 SpriteNode,然后将其作为一个整体与场景融合.

在这个简单的示例中,我使用了一个矩形来突出显示,但您可以将该节点设为任何节点类型或图像,并相应地调整 alpha 值。

正常绘制场景,然后添加此代码:

// dim the entire background of the scene to 70% darker
SKSpriteNode* background = [SKSpriteNode spriteNodeWithColor:[UIColor colorWithRed:0
                                                                             green:0
                                                                              blue:0
                                                                             alpha:0.7]
                                                        size:self.frame.size];

// make a square of 100,100. This could be an image or shapenode rendered to a spritenode
// make the cut out only dim 20% - this is because no dim will look very harsh
SKSpriteNode* cutOut = [SKSpriteNode spriteNodeWithColor:[UIColor colorWithRed:0
                                                                         green:0
                                                                          blue:0
                                                                         alpha:0.2]
                                                    size:CGSizeMake(100,100)];

// add the cut out to the background and make the blend mode replace the colors
cutOut.blendMode = SKBlendModeReplace;
[background addChild:cutOut];

// we now need to make a texture from this node, otherwise the cutout will replace the underlying
// background completely

SKTexture* newTexture = [self.view textureFromNode:background];
SKSpriteNode* newBackground = [SKSpriteNode spriteNodeWithTexture:newTexture];

// position our background over the entire scene by adjusting the anchor point (or position)
newBackground.anchorPoint = CGPointMake(0,0);
[self addChild:newBackground];

// if you have other items in the scene, you'll want to increaes the Z position to make it go ontop.
newBackground.zPosition = 5;