首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >围绕CCLabelTTF的中心旋转,作为顶点

围绕CCLabelTTF的中心旋转,作为顶点
EN

Stack Overflow用户
提问于 2013-09-07 18:13:11
回答 2查看 323关注 0票数 0

我想旋转一个CCLabelTTF围绕它的中心。

但看起来不像。它看起来更像是CCLabelTTF底部的旋转。

代码:

代码语言:javascript
复制
CCLabelTTF *aLabel ... init/addChild and so on

CCRotateBy *rotateLabelA = [[CCRotateBy alloc] initWithDuration:0.5f angle:-60.0f];

aLabel.string = @"0";
aLabel.anchorPoint = ccp(0.5f, 0.5f);
[aLabel runAction:rotateLabelA];

如果字母是CCLabelTTF,如何围绕其可见中心旋转?

我能够使CCLabelTTF的边界框可见:

如图中所示,包围框要大得多。但是没有一个公式来决定信的中间位置。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-08 12:57:12

我找到了如何找到CCLabelTTF的中间点。

代码语言:javascript
复制
    float fontSize = bLabel.fontSize; // actual Font size in pixels
    float labelHeight = bLabel.contentSize.height; // actual label height ( the same as boundingBox.size.height )
    float offset = labelHeight - fontSize; // the free room under the font
    float halfFontSize = fontSize / 2;
    float percentMiddleOfFont = (halfFontSize + offset) / labelHeight;

    bLabel.anchorPoint = ccp(0.5f, percentMiddleOfFont);
票数 0
EN

Stack Overflow用户

发布于 2013-09-07 19:13:46

如果将anchorPoint = cpp(0.5f,0.5f)设置为某个ccNode对象,它将围绕其中心旋转,这是使用boundingBox属性计算的。

问题是标签的boundingBox.size.height与实际高度不同。这就是为什么它不是围绕着中心旋转的。

我不确定在这样一个手动解决方案,但它对我有一天。

代码语言:javascript
复制
    CCLabelTTF *label = [CCLabelTTF labelWithString:@"0" fontName:@"Marker Felt"fontSize:24];
    label.position = ccp(winSize.width /2.0f, winSize.height / 2.0f);

    float fontSize = label.fontSize; // actual Font size
    float labelHeight = label.contentSize.height; // actual label height ( the same as boundingBox.size.height
    float offset = (labelHeight - fontSize - (labelHeight - fontSize) / 2.0f) / labelHeight / 2.0f;
    label.anchorPoint = ccp(0.5f, 0.5f + offset);

    [layer addChild:label];
    [label runAction:[CCRotateBy actionWithDuration:10.0f angle:-360]];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18676402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档