tags:

views:

470

answers:

2

I am trying to place a series of sprites on a layer, but since I want to move all of the sprites as a group, I have created a CocosNode, set it to the same size as the screen and position it in the center of the screen, then add the Sprites to that node (lightNode) instead of adding them to the layer.

What I am seeing is that the sprites don't appear in the correct (same?) positions compared to when I place them in a layer and not in the lightNode.

I made a test program to show the problem using the XCode Cocos2d application template "Hello World" - I then replaced the -init() method of the HelloWorld class in HelloWorldScene.m with this code.

-(id) init{
if( (self=[super init] )) 
{
 // create my parent node that will contain all the related sprites
 lightPane = [[CocosNode alloc] init];
 CGSize paneSize = { 480, 320 };
 [lightPane setContentSize:paneSize]; 
 [lightPane setPosition:ccp(240, 160)];

 // add pane to layer   
 [self addChild:lightPane z:0];

 // add a series of sprites to demonstrate the problem 
    int y = 40;
    int x;
    for (x=30; x<300; x+=20)
    {
     Sprite *sp = [Sprite spriteWithFile:@"pause.png"];
    [sp setPosition:ccp(x, y)];

  NSLog(@"Next sprite at position (%d, %d)", x, y);

  [lightPane addChild:sp z:1];
  //[self addChild:sp z:1];

    x+= 20;
    }
}
return self;}

This code references a PNG file called "pause.png" - just take any small image about 30x30 pixels in size and place it in this project to run it.

What you will see is that the sprites appear about halfway up the screen, starting in the middle and heading to the right side.

If you comment out the line adding the sprite to lightPane, and un-comment the line below it, and re-run the sample, you will see the sprites (correctly) appear at the coords they should.

This obviously has something to do with the CocosNode I am using as a parent for the sprites. Anyone see what I am doing wrong here??

Thanks! I am new to Cocos2.

Jordan.

+1  A: 

You want to set the position of lightPane to 0,0. The position is not centered by default, it is the bottom left corner.

Colin Gislason
Doh! For some reason I expected the CocosNode to behave like a Sprite - which I believe has its anchor set to the center?? Either way, you're right, it does need to be position at 0,0 not at the center. Thanks!
Jordan Bigel
OR - I set the lightPane node to anchor at 0.5,0.5. I could swear I tried this earlier and it did not work. But, I think what I did was set the anchor to 240,160 and not .5,.5 - it turns out the anchor is proportional to the size of the content, not an absolute point, so .5,.5 means half the height and half the width.
Jordan Bigel
I think that sprites have a centered anchor position so actions behave as expected. e.g. imagine rotating a sprite and having it rotate from the bottom left.
Colin Gislason
A: 

Keep in mind that if you move to a sprite atlas, this won't work. Not unless you move to the new version of Cocos2d (1.9, which is in beta). The 1.9 version will let you do complex parenting of atlas sprites.

David Whatley