【发布时间】:2014-05-03 06:16:10
【问题描述】:
我正在开发一款游戏,我在屏幕底部有一条线,我用它来将东西发射到空中。它的行为应该像橡皮筋或弹弓。我已经将一些可行的方法组合在一起,但这是一个糟糕的解决方案,我希望有人可以提出另一种方法。我的方法基本上涉及通过在 touchesMoved 方法期间重复调用 draw 方法来重绘可变路径。再说一次,我知道这是一种不好的做法,对于糟糕的代码非常抱歉。
-(void)drawLine:(CGPoint)location
{
[_powerLine removeFromParent];
CGPoint pointTL = CGPointMake(19, 131);
CGPoint pointTR = CGPointMake(308, 131);
CGPoint pointBL = CGPointMake(location.x-10, location.y);
CGPoint pointBR = CGPointMake(location.x+10, location.y);
UIBezierPath *lineShape = [UIBezierPath bezierPath];
[lineShape moveToPoint:pointTL];
[lineShape addLineToPoint:pointBL];
[lineShape addLineToPoint:pointBR];
[lineShape addLineToPoint:pointTR];
_powerLine = [SKShapeNode node];
_powerLine.path = lineShape.CGPath;
_powerLine.lineWidth = 2.0;
_powerLine.strokeColor = [SKColor colorWithRed:0.0 green:0 blue:0 alpha:1];
[self addChild:_powerLine];
CGMutablePathRef powerLinePath = CGPathCreateMutable();
CGPathMoveToPoint(powerLinePath, nil, pointTL.x, pointTL.y);
CGPathAddLineToPoint(powerLinePath, nil, pointBL.x, pointBL.y);
CGPathAddLineToPoint(powerLinePath, nil, pointBR.x, pointBR.y);
CGPathAddLineToPoint(powerLinePath, nil, pointTR.x, pointTR.y);
_powerLine.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:powerLinePath];
_powerLine.physicsBody.categoryBitMask = WFPhysicsCategoryPowerline;
_lastBR = pointBR;
_lastBL = pointBL;
}
我希望有更好的方法来做到这一点,而不是在人们拉下线以将物体射向空中时不断地重新绘制它。我查看了弹簧接头,但无法说服他们工作。我在使用弹簧接头时遇到的另一个问题是如何让图像拉伸以匹配线的位置。这种方法通过简单地消除图像来解决试图拉伸图像的问题。使用弹簧会很好,这样我就可以避免手动编写物理代码。
有人对如何做到这一点有想法吗?
【问题讨论】:
标签: ios sprite-kit physics