draw a line in sprite kit in touchesmoved
Asked Answered
W

1

8

I would like to draw a line in sprite kit along the points collected in touchesmoved.

Whats the most efficient way of doing this? I've tried a few times and my line is either wrong on the y axis, or takes up a lot of processing power that the fps goes down to 10 a second.

Any ideas?

Wicks answered 13/11, 2013 at 12:29 Comment(0)
M
17

You could define a CGpath and modify it by adding lines or arcs in your touch moved function. After that, you can create a SKShapeNode from your path and configure it as you prefer. If you want to draw the line while the finger is moving on the screen you can create the shape node when the touch begins with an empty path and then modify it.

Edit: I wrote some code, it works for me, draws a simple red line.

In your MyScene.m:

@interface MyScene()
{
    CGMutablePathRef pathToDraw;
    SKShapeNode *lineNode;
}
@end

@implementation
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];

    pathToDraw = CGPathCreateMutable();
    CGPathMoveToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);

    lineNode = [SKShapeNode node];
    lineNode.path = pathToDraw;
    lineNode.strokeColor = [SKColor redColor];
    [self addChild:lineNode];
}

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch* touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    CGPathAddLineToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);
    lineNode.path = pathToDraw;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// delete the following line if you want the line to remain on screen.
    [lineNode removeFromParent];
    CGPathRelease(pathToDraw);
}
@end
Masturbation answered 13/11, 2013 at 16:30 Comment(4)
Out of curiosity, what is the framerate performance like if you draw for say 10 seconds ? Does it begin to degrade at all ?Dictum
In my implementation the frame rate remains constant.Masturbation
nothing shows up for me any idea why? node count goes up but nothing visible while drawing i even comment out the last few lines to keep the lineCox
Did you implement the code in your class or you try this as stand alone?Masturbation

© 2022 - 2024 — McMap. All rights reserved.