Question

I trying to create a game, and following Apple's advice I am using multiple scenes. You could consider the game to be a text heavy point and click adventure.

Therein lies the problem. Lots of text, having done a bit of a search, it seems the recommend way, or rather the current only way to do this is with a UITextView thus:

[self.view addSubview: textView];

This is all well and good if you only have one SKScene, as that is the scene being displayed by the current view/SKView.

My problem is, that when I add the text to my scene's *view, which isn't the first scene the app loaded (its the third, or higher), the app jumps back to the first scene it loaded (which is my menu scene) and happily displays the required text.

Anybody got any idea why? I have menu scene transitioning to scene one, to scene two (in which I wish to display the test).

Please don't say I need a view per scene if I want to display more than a handful of words per scene, that just doesn't really make sense, but perhaps neither does my usage of SpriteKit.

I am still some what stunned there is no SK equivalent of the UITextView.

Anyway, any help, pointers would be great, thank you.

Ok, here are the relevant parts of the code.... I think. Controller:

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    // Configure the view.
    SKView *skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;
    skView.showsDrawCount = YES;

    // Create and configure the scene.
    SKScene *scene = [[GTMainMenu alloc] initWithSize:skView.bounds.size];

    // Present the scene.
    [skView presentScene:scene];
}

where GTMainMenu is a subclass of SKScene, and has a button (orange box) to an "act" (A subclass of GTScene, itself a subclass of SKScene), this will cause a transition to the act, which has another button to the first scene. Only you never make it to the scene, as the viewDidLoad returns you to the main menu.

This is the viewDidLoad of the scene, which will cause it to "jump" back to the main menu:

- (void)didMoveToView:(SKView *)view
{
    [super didMoveToView:view];

    if (!self.contentCreated) {
        [self createSceneContents];

        self.contentCreated = YES;
    }

    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(self.size.width/2, self.size.height/2+20, 200, 40)];
    textView.center = self.view.center;
    textView.textColor = [UIColor blackColor];
    textView.font = [UIFont systemFontOfSize:17.0];
    textView.backgroundColor = [UIColor whiteColor];
    textView.text = @"Where am I?";

    [self.view addSubview:textView];
}

There is a git repo available here.

This is a striped down version of my project, removing everything that is unrelated to the issue at hand.

If you will excuse the code, my day job is Java, and I am struggling with certain concepts in Objective C at the moment. Oh and sorry I managed to include the usersettings :S

Was it helpful?

Solution

Your view controller's viewWillLayoutSubviews method is not safeguarded against repeated execution. This method will not just run at app launch but every time the view rotates and resizes.

Add a check before creating/presenting a scene in that method:

if(self.view.scene == nil) { /* present scene */ }

OTHER TIPS

Have you looked into SKLabelNode? I used it extensively in my SpriteKit game. If you need your SKLabelNode to do anything fancy (physics, etc.), you can just add it to a parent SKSpriteNode.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top