Search code examples
iosobjective-cskview

Should I use UIView's and UILabels to display Game details on top of SKView


I am new to game development and was wondering what is the best approach for displaying game data on screen.

I have an endless runner where the sprite collects gems and I want the total to be displayed on screen. I was looking at placing a UIView with a UILabel above the SKView and getting the label to update after each touch. I also want a game over screen to appear at the end and was also looking at creating this using a UIView.

Is this the best approach or should all data be kept within the SKView itself?

Hopefully this makes sense.


Solution

  • While I don't think there is anything very wrong with using UIKit components within a SpriteKit application, I would recommend using SpriteKit's own components as much as possible.

    For text I would recommend using SKLabelNode instead of UILabel.

    Also, for buttons and other GUI elements, I think SKSpriteNodes would be most sufficient.

    Simple functional button example:

    SKSpriteNode *fireNode = [SKSpriteNode spriteNodeWithImageNamed:@"fireButton.png"];
    fireNode.position = CGPointMake(fireButtonX,fireButtonY);
    fireNode.name = @"fireButtonNode";//how the node is identified later
    

    Handle touches:

    //handle touch events
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        CGPoint location = [touch locationInNode:self];
        SKNode *node = [self nodeAtPoint:location];
    
        //if fire button touched, bring the rain
        if ([node.name isEqualToString:@"fireButtonNode"]) {
             //do whatever...
        }
    }