如何解决通过单击精灵来获取标签文本
| 我添加了一个精灵,并添加了一个标签作为该精灵的子代。 现在我想单击精灵并获取相应的标签文本。请提供一些解决方案。bubblesprite = [CCSprite spriteWithFile:@\"bubble2.png\"];
bubblesprite.position=CGPointFromString([self positioning]);
[self addChild:bubblesprite];
label = [CCLabelTTF labelWithString:[tempArray2 objectAtIndex:i] fontName:@\"Marker Felt\" fontSize:30];
label.color = ccc3(233,34,19);
[bubblesprite addChild: label z:1];
CGSize s = [bubblesprite contentSize];
label.position = ccp(s.width/2,s.height/2);
解决方法
要访问您的标签字符串,请使用tempArray2
[tempArray2 objectAtIndex:i]
我建议您使用CCMenu。
NSString* labelString = @\"test\";
CCLabelTTF* aLabel = [CCLabelTTF labelWithString:labelString fontName:@\"Maker Felt\" fontSize:24];
aLabel.position = ccp(100,100);
[self addChild:aLabel];
CCMenuItemImage* button = [CCMenuItemImage itemFromNormalImage:@\"Icon.png\" selectedImage:@\"Icon.png\" target:self selector:@selector(buttonPress)];
button.position = ccp(100,100);
CCMenu* menu = [CCMenu menuWithItems:button,nil];
menu.position = ccp(0,0);
[self addChild:menu];
-(void)buttonPress
{
CCLog(\"%@\",labelString);
}
要从标签更改文本,请使用其setText:方法。这种方法很慢。对于快速版本,请使用CCLabelBMFont http://www.cocos2d-iphone.org/api-ref/0.99.5/interface_c_c_label_b_m_font.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。