How to dissolve nodes in SpriteKit? - ios

I have 2 SKSpriteNode objects, and I want to crossfade them.
One of the easiest way is to create a fadeOut SKAction and a fadeIn SKAction, and apply them to the SKSpriteNode objects. 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.
For example, I'd like to dissolve the red square SKNode to a green round 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:). So is there anyway to make a dissolve transition for SKNodes?

There's little you can about the background coming through when adjusting the alpha settings on both nodes. 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. Then, you can create the fade out SKAction to fade out this screenshot sprite. You may also remove it when the action ends.
Using this approach, during the fade out the screen will just look like this:

Related

How can I make the intersection of two SKSpriteNodes transparent

I added a background SKSpriteNode as a child to my SKScene. This node itself has some children nodes. Each child has some colored pixels as well as totally transparent pixels. The children nodes are moving across the screen and sometimes intersect each other.
I'd like to make the intersecting colored areas of the children fully transparent as they move so that the background sprite shows through. For the non-intersecting areas of each child, the node should appear as normal. I tried playing around with the blendmode but couldn't get the desired effect. Any ideas how to do this? Or, is there a way to do this outside of SpriteKit?
Thanks
I don't think there are commands to achieve what you want. You can change the the entire texture/image of a sprite, you can change part of a sprite's texture/image appearance by using a filter but you cannot specifically modify part of a sprite's texture/image. To be more specific, a sprite's alpha property applies to the entire sprite and not just part of it.

How to partially animate sprite in Sprite Kit?

I have a large sprite. I want to animate parts of it. Obviously I don't want 30 images of said sprite, I want only the difference, that light blinking or something.
How do I handle that? So far using many images for sprite animations has been way to go for me.
A SKSpriteNode can have children, so you can overlay as many as you like on top of a given SKSpriteNode.
For example if you had a christmas tree, you could put 20 lights on the tree by just adding a child SKSpriteNode for each light and positioning it appropriately. You could then have each of those lights animate independently and dynamically.
You can create you sprite with something like layering.
In practice it will be looking like sprites (that will be animated separated) under 'big' sprite that you do not want to animate.

How to apply full-screen SKEffectNode for post-processing in SpriteKit

I'm trying out SpriteKit with the following setup:
An SKScene with two child nodes used merely for grouping other
nodes: foreground and background.
background is really empty as of now, but would eventually hold some
type of background sprite / layers.
foreground is a SKEffectNode and whenever the user taps on the
screen, a new intance of a SKnode subclass which represents a game
element is added as child to it.
This SKNode subclass basically creates 3 SKShapeNodes and two labels: an outter
circumference, an inner circumference, 2 labels and an inner quarter circumference. The inner quarter circumference has an SKAction that
makes it rotate forever about its origin / center.
Now here's the issue, as long as foreground doesn't have any CIFilter or has shouldEnableEffects = NO, everything is fine. That is, I can tap on the screen and my game elements are instantiated and added to the main scene. But the minute I try adding a CIGaussianBlur or CIBloom to the foreground, I notice two things:
The framerate drops to about 2fps. Mind you, this happens even with
as little as 6 nodes alive in the scene.
The effect seems to be constantly cropping its contents or adjusting
it's frame. That is, if I have one node, the "full screen" effect
seems to try and constantly crop or adjust its bounds to the minimum
area required to hold all nodes. This is for one node:
And this is for 2 nodes:
In OpenGL ES 2, one would do a post blur / bloom by basically rendering the whole framebuffer (all objects) to texture, then doing at least one more pass to blur,etc on that texture and then either present that in the framebuffer attached to the display or compose that with the original render back to the framebuffer. I'd expect SKEffectNode to work in a similar way. However the cropping and the poor performance makes me think I might be using the effect node the wrong way. Any thoughts?
It seems to be a bug with the SKEffectNode trying to apply a filter on children SKShapeNodes as far as I can tell. I played around with this and achieved your results, but when I switched out the SKShapeNodes for SKSpriteNodes (using a simple png of a circle) the cropping no longer appears. It's a bug in that SKEffectNode doesn't handle the stroke of the SKShapeNode very well. If you take off the stroke (lineWidth = 0) and give it a fill color, you'll see that there is no cropping.
As for the frame rate, SKShapeNodes perform poorly. Doing the switch to SKSpriteNodes I mentioned earlier boosted my fps from 40 to 50 when I had 35 nodes on the screen (iPhone 5) with the filter applied.

Simple iOS particle emitter

Is there a simple way to do a bunch of 2d particle images using core animation like the star particle animation in Draw Something?
Particle animation in Draw Something
I found this link which get's me 90% of the way there. It shows how to emit particles using UIImage which is just what I need. I just need some help tweaking the particles so they spew out like the Draw Something button and not like their fire example and I want to randomize the colors of the particles and make them glow.
http://www.raywenderlich.com/6063/uikit-particle-systems-in-ios-5-tutorial
Okay, just found this which got me the rest of the controls I wanted. The variable I was looking for on the emitters was yAccelleration. Set it to a positive number to make it go down.
http://developer.apple.com/library/mac/#samplecode/Fireworks/Listings/AppController_m.html
Hope this helps someone else!

How Can I Animate The Significant Blurring of a Sprite

I am using Cocos2d on the iPad to create a small game. I would like to, purposely, significantly blur a sprite, and then fade it out.
Particularly, I was hoping to make a sprite containing text get blurry (like the ink was fading or soaking into the paper) and then ultimately disappear, as if by magic.
I know how to animate the fade process using Cocos2D actions.
How can I animate the process of a sprite becoming super blurry?
I do not think it is possible to do a blur effect with the default cocos2d actions/functions.
you might want to try this custom gaussian blur class out instead
http://www.cocos2d-iphone.org/forum/topic/6315
the code is in the middle of the thread
or a more troublesome alternative is to create the blur effect using frame by frame animation

Resources