objective-c – SpriteKit:如何创建基础物理接头

前端之家收集整理的这篇文章主要介绍了objective-c – SpriteKit:如何创建基础物理接头前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在两个SK物理机构之间创建简单的关节.但是,他们是奇怪的.
我很清楚锚点应该在场景坐标上.请看附件源代码.

例如,这是在矩形上附加一个小方块之后如何得到固定的关节.

-(void)createFixedJointOnScene:(SKScene*)scene

{

//Adding Rectangle

    SKSpriteNode* backBone = [[SKSpriteNode alloc] initWithColor:[UIColor whiteColor] size:CGSizeMake(20,200)];
backBone.position = CGPointMake(CGRectGetWidth(self.frame)/2.0,CGRectGetHeight(self.frame)/2.0);
backBone.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:backBone.size];
backBone.physicsBody.categoryBitMask = GFPhysicsCategoryRectangle;
backBone.physicsBody.collisionBitMask = GFPhysicsCategoryWorld;
[scene addChild:backBone];

//Adding Square
SKSpriteNode* head = [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size:CGSizeMake(40,40)];
head.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:head.size];
head.position = CGPointMake(backBone.position.x,backBone.position.y-40);
head.physicsBody.categoryBitMask = GFPhysicsCategorySquare;
head.physicsBody.collisionBitMask = GFPhysicsCategoryWorld;
[scene addChild:head];

//Pinning Rectangle and Square
NSLog(@"Head position %@",NSStringFromCGPoint(head.position));
SKPhysicsJointFixed* pin =[SKPhysicsJointFixed jointWithBodyA:backBone.physicsBody bodyB:head.physicsBody anchor:head.position];
[self.physicsWorld addJoint:pin];

}

https://dl.dropboxusercontent.com/u/62559842/PhysicsTest.zip

谢谢.

解决方法

谢谢Smick ..经过比较Smick的代码与我,我发现这两行的顺序正在引起问题.
head.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:head.size];
head.position = CGPointMake(backBone.position.x,backBone.position.y-40);

在设置Sprite物理身体之前,我设置了Sprite的位置,一切都开始正常工作.

head.position = CGPointMake(backBone.position.x,backBone.position.y-40);
head.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:head.size];

现在我已经将Smick的代码附加到了完整的代码,并将链接附在这里.请享用.

https://dl.dropboxusercontent.com/u/62559842/PhysicsTest_Final_Working.zip

原文链接:https://www.f2er.com/c/112355.html

猜你在找的C&C++相关文章