Dessin d'une ligne pointillée dans Sprite Kit à l'aide de SKShapeNode et de CGPath

Je veux dessiner une ligne pointillée dans mon jeu de kit de sprite, je peux utiliser le noeud SKShapeNode pour tracer une ligne normale comme ceci:

UIBezierPath *_path=[UIBezierPath bezierPath]; //1 CGPoint point1 = CGPointMake(100,100); CGPoint point2 = CGPointMake(150,150); [_path moveToPoint:point1]; [_path addLineToPoint:point2]; //2 SKShapeNode *line = [SKShapeNode node]; line.path = _path.CGPath; 

J'ai essayé de définir un motif en pointillés sur UIBezierPath comme ceci:

 // adding this code at location 1 or 2 above but no effect CGFloat dashes[] = {6, 2}; [_path setLineDash:dashes count:2 phase:0]; 

mais le motif en pointillés n'est pas appliqué.

J'ai également essayé de créer une copy en pointillés de CGPath directement à partir de la propriété UIBezierpath.CGPath comme:

  CGFloat dashes[] = {6, 2}; CGPathRef aCGPath= CGPathCreateCopyByDashingPath(_path.CGPath,NULL,0,dashes,2); line.path = aCGPath; 

mais aussi la même chose.

J'apprécie vraiment si quelqu'un pourrait expliquer quel est le problème et comment puis-je dessiner une ligne pointillée entre deux points en appliquant cgpath en pointillé à skshapenode.

Edit: Je sais pour cet exemple simple je pourrais split la distance entre ces deux points à de petites distances fixes et déplacer et dessiner la ligne pointillée par bezeirpath mais considérer un path de main libre avec des points venus des touches, c'est très complexe et inefficace à redessiner le path avec des points de longueur fixe puis dessinez des tirets. Je me request s'il existe un moyen d'appliquer le motif en pointillé au path et de faire en sorte que skshapenode l'utilise, c'est ma question.

Si quelqu'un est toujours intéressé par une réponse simple à cette question:

Utilisez CGPathCreateCopyByDashingPath pour créer une copy en pointillés de - [UIBezierCurve CGPath]

 CGPathRef CGPathCreateCopyByDashingPath( CGPathRef path, const CGAffineTransform *transform, CGFloat phase, const CGFloat *lengths, size_t count ); 

et ajoutez-le à la propriété de path SKShapeNode .

Exemple:

  // creates a dashed pattern CGFloat pattern[2]; pattern[0] = 10.0; pattern[1] = 10.0; CGPathRef dashed = CGPathCreateCopyByDashingPath([bezierPath CGPath], NULL, 0, pattern, 2); self.myShapeNode.path = dashed; CGPathRelease(dashed); 

EDIT: Pour les performances, vous pouvez append un SKShapeNode à un SKEffectNode et définir la propriété shouldRasterize sur YES .

Changez le code à l'location 1 pour quelque chose comme ceci:

 UIBezierPath *_path=[UIBezierPath bezierPath]; CGPoint point1 = CGPointMake(100,100); CGPoint point2 = CGPointMake(150,150); CGFloat deltaX = 1; CGFloat deltaY = 1; CGPoint tmpPoint = point1; [_path moveToPoint:point1]; while(tmpPoint.x<point2.x && tmpPoint.y<point2.y){ tmpPoint.x+=deltaX; tmpPoint.y+=deltaY; if((tmpPoint.y-point1.y)%2==1){ [_path addLineToPoint:tmpPoint]; }else{ [_path moveToPoint:tmpPoint]; } } // If the line is not a 45 degree straight line // Please modify the while loop accordingly [_path addLineToPoint:point2];