问题描述
|
我添加了一个精灵,并添加了一个标签作为该精灵的子代。
现在我想单击精灵并获取相应的标签文本。请提供一些解决方案。
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