Objective-C SpriteKit创建到某些点的虚线 [英] Objective-C SpriteKit Create dotted line to certain points

查看:130
本文介绍了Objective-C SpriteKit创建到某些点的虚线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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创建到某些点的虚线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆