我正在尝试制作一款游戏,你可以通过左右移动平台来控制平台,同时尝试在平台上平衡坠落的物体。然而,似乎平台的移动独立于落在其上的项目。例如,如果一个块落在我的平台上,当我向左或向右移动平台时,块保持不动,只有平台在它下面移动。这使得它不可能堆叠任何东西,因为块不会随着平台移动。我不确定需要使用哪些SpriteKit物理属性来模拟平衡。我试着调整块与平台碰撞时的弹性恢复,以及摩擦力属性,看看这是否有助于块“粘”到平台上,但这似乎也没有帮助。
我的平台不是动态的,所以我尝试设置
platform.physicsBody!.isDynamic = true
platform.physicsBody!.affectedByGravity = false
platform.physicsBody!.allowsRotation = false这似乎是有帮助的?但当平台与块发生碰撞时,平台仍然会移动,这是我不能拥有的。
编辑:
这就是我移动平台的方式。用户通过用手指拖动平台来移动父平台。由于这会移动平台的父级,因此平台也会随之移动。
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
let location = touch.locationInNode(self)
platformParent.position.x = location.x
}
}发布于 2017-02-22 20:49:52
你需要有动态的平台,所以你需要解决的是如何保持它在适当的位置。根据您的用例,有许多选项具有正确的选项。试着看看这些:
在平台和实体参考对象之间创建位置约束:SKConstraint.positionY
SKPhysicsJointSliding
发布于 2017-02-23 02:23:33
摩擦力将阻止积木滑出平台或其他积木(当堆叠时)。当物理物体的friction系数属性为非零时,存在法向力(在本例中为F=质量x重力),并且至少有一个物体随力或设置其速度属性移动时,就会发生摩擦。如果使用moveTo或moveBy操作移动平台或块,摩擦将不起作用。以下是如何在SpriteKit中使用摩擦力的示例实现。

首先,设置世界的重力属性。这是默认设置。
physicsWorld.gravity = CGVector(dx:0, dy:-9.8) 然后,设置平台的摩擦系数。对每个块执行相同的操作。
platform.physicsBody?.friction = 1.0设置块的质量属性以限制其重量。
block.physicsBody?.mass = 0.0001约束平台,以防止其从块的重量垂直移动。
let range = SKRange(constantValue: -self.size.height * 0.45)
let constraint = SKConstraint.positionY(range)
platform.constraints = [constraint]最后,使用物理方法将平台左右移动:
var moveTo:CGPoint?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let point = touch.location(in:self)
moveTo = point
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let point = touch.location(in:self)
moveTo = point
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
moveTo = nil
platform.physicsBody?.velocity = CGVector.zero
}
override func update(_ currentTime: TimeInterval) {
if let moveTo = moveTo {
let dx = moveTo.x - platform.position.x
let vector = CGVector(dx: 2*dx, dy: 0)
platform.physicsBody?.velocity = vector
}
}https://stackoverflow.com/questions/42380145
复制相似问题