我正在测试与雪碧套件针关节,我发现一些不寻常的事情发生。雪碧套件针脚关节似乎有一个不正确的锚点
我想要的设置是这样的:一个宽的扁平盒子和两个圆圈;圆圈通过SKPhysicsPinJoints连接到盒子,因此它们可以充当轮子。
这是我的代码。我试图尽可能简洁:
- (SKNode*) createWheelWithRadius:(float)wheelRadius {
CGRect wheelRect = CGRectMake(-wheelRadius, -wheelRadius, wheelRadius*2, wheelRadius*2);
SKShapeNode* wheelNode = [[SKShapeNode alloc] init];
wheelNode.path = [UIBezierPath bezierPathWithOvalInRect:wheelRect].CGPath;
wheelNode.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:wheelRadius];
return wheelNode;
}
- (void) createCar {
// Create the car
SKSpriteNode* carNode = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(150, 50)];
carNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:carNode.size];
carNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:carNode];
// Create the left wheel
SKNode* leftWheelNode = [self createWheelWithRadius:30];
leftWheelNode.position = CGPointMake(carNode.position.x-80, carNode.position.y);
[self addChild:leftWheelNode];
// Create the right wheel
SKNode* rightWheelNode = [self createWheelWithRadius:30];
rightWheelNode.position = CGPointMake(carNode.position.x+80, carNode.position.y);
[self addChild:rightWheelNode];
// Attach the wheels to the body
CGPoint leftWheelPosition = leftWheelNode.position;
CGPoint rightWheelPosition = rightWheelNode.position;
SKPhysicsJointPin* leftPinJoint = [SKPhysicsJointPin jointWithBodyA:carNode.physicsBody bodyB:leftWheelNode.physicsBody anchor:leftWheelPosition];
SKPhysicsJointPin* rightPinJoint = [SKPhysicsJointPin jointWithBodyA:carNode.physicsBody bodyB:rightWheelNode.physicsBody anchor:rightWheelPosition];
[self.physicsWorld addJoint:leftPinJoint];
[self.physicsWorld addJoint:rightPinJoint];
}
我期待的是,销接头固定在它们的中心点;然而,当我测试这个时,关节的锚点似乎很遥远。
我是否错过了一些非常明显的东西?
不错的小例子,在你的TVC中,你可以注释掉numberOfSectionsInTableView,对于numberOfRowsInSection,只需使用[_dataSourceArray count]。它是一个很好的例子,你应该添加[head.physicsBody applyTorque:0.2];以引脚连接真正向固定连接显示其不同。好工作 - 感谢分享。 – DogCoffee
谢谢Smick ..我更新了示例。但是,TVC是这样编码的,因为它有两个部分。我无法将您的汽车示例添加为联合类型。 :)这个例子被放入不同的部分。 – Bavan
现在我不能在示例中看到汽车,仍然只有关节列表。我将把我所有的测试代码放入TVC - 很棒的主意! – DogCoffee