1

This is my code. The music is not playing for some reason and it started when I added my physicsWorld function. I already checked my sound mp3 file and it seems to be working. I even ran a previous version (same but without the physicsWorld stuff) and the music works perfectly. I'm not sure why this is happening... Your help would be greatly appreciated :)

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()
    }
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
AglaiaZ
  • 31
  • 6

0 Answers0