2
votes

I have managed to draw a line with array of my random CGPoints.

    -(void)drawLine
    {
    SKShapeNode *mainLine = [SKShapeNode node];
    CGMutablePathRef pathToDraw = CGPathCreateMutable();
    CGPathMoveToPoint(pathToDraw, NULL, 0, 0);

    for(int i;i<pointsInMyView.count;i++)
    {
        CGPoint myPoint = [pointsInMyView[i] CGPointValue];
        CGPathAddLineToPoint(pathToDraw, NULL, myPoint.x, myPoint.y);
        mainLine.path = pathToDraw;
    }
    [mainLine setLineWidth:40];
    [mainLine setStrokeColor:[SKColor whiteColor]];
    mainLine.name = @"mainLine";
    [self addChild:mainLine];


}

As you can see I am drawing a SKShapeNode. My goal is to check collision of my SKSpriteNode with my line. But of course, this shape node makes a frame that contains all points of my line, and in this case my ShapeNode is all over my view. My SpriteNode detects collision with this ShapeNode all the time. I should draw multiple different ShapeNodes I guess, so every node would have its own frame. But if i do it this way, my line is not connected. Is there some solution to draw this node by node and still get nice line.

1
If your SKShapeNode is made up of random points, then it will spread all over the screen (I've just tried it). I think you will have to devise some algorithm to ensure that the line does not cross over itself, and forms a relatively simple line. Can you post your code that generates [pointsInMyView]? - Steve Ives
I know that my shape node is all ower my screen. The point is that I need those random points. My line is just a big shape node with vector lines from one point to another. This is ok. But now I am working on how to detect collision with my touch location and line that i drawn. Problem is that collision is detected all the time when I'm inside node frame. I want my touch wont respond on empty pixels of my shape node, just on pixels where my line is drawn. Any idea? - user3215624
Are you trying to detect a touch on the line or another sprite contacting the lines? - Steve Ives
I am trying to detect touch on my line that i drawn as a ShapeNode, so I need to ignore empty pixels of my ShapeNode. - user3215624

1 Answers

3
votes

Unfortunately nodes in SpriteKit cannot be concave (http://mathworld.wolfram.com/ConcavePolygon.html), with lines that cross over each other.

You will have to create separate nodes for each lines between adjacent points and then possibly join them together. you could do this either by:

  1. Create the 1st segment and then add subsequent segments as children of that 1st segment.
  2. Create a non-visible node for the whole line and add wll the line segments as children of that node.
  3. Create physicsbodies for each segment and join them with SKPhysicsJointFixed.