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



我正在尝试在游戏中创建一个教程,该教程通过UI的某些部分并突出显示它们,同时使场景的其余部分变暗。

我想我可以用Sprites和SKBlendMode来做到这一点,但这些在苹果参考指南中解释得很差。

知道吗?

实现此目的的一种方法是使用 SKSpriteNodes 组合您想要的"cookie",然后创建一个纹理以将其"按原样"渲染到新的 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;

相关内容

最新更新