0
votes

I have this code in my init:

-(id)init {
    if ((self = [super init])) {
        CGSize screenSize = [CCDirector sharedDirector].winSize;
        mapSize = CGSizeMake(4000, 4000);

        rotateWorld = [CCNode node];
        [rotateWorld setContentSize:mapSize];
        rotateWorld.position = CGPointMake(screenSize.width / 2, screenSize.height / 2);

        positionWorld = [CCNode node];
        [positionWorld setContentSize:mapSize];
        positionWorld.position = CGPointMake(mapSize.width / 2, mapSize.height / 2);
        [rotateWorld addChild:positionWorld z:0];
        [self addChild:rotateWorld z:0];

        // Test enemy
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"EnemiesSpritesheet.plist"];
        spriteSheetNonPlayer = [[CCSpriteBatchNode  alloc] initWithFile:@"EnemiesSpritesheet.png"capacity:10];
        [positionWorld addChild:spriteSheetNonPlayer z:0];
        enemy = [[Enemy_01 alloc] initWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"enemy_01_01.png"]];
        enemy.position = CGPointMake(mapSize.width / 2, mapSize.height / 2);
        [spriteSheetNonPlayer addChild:enemy];
    }

    return self;
}

Now I would expect my enemy sprite to show up in the middle of the screen, but it does not and I do not know if it is show at all. The funny thing is that if I change the positionWorld from a CCNode to a CCSprite containing a background image of 4000x4000 it works perfectly, but why not with a CCNode with its contetSize set? How do I get this to work with a CCNode?

Thank you Søren

1

1 Answers

0
votes

You need to set the anchorPoint to be ccp(0.5,0.5) for CCNode.

rotateWorld.anchorPoint = ccp(0.5f,0.5f);
positionWorld.anchorPoint = ccp(0.5f,0.5f);

CCSprite does the same thing internally in its init method (line 149 of CCSprite.m).