mtlchris
mtlchris

Reputation: 21

SKLabelNode update at touch

I'm trying to find the cause of a weird behavior. My SpriteKit scene have a SKLabelNode as property. The SKLabelNode is initialized in a method called levelSelected :

 -(void)levelSelected{
    SKSpriteNode* bar = [SKSpriteNode spriteNodeWithColor:[UIColor colorWithHue:0.566f saturation:0.787f brightness:0.792f alpha:1] size:CGSizeMake(self.frame.size.width,60)];
    bar.anchorPoint = CGPointMake(0, 0);
    bar.position = CGPointMake(0, 50);
    float space = (self.size.width - 45*6) / 7;
    SKSpriteNode* letterBox = [SKSpriteNode spriteNodeWithImageNamed:@"letter_box_def"];
    letterBox.anchorPoint = CGPointMake(0.5, 0.5);
    letterBox.position = CGPointMake((space+((space + 45)*2)+22.5), 30);
    [bar addChild:letterBox];
    self.gl2 = [SKLabelNode labelNodeWithFontNamed:@"Arial"];
    self.gl2.fontColor = [SKColor blackColor];
    self.gl2.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
    self.gl2.fontSize = 30;
    self.gl2.text = @"";
    NSLog(@"%@", self.gl2);
    [letterBox addChild:self.gl2];
    [self addChild:bar];
    self.gl2.text = @"D";
    }

As expected, at runtime, the node shows "D". My touch method is as follow :

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"Called touchesBegan");
    NSLog(@"Node : %@", self.gl2);
    NSLog(@"Text : %@", self.gl2.text);
    self.gl2.text = @"k";
    NSLog(@"Node : %@", self.gl2);
    NSLog(@"Text : %@", self.gl2.text);
    }

Now, a touch makes the label disappear from the screen. The log shows :

2014-08-14 06:15:34.841 Guess[3125:607363] Called touchesBegan
2014-08-14 06:15:34.842 Guess[3125:607363] Node : <SKLabelNode> name:'(null)' text:'D' fontName:'Arial' position:{0, 0}
2014-08-14 06:15:34.843 Guess[3125:607363] Text : D
2014-08-14 06:15:34.846 Guess[3125:607363] Node : <SKLabelNode> name:'(null)' text:'k' fontName:'Arial' position:{0, 0}
2014-08-14 06:15:34.847 Guess[3125:607363] Text : k

On thing I have tried is using another method, called from the levelSelected method :

-(void)levelSelected{
    SKSpriteNode* bar = [SKSpriteNode spriteNodeWithColor:[UIColor colorWithHue:0.566f saturation:0.787f brightness:0.792f alpha:1] size:CGSizeMake(self.frame.size.width,60)];
    bar.anchorPoint = CGPointMake(0, 0);
    bar.position = CGPointMake(0, 50);
    float space = (self.size.width - 45*6) / 7;
    SKSpriteNode* letterBox = [SKSpriteNode spriteNodeWithImageNamed:@"letter_box_def"];
    letterBox.anchorPoint = CGPointMake(0.5, 0.5);
    letterBox.position = CGPointMake((space+((space + 45)*2)+22.5), 30);
    [bar addChild:letterBox];
    self.gl2 = [SKLabelNode labelNodeWithFontNamed:@"Arial"];
    self.gl2.fontColor = [SKColor blackColor];
    self.gl2.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
    self.gl2.fontSize = 30;
    self.gl2.text = @"";
    NSLog(@"%@", self.gl2);
    [letterBox addChild:self.gl2];
    [self addChild:bar];
    self.gl2.text = @"D";
    [self touchedSomething];
}
-(void)touchedSomething {
    NSLog(@"Called touchedSomething");
    NSLog(@"Node : %@", self.gl2);
    NSLog(@"Text : %@", self.gl2.text);
    self.gl2.text = @"k";
    NSLog(@"Node : %@", self.gl2);
    NSLog(@"Text : %@", self.gl2.text);
}

This works perfectly, the D is replaced by k, producing the log :

2014-08-14 06:20:09.934 Guess[3132:608100] Called touchedSomething
2014-08-14 06:20:09.935 Guess[3132:608100] Node : <SKLabelNode> name:'(null)' text:'D' fontName:'Arial' position:{0, 0}
2014-08-14 06:20:09.935 Guess[3132:608100] Text : D
2014-08-14 06:20:09.938 Guess[3132:608100] Node : <SKLabelNode> name:'(null)' text:'k' fontName:'Arial' position:{0, 0}
2014-08-14 06:20:09.939 Guess[3132:608100] Text : k

But if I remove the [self touchedSomething] call from levelSelected and add it to touchesBegan, it won't work, and the label will again disappear from the screen.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"Called touchesBegan");
    [self touchedSomething];
}

-(void)touchedSomething {
    NSLog(@"Called touchedSomething");
    NSLog(@"Node : %@", self.gl2);
    NSLog(@"Text : %@", self.gl2.text);
    self.gl2.text = @"k";
    NSLog(@"Node : %@", self.gl2);
    NSLog(@"Text : %@", self.gl2.text);
}

2014-08-14 06:24:49.492 Guess[3145:609098] Called touchesBegan
2014-08-14 06:24:49.493 Guess[3145:609098] Called touchedSomething
2014-08-14 06:24:49.494 Guess[3145:609098] Node : <SKLabelNode> name:'(null)' text:'D' fontName:'Arial' position:{0, 0}
2014-08-14 06:24:49.494 Guess[3145:609098] Text : D
2014-08-14 06:24:49.498 Guess[3145:609098] Node : <SKLabelNode> name:'(null)' text:'k' fontName:'Arial' position:{0, 0}
2014-08-14 06:24:49.498 Guess[3145:609098] Text : k

Thanks for your help !

Upvotes: 1

Views: 692

Answers (1)

mtlchris
mtlchris

Reputation: 21

Well, I was able to solve it (kind of). Defining a zPosition for the label fixed it. Why it worked properly in some cases but not other would remain a mystery.

Upvotes: 1

Related Questions