
物理学
|
241
最终,新计算出来的重力向量被应用到场景的
physicsWorld
中,这将立即对场景
中的所有物体生效。
7.18
拖动物体
问题
我们想让玩家能够拖动屏幕中的物体。
解决方案
首先,创建两个实例变量:一个
SKNode
对象,变量名为
dragNode
,
一个
SKPhysicsJointPin
对象,变量名为
dragJoint
。
在
SKScene
子类代码中,加入下列方法:
override func touchesBegan(_ touches: Set<UITouch>,
with event: UIEvent?) {
//
我们只需要关心单点触摸
guard let touch = touches.first else {
return
}
//
找出当前被触摸的是哪个节点
let touchPosition = touch.location(in: self)
let touchedNode = self.atPoint(touchPosition)
//
确保我们正在拖动的节点的是我们所想的
guard let touchedPhysicsBody = touchedNode.physicsBody else {
return
}
//
创建一个隐形节点,并附上一个小的静态物体
let newDragNode = SKNode()
newDragNode.position = touchPosition ...