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

发布于 2024-12-14 08:45:10 字数 1394 浏览 2 评论 0原文

我的初始化中有这样的代码:

-(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 一起工作?

谢谢 索伦

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红颜悴 2024-12-21 08:45:10

您需要将 CCNode 的 anchorPoint 设置为 ccp(0.5,0.5)

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

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

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文