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

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.

Related

Built in way to convert from screen coordinates to image coordinates?

I have an app where users can scale and position images in a number of ways. They can drag an entire layer of images around, scale that layer, drag around individual images inside the layer, and scale those individual images.
For some unrelated functionality, I need to generate the image coordinates that a user is pointing to on a given image (ie (0,0) for the top left & (width,height) for the bottom right), independent of how much it has been moved around and scaled. Is there a built in method for tranforming an absolute mouse position to it's relative position on an image (and vice versa) that takes into account any scaling/panning? I have started building my own methods for this tranformation but before I got too deep I wanted to see if it was already built in somewhere that I'm not seeing.
Konva doesn't have such methods yet. You have to implement them manually.
You can subscribe to this related issue: https://github.com/konvajs/konva/issues/303

Tiled Software for Map Creating: Make Tiles out of a entire map

Can you drag an image and have tiled create map and brake the image into small tiles for you. I don't know if it's possible. Like I have image done for my game and I have it in .png file which about 2680 * 1080 and I want to create that image in tile based so i can do A* Pathfinding algorithm.
or if you guys know any other alternative way to do it then please advice me on it.
Thanks in advance.
That is how Spritesheets work. Those are big images containing several small tiles.
When adding a new Tileset in Tiled, you choose your image and define the tile-size. Tiled will automatically use them correctly

Swift / SpriteKit: How to create a water drop effect?

i wonder how the following effect could be made in Swift (SpriteKit).
I think about adding some ShapeNodes and fill them with the background image with -yscale 1.
But i think this effect was made somehow different, because the background image isn't just mirrored horizontally - it also has some graphics effects on it (it is mirrored in the shape the water pearl has and so on).
Was it done with a ShaderNode?
Does someone has an idea how i could create a same effect in SpriteKit - like water pearls which looks like they are on the "camera" and they mirror the background "waterpearl like"?
Thank you.
Hmm well you could create a bunch of water images and have them displayed randomly on the screen at some z position, however I think this effect was made in another program like unity or something of the sort, Your best bet is to create transparent water droplets as images and have them displayed randomly on your screen.

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.

cocos2d load random image from sprite sheet

Ok so I am conceptualizing for a game I want to make and have played a little bit but ran in to a problem. I am learning iOS programming and learning cocos2d also.
I have about 20 images. They are all the same size 64x64. Basically they are grouped as 5 different shapes (square, circle, etc.) each in 5 different colors. What I want to be able to do, is randomly drop them down the screen in portrait. There will only ever be one per column falling. There can be duplicates across the screen, I actually want that. While it is random, I want to control the introduction of a new color.
I have been really racking my brain to figure out how to do this. As I said, I am pretty new to this so any help would be really great.
If I could be so bold, can any answers be in a format to "Explain it like I'm 5 I have only been doing this for about 1 year"
First, have a look at this tutorial about sprites sheets: it will show you how to create your sprite sheets and load them into memory.
(Since the tutorial aims at building an animation, you can skip points 4 and 5).
Now, if you have your sprite sheets in place, what you do to create a sprite is:
CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:#"frameName.png"];
and then you can add it to your layer/scene.
About the random selection: you can keep a list of image names (say, an NSArray) and then randomly (i.e., use srand to generate a random index) select the #"frameName.png" value from it.
When using Cocos2D the best way to use sprites is with Sprite Sheets. That is, a combination of two type of files:
A plist file with all the coordinates of N sprites and all different data about the sprites, size, name, etc.
A png file with all the sprites
You can make a sprite sheet with different types of software, like:
Zwoptex
Sprite Helper
Sprite Packer
Sprite Master
Texture Packer
About performance, when using a sprite sheet you will need to use CCSpriteBatchNode, as the API documentation states (read here):
A CCSpriteBatchNode can reference one and only one texture (one image file, one texture atlas). Only the CCSprites that are contained in that texture can be added to the CCSpriteBatchNode. All CCSprites added to a CCSpriteBatchNode are drawn in one OpenGL ES draw call. If the CCSprites are not added to a CCSpriteBatchNode then an OpenGL ES draw call will be needed for each one, which is less efficient.
THe code to use Sprite Sheet:
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"sprite.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"sprite.png"];
[self addChild:spriteSheet];
Hope this helps.

Resources