Search code examples
objective-csprite-kithud

Unable to create static HUD in Sprite Kit


as I used a centerOnNode method to follow my main character (a chopper-pilot) when he flies through the map, I can't think of any possibility to place a nonmoving HUD with points and highscore on the upper side of the screen.

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

    SKNode *myWorld = [SKNode node];
    self.currentMyWorld = myWorld;
    [self addChild:myWorld];

    SKLabelNode *score = [SKLabelNode labelNodeWithFontNamed:@"Verdana-BoldItalic"];
    score.fontSize = 25;
    score.fontColor = [SKColor yellowColor];
    score.zPosition = 4;
    self.pointHUD = score;
    [myWorld addChild:score];

    }
    return self;
}


#pragma mark - didSimulatePhysics

- (void)didSimulatePhysics
{

    [self centerOnNode: [self childNodeWithName: @"//pilot"]]; 
}

- (void) centerOnNode: (SKNode *) node
{
    CGPoint cameraPositionInScene = [node.scene convertPoint:node.position
                                                    fromNode:node.parent];

    if (self.currentPilot.position.x < -400 && self.currentPilot.position.y > -400 && self.currentPilot.y < 400){node.parent.position = CGPointMake(400, node.parent.position.y - cameraPositionInScene.y); 
    }else if (self.currentPilot.position.x < -400 && self.currentPilot.position.y < -400){node.parent.position = CGPointMake(400, 400);
    }else if (self.currentPilot.position.x < -400 && self.currentPilot.position.y > 400){node.parent.position = CGPointMake(400, -400);
    }else if (self.currentPilot.position.x > 400 && self.currentPilot.position.y < -400){node.parent.position = CGPointMake(-400, 400); 
    }else if (self.currentPilot.position.x >400 && self.currentPilot.position.y > 400){node.parent.position = CGPointMake(-400, -400);

    }else if ((self.currentPilot.position.x > -400 && currentPilot.position.y < -400) || (self.currentPilot.x < 400 && self.currentPilot.y < -400))
    {node.parent.position = CGPointMake(node.parent.position.x - cameraPositionInScene.x,400);

    }else if ((self.currentPilot.position.x > -400 && self.currentPilot.position.y > 400) || (self.currentPilot.position.x < 400 && self.currentPilot.position.y > 400))
    {node.parent.position = CGPointMake(node.parent.position.x - cameraPositionInScene.x,-400);

    }else if (self.currentPilot.position.x > 400 && self.currentPilot.position.y > -400 && self.currentPilot.position.y < 400){node.parent.position = CGPointMake(-393, node.parent.position.y - cameraPositionInScene.y); 
    }else if (self.currentPilot.position.x > -400 && self.currentPilot.position.y < 400 && self.currentPilot.position.y > -400 && self.currentPilot.position.y < 400){node.parent.position = CGPointMake(node.parent.position.x - cameraPositionInScene.x,node.parent.position.y - cameraPositionInScene.y);
    }
}

Solution

  • Ok, I just figured out that the solution is pretty basic and simple. I just added another SKNode in my scene.

        SKNode *HUD = [SKNode node];
        self.HudNode = HUD;
        [self addChild:HUD];
    
        SKLabelNode *highscore = [SKLabelNode labelNodeWithFontNamed:@"Verdana-BoldItalic"];
        highscore.fontSize = 25;
        highscore.fontColor = [SKColor yellowColor];
        highscore.zPosition = 4;
        self.highScoreLabel = highscore;
        [HudNode addChild:highscore];
    

    Now the position of the HUD is independent from the pilot's position