放置在CCNode上或位置错误时不显示CCSprite

时间:2011-11-08 07:42:39

标签: objective-c cocos2d-iphone ccsprite

我的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;
}

现在我希望我的敌人精灵出现在屏幕中间,但它没有,我不知道它是否显示。有趣的是,如果我将positionWorld从CCNode更改为包含4000x4000背景图像的CCSprite,它可以很好地工作,但为什么不使用带有contetSize的CCNode?如何使用CCNode?

谢谢 索伦

1 个答案:

答案 0 :(得分:0)

您需要为CCNode设置anchorPointccp(0.5,0.5)

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

CCSprite在其init方法内部执行相同的操作(CCSprite.m的第149行)。