Objective-C SpriteKit 创建到某些点的虚线

标签 objective-c position sprite-kit touch dotted-line

我在屏幕底部有一个特定的点。 . .当我在某处触摸屏幕时,我希望在该点和我手指所在的点之间出现一条虚线。线条的长度和旋转将根据我手指的位置或移动到的位置而改变。

我假设我会用重复的小线图像制作虚线,但我想这就是我需要你帮助的原因!

最佳答案

请注意,所有这些都可以更好地组织起来,我个人不喜欢任何形状的 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;

}

结果是:

drawing dashed image

此外,我不知道它的性能如何,但您可以对其进行测试、调整和改进。或者像你说的那样使用 SKSpriteNode 。编码愉快!

编辑:

我刚注意到你说的是点线(不是虚线):)

您必须将模式更改为:

 pattern[0] = 3.0;
 pattern[1] = 3.0;

关于Objective-C SpriteKit 创建到某些点的虚线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45021023/

相关文章:

ios - 在 iOS 8.1 中的 UIViewController viewDidLoad 之后查看层次结构不完整

ios - 椭圆曲线加密的填充类型是什么

iphone - Objective C 中的 int 问题

svg - 使用 d3 javascript 库时在路径上的特定点放置箭头(标记)

ios - 如何将 SKPhysicsBody 附加到像 Spring 一样的节点?

ios - Spritekit 使用关卡

objective-c - 使用 NSRegularExpression 将每个匹配项替换为方法的结果

css - 位置:固定和宽度:自动

css - 如何将 div 弹出对话框定位到浏览器屏幕的中心?

ios - 快速在函数之间传递变量