我有一个游戏商店,玩家可以在那里购买物品。我有一个输出物品价格的CCLabelTTF
,旁边有一个CCSprite
,它是一个硬币(货币)的图像。由于商品的价格随着用户获得更多的商品而发生变化,标签文本开始重叠就在标签旁边的硬币CCSprite
。我正在努力使硬币CCSprite
定位在标签位置上。是否有比将CCSprite作为子级添加到CCLabelTTF中更好的方法来做到这一点?,例如在CCSprite
中包含CCLabelTTF
现在我有一个:
coinIcon = [CCSprite spriteWithImageNamed:@"coin.png"];
[coinIcon setScale:0.3];
priceLabel.string = [NSString stringWithFormat:@"%.Lf", price];
[priceLabel addChild:coinIcon];
我也在尝试setPositionInPoints,但是每当我设置一个位置,coin
就不再出现在屏幕上。这是我一直使用这种方法时遇到的主要问题。它可以很好地工作,而不设置一个位置,但我需要硬币在左边的priceLabel
。当我试图设置一个位置并运行应用程序时,它不会出现在屏幕上。有什么想法吗?
发布于 2014-10-08 22:06:41
使用CCNode作为容器,如下所示:
// CCSprite and CCLabelTTF are by default anchored (.5,.5)
// so make label same height as coin and it will line dead-center
// to the coin's vertical apex
float offsetInPoints = 8.f;
float labelWidthInPoints = 80.f;
CCNode* priceNode = [CCNode node];
coinIcon = [CCSprite spriteWithImageNamed:@"coin.png"];
[coinIcon setScale:0.3];
priceLabel.string = [NSString stringWithFormat:@"%.Lf", price];
priceLabel.horizontalAlignment=CCTextAlignmentLeft;
priceLabel.dimensions=CGSizeMake(labelWidthInPoints,coinIcon.contentSizeInPoints.height);
[priceNode addChild:coinIcon];
[priceNode addChild:priceLabel];
priceLabel.positionInPoints = ccpAdd(coinIcon.positionInPoints,
ccp(coinIcon.contentSizeInPoints.width+offsetInPoints,coinIcon.position.y));
这将把价格标签在一个恒定的offsetInPoints在硬币的右边,无论最终的价格是什么。
未测试,从内存中编码
https://stackoverflow.com/questions/26266487
复制相似问题