Ios Sprite套件销接头的固定点似乎不正确
我正在用Sprite套件测试销接头,我发现一些不寻常的事情发生了 我想要的设置是:一个宽而平的盒子和两个圆;这些圆通过SKPhysicSpinpoints连接到长方体,因此它们可以充当轮子 这是我的密码。我已经尽力使它尽可能简洁:Ios Sprite套件销接头的固定点似乎不正确,ios,objective-c,sprite-kit,Ios,Objective C,Sprite Kit,我正在用Sprite套件测试销接头,我发现一些不寻常的事情发生了 我想要的设置是:一个宽而平的盒子和两个圆;这些圆通过SKPhysicSpinpoints连接到长方体,因此它们可以充当轮子 这是我的密码。我已经尽力使它尽可能简洁: - (SKNode*) createWheelWithRadius:(float)wheelRadius { CGRect wheelRect = CGRectMake(-wheelRadius, -wheelRadius, wheelRadius*2, wh
- (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];
}
我期望的是,销接头锚固在其中心点;然而,当我测试这一点时,关节的锚似乎离得很远
我是否遗漏了一些非常明显的东西?试试这段代码,我使用了你的代码,但遇到了一些奇怪的问题,所以从零开始
- (SKShapeNode*) makeWheel
{
SKShapeNode *wheel = [[SKShapeNode alloc] init];
CGMutablePathRef myPath = CGPathCreateMutable();
CGPathAddArc(myPath, NULL, 0,0, 16, 0, M_PI*2, YES);
wheel.path = myPath;
return wheel;
}
- (void) createCar
{
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:CGRectMake(0, 0, self.size.width, self.size.height)];
// 1. car body
SKSpriteNode *carBody = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(120, 8)];
carBody.position = CGPointMake(200, 200);
carBody.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:carBody.size];
[self addChild:carBody];
// 2. wheels
SKShapeNode *leftWheel = [self makeWheel];
leftWheel.position = CGPointMake(carBody.position.x - carBody.size.width / 2, carBody.position.y);
leftWheel.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:16];
[self addChild:leftWheel];
SKShapeNode *rightWheel = [self makeWheel];
rightWheel.position = CGPointMake(carBody.position.x + carBody.size.width / 2, carBody.position.y);
rightWheel.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:16];
[self addChild:rightWheel];
// 3. Join wheels to car
[self.physicsWorld addJoint:[SKPhysicsJointPin jointWithBodyA:carBody.physicsBody bodyB:leftWheel.physicsBody anchor:leftWheel.position]];
[self.physicsWorld addJoint:[SKPhysicsJointPin jointWithBodyA:carBody.physicsBody bodyB:rightWheel.physicsBody anchor:rightWheel.position]];
// 4. drive car
[carBody.physicsBody applyForce:CGVectorMake(10, 0)];
}
我也有这个问题,原因是在设置精灵位置之前设置了物理体
carNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:carNode.size];
carNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
将上述内容更改为
carNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
carNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:carNode.size];
它应该会起作用。谢谢你,斯密克
很好的小例子,在TVC中,您可以在TableView中注释出NumberOfSection,对于numberOfRowsInSection,只需使用[\u dataSourceArray count]。这是一个很好的例子,您应该添加[head.physicsBody applyTorque:0.2];连接到销连接,以真正显示其与固定连接的差异。干得好-谢谢分享。谢谢你,斯密克。。我更新了示例。但是,TVC是这样编码的,因为它有两个部分。我无法将您的汽车示例添加为关节类型。:)该示例放在不同的部分。我现在看不到示例中的汽车,仍然只有接头列表。我将把我所有的测试代码放进一个TVC-酷的想法中@巴万:我用你的密码连接到斯普林泰诺德。但它改变了我的spritenode的位置。你能帮帮我吗?非常感谢!我将Bavan的答案标记为正确,因为它更简洁,更便于未来读者理解,但您首先提供了正确的解决方案。@DogCoffee:我已使用您的代码连接到Spritodes。但它改变了我的spritenode的位置。你能帮帮我吗?@Nirav我建议你发一个新问题。我有一段时间没用雪碧套装了。