I have a group of sprites in a node;
E.g.
CCNode *grid = [CCNode node];
CCSprite *sprite1 = [CCSprite spriteWithImageNamed:#"sprite.png"];
[grid addChild:sprite1];
CCSprite *sprite2 = [CCSprite spriteWithImageNamed:#"sprite.png"];
[grid addChild:sprite2];
CCSprite *sprite3 = [CCSprite spriteWithImageNamed:#"sprite.png"];
[grid addChild:sprite3];
Anyway, the end result ends up being a 5x5 grid of tiles. I plan on rotating all the tiles 90 degrees together. My current code for that is this:
[grid runAction:[CCActionRotateBy actionWithDuration:1 angle:90]];
Now, the problem is that the group of sprites aren't just rotating, the whole group is "orbiting" around a point (looks like the bottom left of the group of sprites) and goes off the screen and I only want it to rotate the group of sprites while they remaining stationary.
I have tried setting the anchor to (0,0), (.5, .5), (1, 1), etc... but, that doesn't change anything.
All help greatly appreciated.
You are rotating the grid. If you want each tile to rotate individually you need to run one rotate action on each grid sprite, but not rotate the grid.
Finally with a lot of trial and error, I have found the solution.
When I made the new node and was adding sprites to it, the content size of the node stayed the same. I had to provide the length of the entire grid as the content size and then adjust the position and now the anchor point is the center of the grid.
Related
I can't get my head around what's going on here and I know I'm being stupid, but here's whats going on:
I have two images, one is 1024x768, one is 2048x1536. They are "background" textures. I'm trying to switch from one texture to the other:
CCSprite *bg = [CCSprite spriteWithFile:#"one.jpg"];
bg.scale = 0.5;
[self addChild:bg];
...
CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:two.jpg];
[bg setTexture: tex];
OK this is totally normal, the 2nd image fits perfectly into the same exact space and dimensions of the first, despite being a different resolution. Now i'm adding a 2nd image so i can have the first one fade into the second one
CCSprite *newBG = [CCSprite spriteWithFile:#"two.jpg"];
newBG.scale = bg.scale;
[self addChild: newBG];
This however doesn't work, the image is twice as big as it should be.
So what exactly is happening here? As best I can guess when a sprite is created from an image, the size of the sprite is set, and then any texture applied automatically scales to fit the space, and the 2nd doesn't work because the newly created sprite is using a different sized image?
When you set a texture, you should also call
-(void) setTextureRect:(CGRect) rect;
to set its size. This is automatically called in initWithTexture, but not when you simply call setTexture. So, you assumption is right: the texture size is set when you init the sprite and not changes thereafter; but you can change it when you want to use a new texture.
The idea is: I have a sprite with rectangle image
CCSprite *sprite = [CCSprite spriteWithFile:#"Rectangle.png"];
When I touch the sprite, 8 red points will appear
Holding a point and drag it to scale(resize) the image like this
Can anyone show me how to do or give me a sample code.
I am not sure about this but you can redraw a texture on that 8 points.
It seems similar to draw texture of soft body. I have implemented that for my game and in that i use 12 points and on that 12 point i tried to draw the texture.
So what you can do is you have to redraw the texture on that points when any of the point dragged. This is a tutorial for soft body,but You can refer this tutorial for reference.
http://www.uchidacoonga.com/2012/04/soft-body-physics-with-box2d-and-cocos2d-part-44/
It's not the same thing what you are looking for but yes using this you can implement.
I have a sprite added to a layer. I am having a lot of problems when I transform the layer. As far as I know a layer has its center (anchorPoint) on the bottom left corner and a layer has it in the middle (right?) - I am not totally sure about that.
On the figures below, I represent a CClayer in pink and a CCSprite in purple. See where I think the centers are.
When I add a sprite to a layer, I think Cocos will do like in A, but I want it like in B. How do I do that? Another possibility is C, that I think is better, but that would involve moving the anchorPoint of the layer to the middle and put the sprite there... I don't a have a clue on how to do that.
Change anchor point of CCSprite.
CCSprite *sprite = [CCSprite spriteWithFile:#"sprite.png"];
//For case A
sprite.anchorPoint = ccp(0.0f,0.0f);
sprite.position = ccp(0.0f,0.0f);
//For case B
sprite.anchorPoint = ccp(0.5f,0.5f);
sprite.position = ccp(0.0f,0.0f);
//For case c
sprite.position = ccp(ScreenWidth/2.0f, ScreenHeight/2.0f);
sprite.anchorPoint = ccp(0.5f,0.5f);
Anchor point is relatieve coordinate. (0.f, 0.f) is left-bottom corner of the node, (1.f, 1.f) is right-top corner. All transformations are make relatieve to the anchor point. Positioning is also transform. It means that in case of anchor point (0.5f, 0.5f) all positioning and other transforms will be applied relatieve to the center of the node. If you want to place your sprite to the left-bottom corner of your layer, you can simply set it's anchor point to (0.f, 0.f) and set position (0.f, 0.f). It means that left-bottom corner of your sprite will be placed to the (0.f, 0.f) coordinate of your parent layer.
[sprite setAnchorPoint:ccp(0.f, 0.f)];
[sprite setPosition:ccp(0.f, 0.f)];
[layer addChild:sprite];
When I add a sprite to a layer, I think Cocos will do like in A, but I
want it like in B.
Don't try to guess behaviour. Add sprite to the layer and see what happens.
Also, CCNode and all its subclasses, including CCSprite, have position property, which represents node's position relative to its parent's origin.
How will I show 2 sprites on same layer. Presently sprites with horizontal image is coming behind the other sprites. Horizontal image sprites drawing in CCRibbon class.
Well, you could assign them a z order. This is done by using code like this:
[self addChild: MySprite z:1]
The higher the z order, the highest up the sprite will be.
Currently the setanchor function only sets the anchor within the sprite frame. Is there any (easier) wway to change the anchor point to a coordinate on screen space? My purpose is to allow the sprite to rotate around a circle.
TIA for any suggestions made.
Use the Node hierarchy to your advantage. Create a new Node (which won't be visible unless you want it to be) and add your sprite to the node as children. Position the sprite child somewhat away by giving it a position of 100, 100 for example.
Now if you rotate the node using the rotation property, instead of the sprite, the sprite should rotate with the node, making it appear as if it goes around in a circle. The node itself will be the centerpoint of the rotation.
I've added this Q&A to my cocos2d FAQ:
http://www.learn-cocos2d.com/knowledge-base/cocos2d-iphone-faq/learn-cocos2d-public-content/manual/cocos2d-general/14826-how-to-rotate-a-sprite-in-a-circular-motion