我在屏幕底部有一个特定的点。 . .当我在某处触摸屏幕时,我希望在该点和我手指所在的点之间出现一条虚线。线条的长度和旋转将根据我手指的位置或移动到的位置而改变。
我假设我会用重复的小线图像制作虚线,但我想这就是我需要你帮助的原因!
最佳答案
请注意,所有这些都可以更好地组织起来,我个人不喜欢任何形状的 SKShapeNode :) 或形式,但这是实现它的一种方法:
#import "GameScene.h"
@implementation GameScene{
SKShapeNode *line;
}
-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */
line = [SKShapeNode node];
[self addChild:line];
[line setStrokeColor:[UIColor redColor]];
}
-(void)drawLine:(CGPoint)endingPoint{
CGMutablePathRef pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
CGPathAddLineToPoint(pathToDraw, NULL, endingPoint.x,endingPoint.y);
CGFloat pattern[2];
pattern[0] = 20.0;
pattern[1] = 20.0;
CGPathRef dashed =
CGPathCreateCopyByDashingPath(pathToDraw,NULL,0,pattern,2);
line.path = dashed;
CGPathRelease(dashed);
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
[self drawLine:location];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
[self drawLine:location];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
line.path = nil;
}
结果是:
此外,我不知道它的性能如何,但您可以对其进行测试、调整和改进。或者像你说的那样使用 SKSpriteNode 。编码愉快!
编辑:
我刚注意到你说的是点线(不是虚线):)
您必须将模式更改为:
pattern[0] = 3.0;
pattern[1] = 3.0;
关于Objective-C SpriteKit 创建到某些点的虚线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45021023/