我通常在处理代码时遇到麻烦,但是当我在Xcode上作为游乐场运行它时,它可以正常工作,但是当我在iPad Pro上的游乐场应用程序中运行它时,它给了我错误:“出现了问题运行这个游乐场。请检查您的代码,然后重试。”谁能帮我这个?谢谢。这是我的代码:
import Foundation
import SwiftUI
import PlaygroundSupport
import AVFoundation
import SpriteKit
struct ContentView: View {
var body: some View {
GameView()
}
}
struct GameView: UIViewRepresentable {
let scene = GameScene(fileNamed: "GameScene2")
func makeUIView(context: Context) -> SKView {
// Let SwiftUI handle the sizing
return SKView(frame: .zero)
}
func updateUIView(_ uiView: SKView, context: Context) {
scene?.scaleMode = .aspectFit
uiView.presentScene(scene)
}
}
class GameScene: SKScene {
private var thrustButton: SKSpriteNode!
private var speedLabel: SKLabelNode!
var gameView = SKView()
private var moonSurface: SKSpriteNode!
private var moonLander: SKSpriteNode!
private var altimiter: SKLabelNode!
let cam = SKCameraNode()
override func didMove(to view: SKView) {
// Get label node from scene and store it for use later
super.didMove(to: view)
self.gameView = view
self.camera = cam
altimiter = childNode(withName: "//altimiter") as? SKLabelNode
thrustButton = childNode(withName: "//thrustButton") as? SKSpriteNode
speedLabel = childNode(withName: "//speedLabel") as? SKLabelNode
moonLander = childNode(withName: "//moonLander") as? SKSpriteNode
moonSurface = childNode(withName: "//moonSurface") as? SKSpriteNode
}
@objc static override var supportsSecureCoding: Bool {
// SKNode conforms to NSSecureCoding, so any subclass going
// through the decoding process must support secure coding
get {
return true
}
}
func touchDown(atPoint pos : CGPoint) {
}
func touchMoved(toPoint pos : CGPoint) {
}
func touchUp(atPoint pos : CGPoint) {
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { touchDown(atPoint: t.location(in: self))
let location = t.location(in: self)
let touchedNode = atPoint(location)
if touchedNode.name == "thrustButton" {
moonLander.physicsBody?.applyForce(CGVector(dx: 0, dy: 40))
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { touchMoved(toPoint: t.location(in: self)) }
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { touchUp(atPoint: t.location(in: self)) }
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { touchUp(atPoint: t.location(in: self)) }
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
cam.position = moonLander.position
speedLabel.position = CGPoint(x: moonLander.position.x - 400, y: moonLander.position.y + 450)
speedLabel.color = .white
thrustButton.position = CGPoint(x: moonLander.position.x - 370, y: moonLander.position.y - 500)
altimiter.position = CGPoint(x: moonLander.position.x - 400, y: moonLander.position.y + 380)
speedLabel.text = "Your Speed is: \(moonLander.physicsBody!.velocity.dy.rounded())"
altimiter.text = "Your altitude is: \(moonLander.position.y - moonSurface.position.y.rounded() - 1937)"
}
}
PlaygroundPage.current.liveView = UIHostingController(rootView: ContentView().environmentObject(PlayerUIView()))
这是一个错误;删除Playgrounds应用,然后重试。