2013-10-06 2 views
4

J'ai un problème avec le nouveau sprite du jeu ... Ma "voiture" suit parfaitement la piste au premier tour mais à partir de ce moment, il semble qu'elle soit décalée à la fin de chaque tour. Quelqu'un pourrait-il me dire ce que je fais mal?SpriteNode ne suit pas cgpath

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

Ceci est le chemin ... tout est dans la même classe.

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

cela m'a déconcerté! – DogCoffee

Répondre

5

Après un certain temps, ceci est mon results

enter image description here

Je mis au rebut votre méthode de voiture, et juste placé tout dans ce.

J'ai dû modifier la position de votre piste, mais cela semble fonctionner.

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

Dans votre méthode trackShape, définissez la position à 0,0

newshape.position = CGPointMake(0, 0); 
+2

En note, cette application est vraiment cool http://www.paintcodeapp.com rend les chemins de dessin super facile ... espérons Apple ajoute quelque chose comme ça à Xcode. – DogCoffee

+0

c'est exactement ce que j'ai utilisé pour créer ce chemin. lol Merci, votre réponse est excellente et ça marche parfaitement – Farini