Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you synthesize a private variable (other classes cannot see it) you allow a way for other classes to see and/or modify the value of that variable.</p> <p>First, you want to create the variable:</p> <pre><code>NSMutableArray *_targets; NSMutableArray *_projectiles; int _score; CCLabelTTF *_scoreLabel; </code></pre> <p>Then in your init method to set the <code>_score</code> to <code>0</code>:</p> <pre><code>-(id) init { if( (self=[super init] )) { [self schedule:@selector(update:)]; _score = 0; </code></pre> <p>Then increment (add 1 to) your <code>_score</code> variable and set the string (the text content) of your <code>_scoreLabel</code> to that value.</p> <pre><code> if (CGRectIntersectsRect(projectileRect, targetRect)) { [targetsToDelete addObject:target]; _score++; [_scoreLabel setString:[NSString stringWithFormat:@"%d", _score]]; } </code></pre> <p>The line <code>[_scoreLabel setString:[NSString stringWithFormat:@"%d", _score]];</code> is a way to convert the integer of <code>_score</code> to a string (<code>NSString</code>). It's an old C way of doing it, the <code>%d</code> means that whatever is going to be there should be displayed as an integer as opposed to a float (having decimal points).</p> <p>It also looks like you need to "instantiate" your label and add it as a child to the layer. Instantiation is just a fancy term for creating a instance of something. Think of a "class" as a blueprint for a chair, and an "instance" as a chair created from that blueprint. Once you have the chair created (an instance), you can modify it (paint it, add/remove legs, etc).</p> <p>So, to instantiate your label and add it to the layer (itself):</p> <pre><code>-(id) init { if( (self=[super init] )) { [self schedule:@selector(update:)]; _score = 0; //Create label _scoreLabel = [CCLabelTTF labelWithString:@"0" fontName:@"Marker Felt" fontSize:16]; //Add it to a layer (itself) [self addChild:_scoreLabel]; </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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