显示不同的SKScene Swift

时间:2016-10-08 18:25:45

标签: ios swift sprite-kit skscene

我正在使用精灵工具包创建一个应用程序,我正在尝试从非默认情景" GameScene"中加载它。我已经创建了一个名为" Level1"的新.sks文件。并创建了一个名为" Level1.swift"的Cocoa Touch类。但是," Level1.swift"仍然没有在应用程序中显示。

以下是Level1.swift的代码:

    import Foundation
    import SpriteKit

    class Level1: SKScene {
    var tile = Tile(tile: SKShapeNode(rect: CGRect(x: 0, y: 0, width: 320, height: 320), cornerRadius: 10.0))
var tile2 = Tile(tile: SKShapeNode(rect: CGRect(x: 0, y: 0, width: 320, height: 320), cornerRadius: 10.0))
var tile3 = Tile(tile: SKShapeNode(rect: CGRect(x: 0, y: 0, width: 320, height: 320), cornerRadius: 10.0))
var tile4 = Tile(tile: SKShapeNode(rect: CGRect(x: 0, y: 0, width: 320, height: 320), cornerRadius: 10.0))

override func didMove(to view: SKView) {
    backgroundColor = UIColor.white

    tile.color = UIColor.green
    tile.maxTileNumberCounter = 4
    tile.tileNumber = 1
    tile.initializeTile()

    tile2.color = UIColor.blue
    tile2.maxTileNumberCounter = 4
    tile2.tileNumber = 2
    tile2.initializeTile()

    tile3.color = UIColor.blue
    tile3.maxTileNumberCounter = 4
    tile3.tileNumber = 3
    tile3.initializeTile()

    tile4.color = UIColor.blue
    tile4.maxTileNumberCounter = 4
    tile4.tileNumber = 4
    tile4.initializeTile()



    self.addChild(tile.tile)
    self.addChild(tile2.tile)
    self.addChild(tile3.tile)
    self.addChild(tile4.tile)
}

这是我的viewController的代码:

import UIKit
import SpriteKit
import GameplayKit

class GameViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    if let view = self.view as! SKView? {
        // Load the SKScene from 'GameScene.sks'
        if let scene = SKScene(fileNamed: "Level1") {
            // Set the scale mode to scale to fit the window
            scene.scaleMode = .aspectFill

            // Present the scene
            view.presentScene(scene)
        }

        view.ignoresSiblingOrder = true

        view.showsFPS = true
        view.showsNodeCount = true
    }
}

override var shouldAutorotate: Bool {
    return true
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    if UIDevice.current.userInterfaceIdiom == .phone {
        return .allButUpsideDown
    } else {
        return .all
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}

override var prefersStatusBarHidden: Bool {
    return true
}
}

非常感谢任何有助于此工作的帮助。

1 个答案:

答案 0 :(得分:1)

同意@ Knight0fDragon。您应该检查Level1.sks文件,将Custom Class设置为Level1检查员中的Custom Class。这是demo