iDevGames Forums
centering a sprite in the middle of the screen convertToWorldSpace / NodeSpace - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: centering a sprite in the middle of the screen convertToWorldSpace / NodeSpace (/thread-9640.html)



centering a sprite in the middle of the screen convertToWorldSpace / NodeSpace - sefiroths - Dec 21, 2011 01:25 AM

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 positionRasp]];

how can i make the sprite to be at center of the screen with different scales?
i thought to find the distance in pixels in scaleLayer coordinates while convertToWorldSpace find the absolute coordinates (if i had understood well)
thanks