Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Create a <code>score</code> property in HelloWorldLayer.h after the interface declaration, like </p> <pre><code>@property (nonatomic, retain) int score; </code></pre> <p>Then synthesize it in your .m file just after the <code>@implementation HelloWorldLayer</code> line.</p> <p>Create methods for setting and getting scores:</p> <pre><code>-(int)getScore { return self.score; } -(void)setScore:(int)newScore { self.score = newScore; } </code></pre> <p>In the <code>init</code> method, set the value of the property to zero,</p> <pre><code>if( (self=[super init] )) { //... other stuff [self setScore:0] } </code></pre> <p>You can update the score with the setScore method, but I suggest having another method for this that calls setScore so that you can use it at different places with a single line call, and make any changes like assigning more score in certain situations, like two collisions within half-a-second etc..</p> <pre><code>-(void)updateScore:(int)increment { int currentScore = [self getScore]; [self setScore:(currentScore + increment)]; } </code></pre> <p>Similarly, for label,</p> <pre><code>@property (nonatomic, retain) CCLabelTTF scoreLabel; // in header </code></pre> <p>and </p> <pre><code>@synthesize scoreLabel; // in .m file </code></pre> <p>Again, in your init method, initialize the <a href="http://www.cocos2d-iphone.org/api-ref/0.99.5/interface_c_c_label_t_t_f.html" rel="nofollow">label</a> with position, layer and initial text etc. Then you can update that text in the updateScore method.</p> <pre><code>-(void)updateScore:(int)increment { int currentScore = [self getScore]; [self setScore:(currentScore + increment)]; [scoreLabel setString:[NSString stringWithFormat:@"Score: %i", [self getScore]]]; } </code></pre> <p>Make sure you read through the <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html#//apple_ref/doc/uid/TP30001163-CH1-SW2" rel="nofollow">tutorial</a> before going ahead in order to avoid confusion regarding common tasks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload