问题
I have an object moving along the X-axis, while the other body rests. After a collision is made, I wish the resting object move along with the moving object.
I use this code to move the first body:
SKAction* moveBlock = [SKAction moveByX:-distanceToMove y:0 duration:0.01 * distanceToMove];
SKAction* removeBlock = [SKAction removeFromParent];
_moveBlocksAndRemove = [SKAction sequence:@[moveBlock, removeBlock]];
and in the collision detection I have tried to set a friction:
if (firstBody.categoryBitMask == categoryA && secondBody.categoryBitMask == categoryB) {
firstBody.friction = 1.0;
secondBody.friction = 1.0;
}
But that didn't work. applying velocity of the first body won't work since its velocity = 0;
Any ideas?
回答1:
I haven't tried that but it seems you want to add SKPhysicsJoint
to the scene:
if (firstBody.categoryBitMask == categoryA &&
secondBody.categoryBitMask == categoryB) {
SKPhysicsJointFixed* pin =
[SKPhysicsJointFixed jointWithBodyA:firstBody
bodyB:secondBody
anchor:firstBody.position];
[self.physicsWorld addJoint:pin];
}
来源:https://stackoverflow.com/questions/22083604/how-to-make-one-body-stick-to-another-moving-object-in-spritekit