How to scale a CCLayer and keep its position? - cocos2d-iphone

At the end of the game, I want to zoom out my CCLayer to show all the sprites. When I reduce the scale, the CCLayer become smaller and centered, what I want to know is how to scale the CCLayer and keep its y=0 (Origin at the bottom of the screen). Thanks in advance.
Best regards,

I believe that all CCNode children have an anchorPoint property. CCLayer defaults to (.5,.5) , midScreen. Try setting your layer's anchor point to (0.,0.).

Related

ccsprite texture rect changes based on parent scaling?

I am using Cocos2d 2.2, and am experiencing the following:
I have a sprite added as a child to a CCNode. The sprite's anchor point is 0,0. The CCNode's anchor point is 0,0. I've got a CCNode with an anchor point of 0.5,0.5 and in it I add a child with the following rect: 0,363,165,231).
I then add that CCNode as a child to another CCNode with an anchor point of 0,0...
All is well.. Looks perfect.
I scale that CCNode down to 0.5
All of a sudden, I see at the top of my sprite, garbage from the texture that is not part of it's texture rect.
Why in the world does scaling down a CCNode change the texture coordinates?!
Note, if the CCNode has an anchor point of 0,0 this doesn't happen. Only when its 0.5, 0.5...

confine rendering to cclayer frame

Is there any way of confining the rendering of CCSprites and such onto a CCLayer in a way that anything beyond the frame of the CCLayer gets clipped (ie. doesn't display past the frame of the CCLayer)?
Why do I need this? I'm working on a roguelike and want a modal box to appear to select spells, wands, potions or scrolls. I place a CCSCcrollView on top a CCLayer to scroll through the lists but I need to confine the display to just the region I want.

ccmenuitem click response position is off after changing cccamera coordinates

I'm developing an ios game with cocos2d currently. There is a ccmenuitem on the screen. As the game starts, the player moves forward. In order to keep the player on the centre of the screen, the cccamera coordinates are changing according to player's position. The problem is when I click on the menuitem after the camera coordinates are changed, it does not response. For example, if the camera coordinates are moved 10 px to the right, I have to click 10px to the right of the the menuitem in order to "click on it".
Does any one know how fix this ? :(
This is a know side-effect of using CCCamera. There's rarely any need for using the camera though because you can achieve the same scrolling effect by simply moving the layer in the opposite direction.

Cocos2d: Change sprite's zOrder dynamically (using CCSpriteBatchNode)

Is there a way to change the zOrder of a sprite rendered by CCSpriteBatchNode? I've tried it like this:
[self reorderChild:mySprite z:indexOfAnArray];
I get this error: 'If CCSprite is being rendered by CCSpriteBatchNode, CCSprite#draw SHOULD NOT be called'
I also tried with the zOrder property of the sprite but unfortunately is read-only.
I need to change the zOrder because, depending on the position where my sprite will appear, the zOrder needs to be changed.
self is probably not the parent of sprite. Remember that the parent is the CCSpriteBatchNode that holds it, and self is, from your point of view, probably the scene.

centering a sprite in the middle of the screen convertToWorldSpace / NodeSpace

i have passed many many days on this problem, i've not very clear in mind what convertToWorldSpace, convertToNodeSpace does...
i have 2 layer (CCNode) one that i can scale, one for panning the screen, so i scale always what is displayed makeing the center of scaling the middle of the screen:
scaleLayer=[CCNode node];
scaleLayer.position= ccp( size.width /2 , size.height/2 );
[self addChild:scaleLayer z:-2000];
panLayer=[CCNode node];
panLayer.position= ccp( -size.width /2 , -size.height/2 );
[scaleLayer addChild:panLayer z:-2000];
now i add sprites to panlayer:
[panLayer addChild:mysprite z:2];
i'd like to have a button that, when pressed, moves panlayer to make the sprite in the middle of the screen.
i have wrote this but works only if scaleLayer.scale=1
CGPoint p;
p=[panLayer convertToWorldSpace:mysprite.position];
p.x=(panLayer.position.x+(240-p.x));
p.y=(panLayer.position.y+(160-p.y));
[panLayer runAction:[CCMoveTo actionWithDuration:0.5 position:p]];
how can i make the sprite to be at center of the screen with different scales?
thanks
I'm not sure what your existing code does but you might find the property mysprite.boundingBox useful. It will give you a CGRect with the sprite's width and height and the position of its bottom left corner, and it will reflect whatever scale you have applied to the sprite.