SpriteNode 不遵循 cgpath

Posted

技术标签:

【中文标题】SpriteNode 不遵循 cgpath【英文标题】:SpriteNode not following cgpath 【发布时间】:2013-10-06 23:38:21 【问题描述】:

我对新游戏精灵有疑问... 我的“汽车”在第一圈就完美地跟随了赛道,但从那时起,它似乎在每圈结束时都会发生偏移。谁能告诉我我做错了什么?

- (void)createSceneContents
    self.backgroundColor = [SKColor blackColor];
    self.scaleMode = SKSceneScaleModeAspectFit;

    // CREATING THE CAR AND THE TRACK
    SKSpriteNode *myCar = [self newcar];
    myCar.position = [[self trackShape] position];;
    [self addChild:myCar];

    SKShapeNode *track = [self trackShape];
    track.position = CGPointMake(48, 40);
    [self addChild:track];




- (SKSpriteNode *)newcar
    // GENERATES A RECTANGLE FOR THE SPRITE NODE
    SKSpriteNode *hull = [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size:CGSizeMake(20,50)];
    CGPathRef pathRef = [[self bPath] CGPath];
    // CAR SHOULD FOLLOW THE TRACK
    // OK ONLY ON THE FIRST LAP
    SKAction *hover = [SKAction sequence:@[[SKAction followPath:pathRef duration:20.0],
                                    ]];

    hull.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:hull.size];
    hull.physicsBody.dynamic = NO;
    [hull runAction: [SKAction repeatActionForever:hover]];
    return hull;



-(SKShapeNode *)trackShape
    SKShapeNode *newshape = [[SKShapeNode alloc] init];
    newshape.position = CGPointMake(48, 40);
    newshape.path = [[self bPath] CGPath];
    newshape.fillColor = [SKColor clearColor];
    newshape.strokeColor = [SKColor blueColor];
    newshape.lineWidth = 8;
    return newshape;

这是路径……都在同一个类中。

-(UIBezierPath *)bPath
    UIBezierPath* bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint: CGPointMake(48, 40)];
    [bezierPath addLineToPoint: CGPointMake(10.5, 147.5)];
    [bezierPath addCurveToPoint: CGPointMake(131.5, 209.5) controlPoint1: CGPointMake(10.5, 147.5) controlPoint2: CGPointMake(45.01, 253.16)];
    [bezierPath addCurveToPoint: CGPointMake(267.5, 209.5) controlPoint1: CGPointMake(217.99, 165.84) controlPoint2: CGPointMake(259.57, 194.46)];
    [bezierPath addCurveToPoint: CGPointMake(283.5, 302.5) controlPoint1: CGPointMake(275.43, 224.54) controlPoint2: CGPointMake(325.48, 263.29)];
    [bezierPath addCurveToPoint: CGPointMake(105.5, 327.5) controlPoint1: CGPointMake(241.52, 341.71) controlPoint2: CGPointMake(128.94, 352.5)];
    [bezierPath addCurveToPoint: CGPointMake(10.5, 396.5) controlPoint1: CGPointMake(82.06, 302.5) controlPoint2: CGPointMake(-27.5, 333.95)];
    [bezierPath addCurveToPoint: CGPointMake(239.5, 448.5) controlPoint1: CGPointMake(48.5, 459.05) controlPoint2: CGPointMake(195.5, 463.67)];
    [bezierPath addCurveToPoint: CGPointMake(283.5, 40.5) controlPoint1: CGPointMake(399.5, 40.5) controlPoint2: CGPointMake(375.39, 205.99)];
    [bezierPath addCurveToPoint: CGPointMake(153.5, 78.5) controlPoint1: CGPointMake(191.61, -124.99) controlPoint2: CGPointMake(153.5, 78.5)];
    [bezierPath addLineToPoint: CGPointMake(48, 40)];
    [bezierPath closePath];
    return bezierPath;

【问题讨论】:

【参考方案1】:

过了一会儿,这是我的results

我废弃了你的 car 方法,只是把它都放在了这个里面。

我不得不调整你的轨道的位置,但它似乎工作。

- (void)createSceneContents
    self.backgroundColor = [SKColor blackColor];
    self.scaleMode = SKSceneScaleModeAspectFit;

    SKSpriteNode *hull = [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size:CGSizeMake(20,50)];
    CGPathRef pathRef = [[self bPath] CGPath];

    SKShapeNode *track = [self trackShape];
    //track.position = // scrap me.. is set in track shape method
    [self addChild:track];

    hull.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:hull.size];
    hull.physicsBody.dynamic = NO;
    hull.position = CGPointMake(48, 40);
    [self addChild:hull];

    SKAction *hover = [SKAction followPath:pathRef asOffset:NO orientToPath:YES duration:5.0];
    [hull runAction: [SKAction repeatActionForever:hover]];

在您的 trackShape 方法中,将位置设置为 0,0

newshape.position = CGPointMake(0, 0);

【讨论】:

顺便说一句,这个应用程序真的很酷paintcodeapp.com 让绘制路径变得超级容易......希望苹果在 Xcode 中添加这样的东西。 这正是我用来创建该路径的内容。大声笑,谢谢,您的回答很棒,而且效果很好

以上是关于SpriteNode 不遵循 cgpath的主要内容,如果未能解决你的问题,请参考以下文章

将大小(约束)添加到 SpriteNode

通过 Spritenode

如何用另一个替换 SpriteNode

在 SpriteNode 上重复操作

如何通过触摸旋转 SpriteNode?

触摸 Sprite,使其跳起来然后再次落下(重复多次点击 spritenode。)