So I have a SKSpriteNode, and its supposed to be dragged from the middle of the screen, let go, then smoothly flow to the nearest corner (or rather, 10 points away on each axis from the nearest corner).
I use a method that should return a CGPathRef
with an origin at the node's location, that starts off at the angle provided, (the nodes angle off center), then curves toward the end corner, ending at the end corner (all relative to the node's location...), for a smooth transition between the user's drag and the program's drag completion. The method is as follows:
-(CGPathRef)createPathToEndpoint:(int)end from:(CGPoint)start atAngle:(float)Angle{
UIBezierPath *path = [[UIBezierPath alloc]init];
[path moveToPoint:CGPointZero];
//Code to set up endPoint
CGPoint translatedEndPoint = CGPointMake(endPoint.x-start.x, endPoint.y-start.y);
CGPoint cP1;
/*
Code here to set cP1 for desired path attributes.
Not included for simplicity's sake, because what really seems to be the issue is
translatedEndPoint not seeming correct.
*/
[path addQuadCurveToPoint:translatedEndPoint controlPoint:cP1];
CGPathRef newPath = [path CGPath];
return newPath;
}
The SKSpriteNode
is made to follow that path through the following code:
CGPathRef rollPath = [self createPathToEndpoint:section
from:[touch locationInNode:self]
atAngle:angle];
NSTimeInterval timeMove = MIN((timePassed/[self distanceBetween:mainBall.position and:middle])*100,1);
SKAction *move = [SKAction followPath:rollPath
asOffset:YES
orientToPath:NO
duration:timeMove];
But when the code runs, the sprite will generally end up some distance away from the end point, anywhere from a couple to probably upwards of 30 points away, even. What gives?
As I was typing up this question, at the very end, I saw my problem. I've fixed it in the program, but I figure might as well share with the community on the off chance some one else has the same issue. (and because I don't want this typing to go to waste)
Here goes: even though the node is told to start following the curve after your finger leaves, in reference to where your finger left, (at position [touch locationInNode: self]
), the node's location might be slightly different from the touch's location, even though the -(void)touchesMoved
method tries to keep the node at your fingertip.
So the simple fix is to change this line:
CGPathRef rollPath = [self createPathToEndpoint:section
from:[touch locationInNode:self]
atAngle:angle];
to this:
CGPathRef rollPath = [self createPathToEndpoint:section
from:mainBall.location
atAngle:angle];
Again, simple rookie mistake looking back on it, but hey, I can't be the only rookie here, hope this helps someone else!