How to partially animate sprite in Sprite Kit? - ios

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.

Related

How to make animated spritekit background?

I want to make animated space background in my game, i tried using nodes on spritekit "stars nodes" but the game become laggy because of the amount of the stars, is there any program or way to make HD animated background ?
You just need one node for the background ya know.
Basically, you create each frame of your animated background as images. You can put these images into an image atlas in the asset catalogue.
Now, you can add a single sprite node that covers the whole scene as the "background". You just set the texture of the sprite node to the first frame of your animation.
Then, in didMoveToView or some methods like that,
get your image atlas as an SKTextureAtlas object
get each texture in the atlas using textureNamed and put each of them into an [SKTexture]
Now you have the array, you can do something like this:
-
yourBackgroundSprite.runAction(SKAction.repeatForever(
SKAction.animateWithtextures(theTexturesYouCreatedInStep2, timePerFrame: 30)
))
This link might also help you.

How to dissolve nodes in SpriteKit?

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:

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.

iOS: move circle shape view by collision by dragging another?

I have two UIViews shaped like a circle. (see image).
I am trying to figure out how to make the two views move by collision.
So I am moving one with a touch by changing it's position as I move my finger on it. I want to see the other move as I "push" the one I am moving against the other circle shaped view.
What is the correct way to achieve this?
Using SceneKit did the trick. I created the geometries and then applied physic bodies to each. With the proper physics configuration in the scene they now collide.

scale around certain point over time SpriteKit

I have a SpriteKit Scene in which I want to have the effect as if a camera zoom and scale. Does anyone know of any libraries or some easy methods of doing this?
It was very easy to do in other 2D engines but does not seem simple.
I was thinking of doing it from the app delegate, and using the window to zoom since my character does stay around the same position.
The desired effect I would like to accomplish is like that of the start of an Angry Bird level when the camera pans into the level and then the launch doc.
http://www.youtube.com/watch?v=_iQbZ3KNGWQ This is an example of the camera zoom and pans I am talking about.
Thanks for the help.
If you add an SKNode to the SKScene, and make your scene content children of that node instead of direct children of the scene, then you can zoom and pan all of the contained content just by adjusting the xScale, yScale and position properties of the added node. (Any content you did not want scrolled e.g. scores or whatever could then be added to a different SKNode or added directly to the scene).
The adjustment could be done by overriding one of update:, didEvaluateActions, or didSimulatePhysics in your SKScene subclass. The choice would depend on if you are just moving your character around by yourself in update:, or if it also gets moved around by running SKActions or by simulated physics.

Resources