将 UITextView 添加到 SpriteKit 中的场景 [英] Adding UITextView to a scene in SpriteKit

查看:28
本文介绍了将 UITextView 添加到 SpriteKit 中的场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建一个游戏,并按照 Apple 的建议使用多个场景.您可以将游戏视为文本繁重的点击式冒险游戏.

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.

问题就在这里.大量文本,经过一些搜索,似乎是推荐的方式,或者更确切地说,目前唯一的方法是使用 UITextView 因此:

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];

如果你只有一个 SKScene,这一切都很好,因为这是当前视图/SKView 显示的场景.

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

我的问题是,当我将文本添加到场景的 *view 中时,该视图不是应用加载的第一个场景(第三个或更高),应用跳回它加载的第一个场景(即是我的菜单场景)并愉快地显示所需的文本.

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).

如果我想在每个场景中显示多个单词,请不要说我需要每个场景的视图,这真的没有意义,但也许我对 SpriteKit 的使用也没有.

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.

我仍然有些震惊,没有与 UITextView 等效的 SK.

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];
}

其中 GTMainMenuSKScene 的子类,并且有一个按钮(橙色框)到行为"(GTScene 的子类),它本身是 SKScene 的子类),这将导致转换到动作,它有另一个按钮到第一个场景.只有您永远不会出现在场景中,因为 viewDidLoad 会将您返回到主菜单.

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.

这是场景的viewDidLoad,它会导致它跳"回主菜单:

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];
}

此处有一个 git 存储库可用.

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.

请原谅我的代码,我的日常工作是 Java,目前我正在为 Objective C 中的某些概念而苦苦挣扎.哦,对不起,我设法包含了用户设置:S

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

推荐答案

您的视图控制器的 viewWillLayoutSubviews 方法无法防止重复执行.此方法不仅会在应用启动时运行,还会在每次视图旋转和调整大小时运行.

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 */ }

这篇关于将 UITextView 添加到 SpriteKit 中的场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆