1

I'm trying to create CCLabelTTF where the text changes as the story progresses, but I'm having trouble setting a simple piece of text to it. The label currently displays (null) and I can't seem get it to display "This is text". NSLog returns it properly, but the label won't display it. I also made sure the label wasn't destroyed. Here is the code:

    CGSize narrativeTextSize;
    narrativeTextSize.width=740;
    narrativeTextSize.height=60;

    CCLabelTTF  *narrativeTextLabel =[CCLabelTTF labelWithString:[NSString
                                                                   stringWithFormat:@"%@", narrativeText]
                                                       dimensions:narrativeTextSize
                                                       hAlignment:UITextAlignmentLeft
                                                    lineBreakMode:UILineBreakModeWordWrap
                                                         fontName:@"Helvetica"
                                                         fontSize:20];

    narrativeTextLabel.position =  ccp(255,60);

narrativeText = @"This is text";

//NSLog(@"%@", narrativeText);

[gameMenu addChild:narrativeTextLabel];
Venk
  • 5,949
  • 9
  • 41
  • 52

1 Answers1

1

Just write

narrativeText = @"This is text"; above CCLabelTTF *narrativeTextLabel ... line.

Venk
  • 5,949
  • 9
  • 41
  • 52
NiravPatel
  • 3,260
  • 2
  • 21
  • 31
  • Well, that was easy. Thanks. Now how do I change the text if I write a method to change the text, but located below the object...or if I write a method to change it in another layer? – Maximus Vermillion May 27 '13 at 06:44
  • what you can do is , you can create your label in .h file so it will be global..Now wherever you want to change the text of that label,you can write code like [yourlabel setString: [NSString stringWithFormat:@"Your Updated Text"]];...Hope it helps... – NiravPatel May 27 '13 at 06:50
  • -(void) changeTextButton { narrativeText = @"This is another piece of text"; } – Maximus Vermillion May 27 '13 at 06:54
  • If there a way to change the text with a method like something above? – Maximus Vermillion May 27 '13 at 06:55
  • Inside your changeTextButton method write down this code..-(void) changeTextButton { narrativeText = @"This is another piece of text"; [yourlabel setString: [NSString stringWithFormat:@"%@",narrativeText]]; } ,,so here "This is another piece of text"will be printed in a label which is declared in .h file – NiravPatel May 27 '13 at 06:56
  • I've thought about that, but I'm importing this label into a scene, so ideally, it should reuse the label instead of creating a new one. Maybe there is way to declare it differently so I can just change the text instead of the label. – Maximus Vermillion May 27 '13 at 07:18