简体   繁体   English

如何在SpriteKit中分解节点?

[英]How to dissolve nodes in SpriteKit?

I have 2 SKSpriteNode objects, and I want to crossfade them. 我有2个SKSpriteNode对象,并且想淡化它们。 One of the easiest way is to create a fadeOut SKAction and a fadeIn SKAction, and apply them to the SKSpriteNode objects. 最简单的方法之一是创建一个fadeOut SKAction和fadeIn SKAction,并将它们应用于SKSpriteNode对象。 But the problem of this approach is that during the action both of them have the alpha under 1.0, which looks not so good. 但是这种方法的问题在于,在操作过程中,它们两个的alpha值都小于1.0,这看起来不太好。

For example, I'd like to dissolve the red square SKNode to a green round SKNode 例如,我想将红色正方形的SKNode分解为绿色的圆形SKNode 开始结束

If just fade out the red square and fade in the green round during the action it'll look like this 如果在操作过程中淡出红色方块并淡入绿色轮,它将看起来像这样 褪色

So you can see background through these 2 objects. 这样您就可以通过这两个对象看到背景。 I hope to dissolve these 2 objects like this: 我希望像这样溶解这两个对象: 溶解

In UIKit I can use UIView.transitionWithView but in SpriteKit I only found a similar method for presenting scenes ( SKViewObject.presentScene: transition: ). 在UIKit中,我可以使用UIView.transitionWithView但在SpriteKit中,我仅发现了一种类似的场景呈现方法( SKViewObject.presentScene: transition: So is there anyway to make a dissolve transition for SKNodes? 那么,是否有必要为SKNode进行过渡转换?

There's little you can about the background coming through when adjusting the alpha settings on both nodes. 在两个节点上调整Alpha设置时,您几乎看不到背景。 A possible hack could be to temporarily insert a solid node in front of your background but behind both other nodes until the action is done. 可能的破解方法是在背景之前但在其他两个节点之后临时插入一个实心节点,直到操作完成。 At which point you remove it. 此时将其删除。

The node should be in the shape of both nodes cross-section (pardon the sloppy artwork on my part): 该节点的形状应为两个节点的横截面形状(请原谅我马虎的艺术品):

在此处输入图片说明

I searched a lot for this question and I nearly decided to use a shader to do this kind of dissolve (since you can directly edit the pixel in a shader), but then I found that there's an unusual way to solve this problem. 我在这个问题上进行了很多搜索,几乎决定使用着色器进行这种溶解(因为您可以直接在着色器中编辑像素),但是后来我发现有一种解决此问题的不寻常方法。 It may not be useful for every situation, but if your background doesn't do things like scroll or zoom, this approach may be the easiest. 它可能并非在每种情况下都有用,但是如果您的背景没有进行滚动或缩放之类的操作,则此方法可能是最简单的。 In simple words, just create a screenshot for the current screen and display it at the top, then change your node to the sprite you need, and at last fade out the screenshot you took. 简而言之,只需为当前屏幕创建一个屏幕截图并将其显示在顶部,然后将节点更改为所需的精灵,最后淡出您所拍摄的屏幕截图。

So at first you have to make sure all the nodes are in the correct node tree. 因此,首先必须确保所有节点都在正确的节点树中。 Then use SKViewObject.textureFromNode(rootNode) to create the screenshot, make a sprite node from this texture, and add it to your screen. 然后使用SKViewObject.textureFromNode(rootNode)创建屏幕快照,从该纹理创建一个sprite节点,并将其添加到屏幕。 Then, you can create the fade out SKAction to fade out this screenshot sprite. 然后,您可以创建淡出SKAction来淡出此屏幕快照精灵。 You may also remove it when the action ends. 您也可以在操作结束时将其删除。

Using this approach, during the fade out the screen will just look like this: 使用这种方法,在淡出期间,屏幕将如下所示: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM