To get label text by clicking on the sprite
I have added a sprite and 开发者_Python百科added a label as the child of the sprite. Now I want to click the sprite and get the corresponding label text .Help me with some solution.
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);
To access your labelstring use your tempArray2 [tempArray2 objectAtIndex:i]
I suggest to use CCMenu for your demand.
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); }
To change the text from the label use its setText: method. This method is slow. For a fast version use CCLabelBMFont http://www.cocos2d-iphone.org/api-ref/0.99.5/interface_c_c_label_b_m_font.html
精彩评论