为什么 Cocos2d 没有把精灵放在我认为应该放的地方?

Why does Cocos2d not put sprites where I think it's supposed to?

50% 的时间我觉得我得到了我所期望的,然后另外 50% 的时间,我发现自己在“!?!?!?为什么在世界上是 cocos2d把这个放在这里?!?"

示例:

parent: CCNode, anchorpoint (0.5, 0.5), position (screenWidth / 2, screenHeight / 2)
  child: CCSprite, anchorpoint (0.5, 0.5), position (0,0), textureRect CGRectZero, contentSize manually set to scale of child of child
    child of child: CCSprite, anchorpoint (0.5, 0.5), position (0,0), same texture as "child", has a textureRect of something like (0,0,25,1), rotation of 90.  Its scaleY is set to 384.

我在哪里期待 child 的 child?当然是在屏幕中间。锚点在中间。

它实际出现在哪里?在屏幕的左侧。为什么?不知道。没有意义。

我的天啊……

所以,这与精灵添加到父级时有关!!!

如果你这样做:

CCSprite *sprite = [[CCSprite alloc] initWithFile:@"test.png" rect:CGRect(0,0,1,1)];
sprite.anchorPoint = ccp(0.5, 0.5);
sprite.scaleX = 384;
[node addChild:sprite];

那么定位就完全错了。但如果你这样做:

CCSprite *sprite = [[CCSprite alloc] initWithFile:@"test.png" rect:CGRect(0,0,1,1)];
[node addChild:sprite];
sprite.anchorPoint = ccp(0.5, 0.5);
sprite.scaleX = 384;

然后它被正确定位......完全是假的。难以置信,我在这上面浪费了很多时间。希望这对将来尖叫胡说八道的人有所帮助。