★ TouchArcade needs your help. Click here to support us on Patreon.

Restricting the size of a line made by touch-SpriteKit (objective-C)

07-19-2014, 02:24 PM
#1
Junior Member [Original Poster]
Joined: Jul 2014
Posts: 3
Restricting the size of a line made by touch-SpriteKit (objective-C)

So i've created a code that creates a line of unlimited size by touch and i'm wondering how to restrict the size such that the beginning and end of the line can be a small distance to a maximum set distance apart?

The code i used was:

pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);
lineNode = [SKShapeNode node];
lineNode.path = pathToDraw;
lineNode.zPosition = 1000;
lineNode.strokeColor = [SKColor blueColor];
lineNode.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromPathathToDraw];
lineNode.physicsBody.categoryBitMask = ballCategory;
lineNode.physicsBody.contactTestBitMask = ballCategory;
[self addChild:lineNode];
I used this in the touch began and touch Moved method,
07-20-2014, 01:36 PM
#2
Joined: Apr 2012
Posts: 65
Use the initial point of touch and the current point of touch to model a right triangle, than check the length of the hypotenuse. If its longer than the line you want, make it smaller, else let it expand.
07-20-2014, 04:25 PM
#3
Junior Member [Original Poster]
Joined: Jul 2014
Posts: 3
I had already tried similar where i used the code below within the touch moved method, in my code i had already set "previous" as the variable for location at the touch began method (made it a property). I'm wondering if there's a mistake in my code or line of thinking, and further consider that within the touch began method, the code initially shown in my question only occurs if lineNode doesn't exist since i placed it within if(!(lineNode){}. Thank you.

int xprevious=previous.x;
int yprevious=previous.y;
int xlocation=location.x;
int ylocation=location.y;


int distance=((xlocation-xprevious)^2 +(ylocation-yprevious)^2)^(1/2);



if(distance<80){

CGPathAddLineToPoint(pathToDraw, NULL, location.x, location.y);
lineNode.path = pathToDraw;
lineNode.zPosition = 1000;
lineNode.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromPathathToDraw];
lineNode.physicsBody.categoryBitMask = boundaryCategory;
lineNode.physicsBody.contactTestBitMask = ballCategory;
lineNode.name = @"boundary";
lineNode.physicsBody.restitution=1;

}

// }
else{
lineNode=NO;
[lineNode removeFromParent];
}