【发布时间】:2015-01-28 05:41:18
【问题描述】:
我正在尝试从场景中绘制的线创建SKPhysicsBody。这条线不是封闭的多边形,而是在跟踪touchesMoved 中的用户手指时创建的。我需要在这条线上创建一个物理体,以便一个球从它上面反弹,就好像它是一个壁架一样,但我似乎无法弄清楚如何做到这一点。
我找到的最接近的东西是bodyWithPolygonFromPath,但它只在线条被封闭成多边形时才有效。
我对 iOS 编程非常陌生(这是我的第一个项目),所以请放轻松!
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[lineNode removeFromParent];
CGPathRelease(pathToDraw);
UITouch* touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);
lineNode = [SKShapeNode node];
lineNode.path = pathToDraw;
lineNode.strokeColor = [SKColor redColor];
[self addChild:lineNode];
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch* touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
CGPathAddLineToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);
lineNode.path = pathToDraw;
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
lineNode.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:lineNode.path];
lineNode.physicsBody.categoryBitMask = paddleCategory;
lineNode.physicsBody.collisionBitMask = ballCategory | paddleCategory;
lineNode.physicsBody.contactTestBitMask = ballCategory | paddleCategory;
}
【问题讨论】:
标签: ios objective-c sprite-kit