我的 xCode 项目有问题。我有一个完整的 gamescene.swift 代码文件(600 行)。但是代码不会运行。当我在模拟器中运行我的游戏时,模拟器会显示标准的 gamescene.sks 颜色以及我的广告,但它不会显示我的任何代码。我已经检查过 gamescene.sks 中的自定义类是否正确。一切正常,除了 gamescene.swift 代码,它不会运行。
import UIKit
import SpriteKit
import GameplayKit
import GoogleMobileAds
class GameViewController: UIViewController {
@IBOutlet weak var GoogleBannerView: GADBannerView!
@IBOutlet weak var MainMenu: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
GoogleBannerView.adUnitID = "ca-app-pub-13***46014918193/236762****"
GoogleBannerView.rootViewController = self
GoogleBannerView.load(GADRequest())
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .resizeFill
// Present the scene
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = false
view.showsNodeCount = false
view.showsPhysics = 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
}
最佳答案
经过无数小时的尝试,我设法解决了这个问题。
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = GameScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .resizeFill
这是工作代码。问题是我没有具体说明我应该领导什么场景,而是写了
if let scene = SKScene(fileNamed: "GameScene")
简单的修复方法是将“SKScene”更改为我的 SKScene 名称。
if let scene = GameScene(Filenamed: "GameScene")
希望这能帮助无数其他遇到此问题的人!
关于swift - Gamescene.swift 和 Gamescene.sks 不能一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42269537/