ios - 遵循 CGPathRef 的不准确之处

标签 ios sprite-kit skaction

所以我有一个 SKSpriteNode,它应该从屏幕中间拖动,放开,然后平滑地流到最近的角(或者更确切地说,从最近的角在每个轴上 10 个点)。

我使用的方法应该返回一个 CGPathRef,原点在节点位置,从提供的角度开始,(节点偏离中心的角度),然后向结束角弯曲,在结束角结束(全部相对于节点的位置......),用于用户拖动和程序拖动完成之间的平滑过渡。方法如下:

-(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;
}

通过以下代码使 SKSpriteNode 遵循该路径:

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];

但是当代码运行时, Sprite 通常会在距离终点有一段距离的地方结束,从几点到可能超过 30 点,甚至。是什么赋予了?

最佳答案

当我输入这个问题时,在最后,我看到了我的问题。我已经在程序中修复了它,但我认为如果其他人遇到同样的问题,不妨与社区分享。 (而且因为我不想浪费这个打字)

这里是这样的:即使节点被告知在您的手指离开后开始沿着曲线移动,引用您手指离开的位置(在位置 [touch locationInNode: self]),节点的location 可能与触摸的位置略有不同,即使 -(void)touchesMoved 方法试图让节点触手可及。

所以简单的解决方法是更改​​这一行:

CGPathRef rollPath = [self createPathToEndpoint:section 
                                           from:[touch locationInNode:self] 
                                        atAngle:angle];

为此:

CGPathRef rollPath = [self createPathToEndpoint:section 
                                           from:mainBall.location 
                                        atAngle:angle];

再次回顾,简单的菜鸟错误,但是嘿,我不能是这里唯一的菜鸟,希望这对其他人有帮助!

关于ios - 遵循 CGPathRef 的不准确之处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25049833/

相关文章:

ios - 停止 SKAction

ios - iOS Level Editor未连接到我的Skscene

ios - 来自 SKEmitterNode 的动画粒子

ios - 游戏持续时间越长,我怎样才能让我的炸弹落得更快或产生得更快?

ios - 将屏幕截图上传到新的 iTunes Connect 时出错

ios - iOS-如何使用现有动态库(.so文件)

ios - 在设备上运行时应用程序功能发生变化

ios - 使用 UIButton 实现 UIDatePicker 和 UIPickerView

swift - iCloud keyValue 使我的游戏崩溃

ios - SKAction 未删除 Sprite