遵循this Free PoBo site中的示例,但希望创建“交叉”(或“加号”)形状,而不是仅跷跷板。如何用Sprite Kit Physics创建一个“交叉”(或“加号”)形状的旋转物体?
上传一个小的测试项目here,基本上它创建一个场景,如下图所示,一些跷跷板与我试图把在横穿东西取代他们中的一个。
当您点上的任何位置上屏幕上,它会创建一个小球或方块,然后从您敲击的地方落下。当物体撞击跷跷板时,它会按预期旋转。
问题是,交叉物体并不总是按预期旋转 - 只有当物体碰到交叉形状物体的中间绿色圆圈时,它才会旋转。否则,该对象只能通过蓝色矩形下降,就好像它不存在一样。不知道我做错了什么?
这是如何在代码中添加“交叉”(或“加号”)形状。任何帮助表示感谢,谢谢!
....
float wWidth = 100.0f;
float wHeight = 10.0f;
....
[self addCross:CGPointMake(self.frame.size.width*0.75f, self.frame.size.height*0.60f)
game:self
whipWidth:wWidth*0.5f
whipHeight:wHeight];
....
-(CGMutablePathRef)getCGMutablePathRef:(float)ww thickness:(float)tt {
CGMutablePathRef boundingPath = CGPathCreateMutable();
float halfTT = tt*0.5;
CGPathMoveToPoint(boundingPath, NULL, halfTT, halfTT);
CGPathAddLineToPoint(boundingPath, NULL, halfTT+ww, halfTT);
CGPathAddLineToPoint(boundingPath, NULL, halfTT+ww, -halfTT);
CGPathAddLineToPoint(boundingPath, NULL, halfTT, -halfTT);
CGPathAddLineToPoint(boundingPath, NULL, halfTT, -halfTT-ww);
CGPathAddLineToPoint(boundingPath, NULL, -halfTT, -halfTT-ww);
CGPathAddLineToPoint(boundingPath, NULL, -halfTT, -halfTT);
CGPathAddLineToPoint(boundingPath, NULL, -halfTT-ww, -halfTT);
CGPathAddLineToPoint(boundingPath, NULL, -halfTT-ww, halfTT);
CGPathAddLineToPoint(boundingPath, NULL, -halfTT, halfTT);
CGPathAddLineToPoint(boundingPath, NULL, -halfTT, halfTT+ww);
CGPathAddLineToPoint(boundingPath, NULL, halfTT, halfTT+ww);
CGPathAddLineToPoint(boundingPath, NULL, halfTT, halfTT);
return boundingPath;
}
-(void)addCross:(CGPoint)pos game:(SKScene*)game whipWidth:(float)whipWidth whipHeight:(float)whipHeight {
idNumber++;
SKSpriteNode * anchor = [SKSpriteNode spriteNodeWithColor:[UIColor clearColor] size:CGSizeMake(1, 1)];
anchor.position = pos;
anchor.name = [NSString stringWithFormat:@"whip_anchor-%d",idNumber];
anchor.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:1];
anchor.physicsBody.affectedByGravity = false;
anchor.physicsBody.dynamic = false;
[game addChild:anchor];
CGMutablePathRef boundingPath = [self getCGMutablePathRef:whipWidth thickness:whipHeight];
CGMutablePathRef boundingPath2 = [self getCGMutablePathRef:whipWidth thickness:whipHeight];
SKShapeNode* polygon = [[SKShapeNode alloc]init];
polygon.path = CGPathCreateMutableCopy(boundingPath2);
polygon.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:boundingPath];
CGPathRelease(boundingPath);
CGPathRelease(boundingPath2);
polygon.name = @"polygon";
polygon.lineWidth = 0.01;
polygon.fillColor = [SKColor blueColor];
polygon.lineWidth = 0;
polygon.physicsBody.affectedByGravity = NO;
[anchor addChild:polygon];
SKSpriteNode * gear = [SKSpriteNode spriteNodeWithImageNamed:@"ball"];
gear.size = CGSizeMake(whipHeight+5, whipHeight+5);
gear.name = [NSString stringWithFormat:@"gear-%d",idNumber];
[anchor addChild:gear];
SKPhysicsJointPin* jointPin = [SKPhysicsJointPin jointWithBodyA:anchor.physicsBody
bodyB:polygon.physicsBody
anchor:anchor.position];
[game.physicsWorld addJoint:jointPin];
idNumber++;
}
的横仅仅是两个重叠的矩形 – LearnCocos2D 2014-08-29 11:05:40
感谢@ LearnCocos2D,但如果I类基础上的原代码从“http://www.free-pobo.com/build-a-simple-seesaw- with-sprite-kit-physics /“,然后添加另一个SKSpriteNode并将其命名为”whip2“,然后再将另一个SKPhysicsJointPin加入到相同的”锚点“中,只显示第二个。任何想法如何显示两个矩形?或者是否有任何限制可以添加SKSpriteNode的SKPhysicsJointPin?谢谢。 – New2ObjectiveC 2014-08-30 11:34:26
您可以简单地使用bodyWithRectangleOfSize而不是此处使用的多边形。 – ZeMoon 2014-09-01 13:09:35