0

So I create an SKLabelNode: EDIT: CHANGED CODE TO: //.h {SKLabelNode *PausedLabel22;} //.m PausedLabel22= [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"]; PausedLabel22.text = @"Game Paused"; PausedLabel22.fontSize = 30; PausedLabel22.fontColor = [SKColor blackColor]; PausedLabel22.position = CGPointMake(160, 340); //[self addChild:PausedLabel22]; This used to perfectly fine, but then I have my method:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
 if (paused == YES) {
    paused = NO;
    [PausedLabel22 removeFromParent];        
    NSLog(@"UnPaused");
}else if (touchLocation.x > self.frame.size.width - 80 && touchLocation.y > self.size.height-80 && touchLocation.y < self.size.height-40) {
    paused = YES;
    NSLog(@"Paused");    
    //PausedLabel22.text = [NSString stringWithFormat:@"Game Paused"];
    //PausedLabel22.fontColor = [SKColor blackColor];
    //PausedLabel22.position = CGPointMake(160, 320);
    [self addChild:PausedLabel22];
}
NSLog(@"Text Postion: (%f, %f)", PausedLabel22.position.x, PausedLabel22.position.y);
NSLog(@"Text: %@", PausedLabel22.text);
NSLog(@"Text color: %@", PausedLabel22.fontColor);

My log shows me: 2014-03-15 23:12:12.734 NuclearGame[1418:60b] UnPaused 2014-03-15 23:12:12.735 NuclearGame[1418:60b] Text Postion: (160.000000, 320.000000) 2014-03-15 23:12:12.736 NuclearGame[1418:60b] Text: Game Paused 2014-03-15 23:12:12.737 NuclearGame[1418:60b] Text color: UIDeviceRGBColorSpace 0 0 0 0 2014-03-15 23:12:13.248 NuclearGame[1418:60b] Paused 2014-03-15 23:12:13.251 NuclearGame[1418:60b] Text Postion: (160.000000, 320.000000) 2014-03-15 23:12:13.252 NuclearGame[1418:60b] Text: Game Paused 2014-03-15 23:12:13.254 NuclearGame[1418:60b] Text color: UIDeviceRGBColorSpace 0 0 0 1

However, on screen the label will have appeared (since it was called at the start) then disappear just fine, but never reappear on screen!

EDIT: Label still did not show up...

CodeSmile
  • 64,284
  • 20
  • 132
  • 217

1 Answers1

0

I believe you're not seeing it because you are setting the pause label's position to (160, 320) in the touchesBegan: method in its own coordinate system so it draws it off screen. You don't need to set its position after its already been set in its parent node's coordinate system.

Give your pause label node the name "pauseLabel". This name will be used to detect if the node was touched in the touchesBegan: method.

//.h
{SKLabelNode *PausedLabel22;}
//.m
PausedLabel22= [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
PausedLabel22.name = @"pauseLabel";
PausedLabel22.text = @"Game Paused";
PausedLabel22.fontSize = 30;
PausedLabel22.fontColor = [SKColor blackColor];
PausedLabel22.position = CGPointMake(160, 340);
[self addChild:PausedLabel22];

touches began:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        for (UITouch *touch in touches) {
            SKNode *node = [self nodeAtPoint:[touch locationInNode:self]];
            if (node != self && [node.name isEqualToString:@"pauseLabel"]) {
                if (paused == YES) {
                    paused = NO;
                    [PausedLabel22 removeFromParent];
                } else {
                    paused = YES;
                    [self addChild:PauseLabel22];
                }
                return;
            }
        }
    }

Let us know if this works.

Here is also a great class to use to create buttons in spriteKit: https://stackoverflow.com/a/19199748/2526115

Community
  • 1
  • 1
Ivan Lesko
  • 1,274
  • 13
  • 26
  • Thanks, but it did not work, but the NSLogs still thought that everything was working, is it possible that by adding a child in the touchesBegan method it doesn't add them to the 'top' layer. – Andrew John Weeks Mar 16 '14 at 08:19