直截了当,我有一个基本上从CCLayer继承而且有2个精灵作为孩子的课程。
some_layer.h
@interface some_layer : CCLayer {
CCSprite *back;
CCSprite *front;
}
some_layer.mm
// on "init" you need to initialize your instance
-(id) init
{
if( (self=[super init])) {
back = [CCSprite spriteWithFile:@"back.png"];
front = [CCSprite spriteWithFile:@"front.png"];
[front setOpacity:0];
[self addChild:back];
[self addChild:front];
//this is after modification
back.positionInPixels = CGPointMake(winSizeInPixels.width/2,winSizeInPixels.height/2);
front.positionInPixels = CGPointMake(winSizeInPixels.width/2,winSizeInPixels.height/2);
}
return self;
}
//after the edit
-(void) setPositionInPixels:(CGPoint)positionInPixels {
[super setPositionInPixels:CGPointMake(positionInPixels.x -(winSizeInPixels.width/2), positionInPixels.y -(winSizeInPixels.height/2))];
}
-(void) setPosition:(CGPoint)position {
[super setPosition:CGPointMake(position.x -(winSize.width/2), position.y -(winSize.height/2))];
}
当然,这不是图层的全部实现,但该代码是唯一与问题相关的代码,如果您认为需要更多,请告诉我。
现在在父图层的某些部分我正在尝试这样做
float x = winSizeInPixels.width/2;
float y = winSizeInPixels.height/2;
[self setPositionInPixels:CGPointMake(x, y)];
mylayer = [[some_layer alloc] init];
[mylayer setPositionInPixels:CGPointMake(-offset, -offset)];
[self addChild:mylayer];
[mylayer runAction:[CCEaseInOut actionWithAction:[CCRotateTo actionWithDuration:speed*20 angle:angle] rate:4]];
这段代码的输出是,我可以看到精灵旋转,但不是在some_layer的中心周围,我希望它们旋转并保持在它们的位置(绕自己旋转),我想要做的是旋转,同时缩放它们,同时在屏幕上保持其位置相同,
精灵在一些随机点周围保持旋转(也许在他们的父母的父母身边,这是最后一段代码中的自我)
顺便说一下,我没有触及精灵的前后位置,我只处理那个“some_layer”,我试着将他们的位置设置为(0,0)“some_layer”的位置唯一正确的时候是它的旋转角度是0,它的刻度是1.0,如果有什么不清楚请告诉我,非常感谢,我可以提供一些屏幕截图
编辑:我修改了上面的代码,它作为一种解决方法正常工作,但我认为这不是正确的方法!请查看评论,我不知道发生了什么奇怪的事情!!
谢谢!
编辑2:找到答案
回答我的自我
将其添加到some_layer.mm
的init方法中[self setContentSize:CGSizeMake(0, 0)];
感谢所有试图帮助我的人:)
分析代码是最好的解决方案!
BTW我删除了两个setPos的重写方法并删除了改变Sprites位置的代码,现在就像这样
// on "init" you need to initialize your instance
-(id) init
{
if( (self=[super init])) {
back = [CCSprite spriteWithFile:@"back.png"];
front = [CCSprite spriteWithFile:@"front.png"];
[front setOpacity:0];
[self addChild:back];
[self addChild:front];
[self setContentSize:CGSizeMake(0, 0)];
}
return self;
}
答案 0 :(得分:1)
您是否正在为正在使用的图层或精灵设置一些anchorPoint。如果是,那么请检查锚点是否应设置在中心位置:
someSprite.anchorPoint = CCP(0.5,0.5);
有关详情:http://www.qcmat.com/understanding-anchorpoint-in-cocos2d/