正在关注this Free PoBo site中的示例,但想要创建一个"交叉" (或"加")形状而不仅仅是跷跷板。
上传了一个小型测试项目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++;
}
答案 0 :(得分:1)
最后让它工作,而不是使用多边形绘制十字架,只需使用固定关节(SKPhysicsJointFixed)将2个矩形连接在一起。我的博客上发布了更新的代码(请参阅[2014-09-03更新]下的部分)。感谢。
http://new2objectivec.blogspot.com.au/2014/08/how-to-create-cross-or-plus-shape.html