问题描述
我尝试制作一个视图控制器,即屏幕的上半部分是SKScene,屏幕的下半部分是普通场景,我的意思是带有按钮等。这可能吗?我创建了一个Single View应用程序,并在下面添加了一些代码段,这些代码段是viewcontroller.swift文件中的代码。但是它不会在被点击的位置创建盒子,也不会像预期的那样自由移动。也许有一个问题是我在创建项目时创建了单视图应用程序,而不是选择游戏应用程序?也许需要启用/禁用Metal?我不是专业人士,因此只能猜测。
import UIKit
import SwiftUI
import SpriteKit
class GameScene: SKScene {
override func didMove(to view: SKView) {
physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
}
override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) {
guard let touch = touches.first else { return }
let location = touch.location(in: self)
let box = SKSpriteNode(color: .red,size: CGSize(width: 0.04,height: 0.04))
box.physicsBody = SKPhysicsBody(rectangleOf: box.frame.size)
box.position = location
addChild(box)
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
if let scene = SKScene(fileNamed: "GameScene") {
scene.scaleMode = .aspectFill
scene.backgroundColor = .clear
let transition = SKTransition.fade(withDuration: 1)
view.presentScene(scene,transition: transition)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
view.allowsTransparency = true
view.backgroundColor = UIColor.clear
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)