这篇文章将记录我在使用 Sprite Kit
过程中遇到的问题和解决方法。
private func initScene() {
let scnView = SKView(frame: CGRect(x: 0, y: topView!.bottom,
width: view.width, height: view.height - topView!.bottom))
view.addSubview(scnView)
let scene = PJPlacesAnnotationScene(size: scnView.frame.size)
scene.scaleMode = .aspectFill
scnView.presentScene(scene)
// scnView.showsPhysics = true
scnView.ignoresSiblingOrder = true
scnView.showsFPS = true
scnView.showsNodeCount = true
}
bottleViwSprite = SKSpriteNode(imageNamed: "")
bottleViwSprite?.size = CGSize()
bottleViwSprite?.position = CGPoint()
bottleViwSprite?.anchorPoint = CGPoint()
bottleViwSprite?.physicsBody = SKPhysicsBody(edgeLoopFrom: CGRect())
self.addChild(bottleViwSprite!)
let leftBox = SKSpriteNode(imageNamed: "")
leftBox.size = CGSize()
leftBox.position = CGPoint()
leftBox.physicsBody = SKPhysicsBody(rectangleOf: CGSize())
// 是否静止
leftBox.physicsBody?.isDynamic = false
bottleViwSprite?.addChild(leftBox)
let annotationSprite = SKSpriteNode(imageNamed: "")
annotationSprite.position = CGPoint()
annotationSprite.size = CGSize()
annotationSprite.physicsBody = SKPhysicsBody(circleOfRadius: )
annotationSprite.physicsBody?.usesPreciseCollisionDetection = true
annotationSprite.scale(to: CGSize())
self.bottleViwSprite?.addChild(annotationSprite)
func startMonitoringAcceleration(){
if motionManager.isAccelerometerAvailable {
// 感应时间
motionManager.accelerometerUpdateInterval = 0.2
motionManager.startAccelerometerUpdates(to: OperationQueue.current!) { (data, error) in
guard let accelerometerData = data else {
return
}
let acceleration = accelerometerData.acceleration
self.physicsWorld.gravity = CGVector(dx: acceleration.x * 9.8,
dy: acceleration.y * 9.8)
}
}
}