这是我的代码。音乐由于某种原因无法播放,并且在我添加physicsWorld函数时开始播放。我已经检查了我的声音mp3文件,并且似乎可以正常使用。我什至运行了以前的版本(相同但没有PhysicalWorld的东西),并且音乐运行完美。我不确定为什么会这样...您的帮助将不胜感激:)
import UIKit
import QuartzCore
import SceneKit
var ballNode = SCNNode()
var floorNode = SCNNode()
class GameViewController: UIViewController, SCNPhysicsContactDelegate {
let categoryFloor = 2
var scene: SCNScene!
override func viewDidLoad() {
super.viewDidLoad()
scene = SCNScene(named: "art.scnassets/main.scn")!
let lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light!.type = .ambient
lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
scene.rootNode.addChildNode(lightNode)
let ball = scene.rootNode.childNode(withName: "ball", recursively: true)!
ballNode = scene.rootNode.childNode(withName: "ball", recursively: true)!
ballNode.physicsBody = SCNPhysicsBody.dynamic()
ballNode.physicsBody!.contactTestBitMask = 1
floorNode = scene.rootNode.childNode(withName: "floor", recursively: true)!
floorNode.physicsBody = SCNPhysicsBody.static()
floorNode.physicsBody!.contactTestBitMask = 1
scene.rootNode.addChildNode(ballNode)
scene.rootNode.addChildNode(floorNode)
let camera = scene.rootNode.childNode(withName: "camera", recursively: true)!
ball.runAction(SCNAction.repeatForever(SCNAction.moveBy(x: 0, y: 0, z: 10, duration: 1)))
camera.runAction(SCNAction.repeatForever(SCNAction.moveBy(x: 0, y: 0, z: 10, duration: 1)))
let scnView = self.view as! SCNView
scnView.scene = scene
scnView.scene?.physicsWorld.contactDelegate = self
scnView.allowsCameraControl = false
scnView.showsStatistics = true
if let source = SCNAudioSource(fileNamed: "art.scnassets/gameMusic.mp3") {
let action = SCNAction.playAudio(source, waitForCompletion: false)
ball.runAction(action)
} else {
print("cannot find file")
}
addSwipeGestureRecognizers()
}
func addSwipeGestureRecognizers() {
let gestureDirections: [UISwipeGestureRecognizerDirection] = [.right, .left, .up, .down]
for gestureDirection in gestureDirections {
let gestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector((handleSwipe)))
gestureRecognizer.direction = gestureDirection
self.view?.addGestureRecognizer(gestureRecognizer)
}
}
func physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) {
if (contact.nodeA == ballNode || contact.nodeA == floorNode) && (contact.nodeB == ballNode || contact.nodeB == floorNode) {
ballNode.removeFromParentNode()
}
}
@objc func handleSwipe(gesture:UIGestureRecognizer) {
let jumpUpAction = SCNAction.moveBy(x: 0, y: 10, z: 0, duration: 0.2)
let jumpDownAction = SCNAction.moveBy(x: 0, y: -10, z: 0, duration: 0.2)
let up = SCNAction.sequence([jumpUpAction, jumpDownAction])
let right = SCNAction.moveBy(x: -10, y: 0, z: 0, duration: 0.2)
let left = SCNAction.moveBy(x: 10, y: 0, z: 0, duration: 0.2)
let down = SCNAction.moveBy(x: 0, y: 0, z: 0, duration: 0.2)
if let gesture = gesture as? UISwipeGestureRecognizer{
switch gesture.direction {
case .up:
ballNode.runAction(up)
case .right:
ballNode.runAction(right)
case .left:
ballNode.runAction(left)
case .down:
ballNode.runAction(down)
default:
print("No Swipe")
}
}
}
override var shouldAutorotate: Bool {
return false
}
override var prefersStatusBarHidden: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return .allButUpsideDown
} else {
return .all
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}