如何为新的.sks文件创建.swift文件? [英] How can I create a .swift file for my new .sks file?

查看:202
本文介绍了如何为新的.sks文件创建.swift文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Xcode时,我遇到了烦人的问题,即单击.SKS文件时Xcode总是崩溃.我在这里甚至在苹果开发人员论坛上都提出了问题,并在Internet上搜索了解决方案……但这是没有希望的.

When using Xcode, I've faced the annoying problem, which is Xcode always crashes when I click .SKS files. I have raised questions here, and even in Apple developer forum, as well as searched for the solutions on the Internet... but it is hopeless.

因为我要制作一个包含许多场景的游戏,所以如果我不能使用SpriteKit编辑器以简单的方式与这些场景进行交互,我想知道如何通过对它们的.swift文件进行编码来与这些场景进行交互.

Because I am making a game with many scenes, so if I can't use SpriteKit editor to interact with the scenes in an easy way, I want to know how can I interact with my scenes by coding their .swift files.

例如,问题是,例如,当我创建文件"EndScene.sks"时,如何创建与.sks文件链接的"EndScene.swift"?

So the question is, for example, when I create a file "EndScene.sks", how can I create a "EndScene.swift", which links with my .sks file?

非常感谢!

推荐答案

创建一个新的swift文件,并将其命名为与您的.sks文件相同的名称.假设您有一个名为MainMenu.sks的.sks文件.您将创建一个名为MainMenu.swift的快速文件.在该文件中,您需要创建一个从SKScene继承的Main Menu类.

Create a new swift file and name it the same as your .sks file. Let's say you have a .sks file called MainMenu.sks. You'd create a swift file called MainMenu.swift. Inside that file, you'll want to create a Main Menu class that inherits from SKScene.

import SpriteKit

class MainMenu: SKScene {

}

在其中放置所有代码.如您所说,密钥是将其链接到.sks文件.

Inside there is where you'll put all your code. The key is, as you said, linking this to the .sks file.

当您展示场景时,您将实例化该类并将其与.sks文件关联.

When you go to present your scene, you'll instantiate the class and associate it with the .sks file.

import UIKit
import SpriteKit

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if let scene = MainMenu(fileNamed:"MainMenu") {
            let skView = self.view as! SKView
            // Some settings applied to the scene and view
            // ... code ...

            skView.presentScene(scene)
        }
    }
//... other code
}

注意行let scene = MainMenu(fileNamed:"MainMenu").在这里实例化在MainMenu.swift.中创建的类,因此,从技术上讲,MainMenu().swift文件,而fileNamed: "MainMenu".sks文件.从技术上讲,您可以将任何.sks文件放入init调用中,它将渲染该场景.

Note the line let scene = MainMenu(fileNamed:"MainMenu"). That's where you are instantiating the class you created in MainMenu.swift. So, technically, MainMenu() is the .swift file and fileNamed: "MainMenu" is the .sks file. You can technically put any .sks file into the init call and it'll render that scene.

想象一下,一个迷宫赛跑者拥有所有逻辑的游戏.您可以在名为MazeScene的类中构建所有游戏逻辑,并仅制作一堆.sks文件,每个文件具有不同的迷宫.然后,您可以使用MazeScene(fileNamed: "MazeOne")MazeScene(fileNamed: "MazeThree")之类的格式.

Imagine having a game with all the logic for a maze runner. You could build all the game logic in a class called MazeScene and just make a bunch of .sks files, each with a different maze. You could then so something like, MazeScene(fileNamed: "MazeOne") or MazeScene(fileNamed: "MazeThree").

对于此文档,SKScene继承自SKNode,因此您将找到

For the documentation on this, SKScene inherits from SKNode so you'll find the documentation for init(fileNamed:) here

那应该带你走.

这篇关于如何为新的.sks文件创建.swift文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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