Vins Rach
Vins Rach

Reputation: 41

Label showing wrong values while NSLog shows the right ones in SpriteKit

Label not showing the required values but nslog shows the right values that should be shown by the label. The label show a big number that is full of digits. Have I done anything wrong?, the relevant code shown below.

How it looks at the top...

@implementation ORPlayerResults
{ 
    SKLabelNode *numberOfPointsLabel;

    NSInteger newPoints;
    NSString *addingNewPointNumberStored;
}

In didMoveToView...

-(void)didMoveToView:(SKView *)view
{    
    // adding the label
    [self addChild:[self pointsTotalLabel]];
}

Information about the label

-(SKLabelNode *)pointsTotalLabel
{
    numberOfPointsLabel = [[SKLabelNode alloc] initWithFontNamed:@"Arial"];
    numberOfPointsLabel.text = @"Points Achieved: 0";
    numberOfPointsLabel.fontSize = 35;
    numberOfPointsLabel.fontColor = [SKColor whiteColor];
    numberOfPointsLabel.position = CGPointMake((self.size.width * 0.5)-200, self.size.height - 200);
    numberOfPointsLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;

    return numberOfPointsLabel;
}

the text label below is not showing the right values, but rather showing a wrong big number full of digits. nslog shows the results that I want.

-(void)pointsAchieved
{
    newPoints = [[NSUserDefaults standardUserDefaults] integerForKey:kORNewPoints];
    addingNewPointNumberStored = [NSString stringWithFormat:@"%li", (long)newPoints];
    numberOfPointsLabel.text = [NSString stringWithFormat:@"Points Achieved: %ld", (long)addingNewPointNumberStored];

    NSLog(@"Points accumulated is: %@", addingNewPointNumberStored);
}

Upvotes: 0

Views: 81

Answers (2)

user3001058
user3001058

Reputation: 13

You got confused on the different types in the formatting string, it should look like this:

-(void)pointsAchieved
{
newPoints = [[NSUserDefaults standardUserDefaults] integerForKey:kORNewPoints];
addingNewPointNumberStored = [NSString stringWithFormat:@"%d", newPoints];
numberOfPointsLabel.text = [NSString stringWithFormat:@"Points Achieved: %@", addingNewPointNumberStored];

NSLog(@"Points accumulated is: %@", addingNewPointNumberStored);
}

Upvotes: 1

Christian Fox
Christian Fox

Reputation: 400

I can't remember the name of them but you've mixed up your %@, %ld thingies.

Your NSLog uses %@ and works.

Your label.text uses %ld and does not.

You put your number in a string and then added that string to the label.text as if it were still a number. So you also need to remove the (long) from that statement.

Upvotes: 2

Related Questions