Support our Sponsors:

Go Back   Touch Arcade > Developer Discussion > Public Game Developers Forum

Reply
 
Thread Tools Display Modes
  #1  
Old 07-19-2014, 03:24 PM
bob_ bob_ is offline
Junior Member
iPod Touch (5th Gen), iOS 7.x
 
Join Date: Jul 2014
Posts: 3
Default 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,
Reply With Quote
  #2  
Old 07-20-2014, 02:36 PM
M.O. M.O. is offline
Member
iPhone 5, iOS 8.x
 
Join Date: Apr 2012
Posts: 63
Default

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.
Reply With Quote
  #3  
Old 07-20-2014, 05:25 PM
bob_ bob_ is offline
Junior Member
iPod Touch (5th Gen), iOS 7.x
 
Join Date: Jul 2014
Posts: 3
Default

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];
}
Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Copyright 2012, TouchArcade.com, LLC.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Copyright 2008 - 2011, TouchArcade.com. Privacy Policy / DMCA Copyright Agent