Create a reference point on a sprite

Forums Programming cocos2d support (graphics engine) Create a reference point on a sprite

This topic contains 8 replies, has 5 voices, and was last updated by  HiRez 1 year, 5 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
Author Posts
Author Posts
November 6, 2012 at 9:37 pm #245185


Hi guys,

I am looking for a way to create a reference point on a sprite. I am using it’s anchorpoint with CCScale, but now I need another point of reference to check for collision detection. My game does not require complex CD, just whether or not a point has intersected a rect or a path. But in order to do that, I need to create a reference point, that stays relative to the sprite after rotating/scaling. Has anyone does this before?

November 7, 2012 at 5:34 am #391697


For example, how do I compare the bottom middle of a sprite (that has been rotated and scaled) with another sprite? (Say, usingCGRectContainsPoint) What point would I reference to?

November 7, 2012 at 10:07 am #391698


You could attach an empty node as a child at the reference location (before you translate/scale/rotate it), then read the position off that. Don’t know if that’s the best or only way to do it, just something I thought of.

November 7, 2012 at 3:21 pm #391699


The way @HiRez describes it, would be the way to do it.

November 9, 2012 at 4:51 am #391700


I tried this a couple times now, but no success. How exactly do I do this? Am I adding the CCNode as a child to the sprite? And how do I attach it to a reference location?

CCSprite *growingStick = [CCSprite spriteWithFile@"stick.png"];
growingStick.position = ccp ( 240, 320 ); //top middle of screen
growingStick.anchorpoint = ccp ( 0.5, 1.0 ); //top middle of stick
[self addChild: growingStick];

CCNode *endPoint = [CCNode new];
endPoint.position = ??
[??? addChild: endPoint];

Anyway I try, it doesn’t work :(

Basically I need to create another anchorpoint that I can use for myself.

November 9, 2012 at 7:31 am #391701


You can do:

CCNode *endPoint = [CCNode node];
endPoint.position = ccp(0,0);
[growingStick addChild: endPoint];

That will attach the endPoint to the bottom right of the sprite.

Note: ccp(0.5*(growingSticks Width), growingSticks Height) would give you the same position point as the growing stick.

Then to get it’s position you’d say something like:

CGPoint pos = [growingStick convertToWorldSpace: endPoint.position];

I find it’s helpful for testing purposes to do something like:

NSLog(@"Node Position:(%f, %f)",pos.x, pos.y);

To see where it goes in your log navigator output.


NSLog(@"Node Position:(%i, %i)",(int)pos.x,(int)pos.y);

If you don’t like decimal places. :)

November 9, 2012 at 4:14 pm #391702


Thank you so much! I messed around with convertToWorldSpace but never got it working… Thanks! There is no rep system here is there? I’d +1 you

November 9, 2012 at 5:56 pm #391703


You converting to and from worldspace to node space is pretty cool once you have the hang of it.

The trick I always remember is ["parent of that node" convertToWorldSpace: "node".position];

So if you keep that in mind, “parent of that node converts the node’s position to world space”, it’ll always work out.

November 10, 2012 at 5:03 am #391704


Good tip, Lam. Always found convertToWorldSpace a little confusing. Knowing you call it on the parent of the node whose position you want converted helps.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.