问题描述
我在屏幕底部有一个点...当我在某处触摸屏幕时,我希望在该点和我的手指所在的点之间出现一条虚线.线条的长度和旋转将根据我手指的位置或移动到的位置而改变.
I have a certain point at the bottom of the screen . . . when I touch the screen somewhere, I'd like a dotted line to appear between the point, and the point my finger is at. The length and rotation of the line will change based on where my finger is, or moves to.
我假设我会用重复的小线条图像来制作虚线,但我想这就是我需要你帮助的原因!
I'm assuming I'd make the dotted line with a repetition of a small line image, but I guess that's why I need your help!
推荐答案
请注意,所有这些都可以更好地组织,我个人不喜欢任何形状的 SKShapeNode :) 或形式,但这是一种方法它:
Note that all this can be organized better, and I personally don't like SKShapeNode in any shape :) or form, but this is the one way to do it:
#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 .编码愉快!
Also I don't know how much performant this is, but you can test it, tweak it and improve it. Or even use SKSpriteNode like you said. Happy coding!
编辑:
我刚刚注意到你说的是虚线(不是虚线):)
I just noticed that you said dotted (not dashed) :)
您必须将模式更改为:
pattern[0] = 3.0;
pattern[1] = 3.0;
这篇关于Objective-C SpriteKit 创建虚线到某些点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!