我想做一个钟摆。从 SKScene 开始,一切默认,我执行以下操作...
- (void)createSceneContents {
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.view.bounds];
// object 1 is the fulcrum
SKSpriteNode *object1 = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(5, 5)];
[self addChild:object1];
object1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:object1.frame.size];
object1.physicsBody.dynamic = NO;
object1.position = self.view.center;
// object2 is like a broomstick, which I will pin to the fulcrum
SKSpriteNode *object2 = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(90, 2)];
[self addChild:object2];
object2.anchorPoint = CGPointMake(0.0, 0.5);
object2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:object2.frame.size];
object2.position = self.view.center;
// pin the physics bodies
SKPhysicsJointPin *pinJoint = [SKPhysicsJointPin jointWithBodyA:object1.physicsBody bodyB:object2.physicsBody anchor:object1.position];
[self.physicsWorld addJoint:pinJoint];
}
如果我不添加引脚逻辑,支点会如预期的那样停留在场景的中间,并且扫帚柄会掉到地板上,所以我知道扫帚柄会受到重力的影响。添加引脚后,我得到了这个:
没有动静。为什么?这也不会导致任何运动......
[object2.physicsBody applyForce:CGVectorMake(0, -5)];
我希望扫帚柄会摆动和摆动,因为它固定在支点上。我看过关于先定位节点的文章,然后是关节,但我已经做到了。我期望扫帚摆动我错了吗?我怎样才能让它这样做?