CCSprite放在CCNode上不显示或位置错误
我的初始化中有这样的代码:
-(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将 CCNode 的
anchorPoint
设置为ccp(0.5,0.5)
。CCSprite 在其
init
方法内部执行相同的操作(CCSprite.m
的第 149 行)。You need to set the
anchorPoint
to beccp(0.5,0.5)
for CCNode.CCSprite does the same thing internally in its
init
method (line 149 ofCCSprite.m
).