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??
You want to set the position of lightPane
to 0,0
. The position is not centered by default, it is the bottom left corner.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With