0

In Cocos2D, I would like a sprite placed on a screen coordinate, not a map coordinate. I thought I could get by using convertToNodeSpace, but it doesn't seem to do what I want.

I thought this should place a sprite in the middle of my iPad screen:

selectionScreenOverlaySprite.position = [self convertToNodeSpace:CGPointMake(512, 384)];

But it doesn't. It also places it in a different place depending on the size of my map. Does anyone know what I should be using? I've also tried: convertToWorldSpace, convertToNodeSpaceAR, and convertToWorldSpaceAR.

1 Answer 1

1

Try this:

CGSize wins = [[CCDirector sharedDirector] winSize];
[yourSprite setPosition:CGPointMake(wins.width / 2, wins.height / 2)];

This is better than using hard-coded values because it will work regardless of resolution.

10
  • Thanks. That is helpful to know, but it is still placing it in a different position depending on the size of the map. Also, I may be viewing the opposite side of the map when it is placed, which may add to this problem. Jan 20, 2012 at 1:58
  • What is this map of which you speak? Is it the layer to which the sprite is added? If so, that will affect its position. Add it to a layer that has the same bounds as the screen. Jan 20, 2012 at 2:02
  • Okay I understand, but that will also force a different order of the sprite in relation to everything else, right? It's being added to self, which is the CCLayer that renders the map. Jan 20, 2012 at 2:14
  • When adding sprites to a layer, you can specify their ordering with the z: parameter. [self addChild:someSprite z:0] Jan 20, 2012 at 2:21
  • I know about z ordering, but if I add it to another layer, can I still specify a z order that will make it appear in front of a higher layer? Jan 20, 2012 at 2:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.