HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – Sprite Kit销接头似乎有一个不正确的锚点大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在用Sprite Kit测试针脚接头,我发现异常发生.

我想要的设置是:一个宽,平的盒子和两个圆圈;圆圈通过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];
}

我期望的是针接头锚定在他们的中心点;然而,当我测试这个,关节的锚点似乎是远离的.

我错过了一些很明显的东西吗?

解决方法

我也有这个问题,原因是在设置精灵位置之前设置物理体.
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];

应该工作谢谢Smick

SpriteKit: How to create Basic Physics Joints

大佬总结

以上是大佬教程为你收集整理的ios – Sprite Kit销接头似乎有一个不正确的锚点全部内容,希望文章能够帮你解决ios – Sprite Kit销接头似乎有一个不正确的锚点所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。