SpriteKit:如何在教程中突出显示场景的一部分?

时间:2015-03-10 19:01:45

标签: sprite-kit skspritenode sktexture

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

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

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

实现这一目标的一种方法是通过使用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;
相关问题