1

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!

4

1 に答える 1

2

これはすべてより適切に整理できることに注意してください。個人的には、SKShapeNode はどのような形でも好きではありません :) が、これはそれを行う 1 つの方法です。

#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;
于 2015-03-31T01:32:44.840 に答える