我已經很簡單的遊戲。這就像使用加速計的經典迷宮遊戲,但用戶也可以使用3D Touch來改變矩形的大小。改變SKPhysicsBody,同時改變SKSpriteNode的位置
玩家的節點是矩形,其移動得益於世界物理學從加速計(在這裏沒有什麼特別的)獲取數據:
motionManager.startAccelerometerUpdates(to: OperationQueue.current!) { (data, error) in
if let motionData = data{
self.physicsWorld.gravity = CGVector(dx: motionData.acceleration.x * 7, dy: motionData.acceleration.y * 7)
}
}
和touchesMoved:
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first{
let maxForce = touch.maximumPossibleForce
let force = touch.force
let forceValue = force/maxForce
player.size = CGSize(width: 26-forceValue*16, height: 26-forceValue*16)
player.physicsBody = SKPhysicsBody(rectangleOf: player.size)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
resetSize()
}
func resetSize(){
player.size = CGSize(width: 26, height: 26)
}
所以,當我正在改變玩家的大小工作得很好:節點正在屏幕上移動,基於設備的方向,同時越來越緊,越來越小。問題是physicsBody(我在GameViewController.swift中設置了view.showsPhysics = true
)始終是相同的,所以我添加了player.physicsBody = SKPhysicsBody(rectangleOf: player.size)
,但現在當我按下屏幕時,播放器的節點調整大小,但它也停止移動。如何同時更改節點的大小和物理體積並保持運動?
人誰downvote應該表現出一個解決方案(但真正的物理瓦爾時間真正的傳播,而不是僞造..),並試圖解釋我的陳述,以幫助他們的OP相反的想法。 –
凍結時間是什麼意思?當我想要改變尺寸時,我應該停止運動嗎?尺寸沒問題後繼續運動球嗎? – codddeer123
這只是一個想法,但你可以找到其他模式:你可以創造類似於它周圍的「矩陣」電影場景的效果(降低音樂速度和遊戲元素的所有動作只有在這種狀態..) –