How to make animated spritekit background? - ios

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.

Related

How to add multiple images to a single sprite in cocos2d?

I am creating an application in cocos2d using sprite builder where a user clicks a picture from the camera which is then converted into a jigsaw puzzle. I have cropped and converted the image into a puzzle of four images by masking and saved it into an array successfully. I now want to add all the four images into the same sprite. At the moment I'm adding the images to the sprite with a for loop but I end up overwriting the previous images and the end result is a single image in the sprite. I'm not sure how to add multiple images to the sprite (or multiple sprites to the screen. Whichever works). Please let me know how can I achieve this.
Thanks in advance.
Regards codePassion.

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 to change border of this image

I am making a game using this function:
if (CGRectIntersectsRect(object.frame, object2.frame)) {
[self GameOver];
}
Both objects are square however the image of object #2 is not. Therefore when the borders collide (but not the actual pictures) the game ends. Is there a way I can have the border "fit" to the image, so that the game only ends when the actual pictures collide.
Thanks :)
**my image is a shark and therefore a rectangle cannot be used
A view's frame is always a CGRect, which is a rectangle. You can use UIKit Dynamics for collision detection with views, but that only support rectangles too.
As #jammycoder mentioned, try out SpriteKit or other game engines, if you need to detect custom shape bounds.

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.

Resources