1

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

Rashad
  • 11,057
  • 4
  • 45
  • 73
Gavin
  • 13
  • 5
  • Thank you for the edits. I am not sure this is iOS specific, though the "game" is an iOS game. I think this "issue" might exist on OSX too. – Gavin Mar 09 '14 at 19:43
  • Ok; I see, people follow tags. :D – Gavin Mar 09 '14 at 19:45
  • This sudden "jump back to previous scene" strongly indicates a programming error on your behalf, but you didn't provide any of the contextually relevant code you've written, so we can't make any recommendations. – CodeSmile Mar 09 '14 at 21:57
  • Added code examples, and link to a git repo – Gavin Mar 10 '14 at 00:23

2 Answers2

1

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 */ }
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • That would suggest my view controllers .h file is wrong as my view controller apparently doesnt have a scene property. `#import #import @interface GTViewController : UIViewController` – Gavin Mar 10 '14 at 08:41
  • Doh! Just a bit of casting required. – Gavin Mar 10 '14 at 08:44
  • Thank you LearnCocos2D, problem solved. Lesson learned, understand the life cycle of the framework. – Gavin Mar 10 '14 at 08:47
1

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.

Nick Barr
  • 554
  • 1
  • 7
  • 20
  • I need to add formatted text, and more than a few words, SKLabelNode doesnt seem able to handle this. It was my first call. What surprises me is that many games have screens that are text heavy, mission explanation screens, and yet as far as I can see SpriteKit doesnt support this. It could just be I am abusing the framework though. – Gavin May 02 '14 at 09:14
  • 1
    Try https://github.com/alex-alex/ASAttributedLabelNode, it is a fantastic wrapper around NSAttributedStrings. Writing up text heavy screens in an RTF file, with all of the formatting options that gives you, is so much easier. – Kevin Vaughan Aug 01 '15 at 16:15