我很清楚锚点应该在场景坐标上.请看附件源代码.
例如,这是在矩形上附加一个小方块之后如何得到固定的关节.
-(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 SquareSKSpriteNode* 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 SquareNSLog(@"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
总结以上是内存溢出为你收集整理的objective-c – SpriteKit:如何创建基础物理接头全部内容,希望文章能够帮你解决objective-c – SpriteKit:如何创建基础物理接头所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)