I'd like to make SKSpriteNodes to move along letter outlines. I have many letters but here's one example:
I would like the sprite to follow the red line. I found this answer that mostly covers my problem: Get path to trace out a character in an iOS UIFont
The answer comes with this good and working example code:
let font = UIFont(name: "HelveticaNeue", size: 64)!
var unichars = [UniChar]("P".utf16)
var glyphs = [CGGlyph](count: unichars.count, repeatedValue: 0)
let gotGlyphs = CTFontGetGlyphsForCharacters(font, &unichars, &glyphs, unichars.count)
if gotGlyphs {
let cgpath = CTFontCreatePathForGlyph(font, glyphs[0], nil)!
let path = UIBezierPath(CGPath: cgpath)
print(path)
XCPlaygroundPage.currentPage.captureValue(path, withIdentifier: "glyph \(glyphs[0])")
}
However I still run into a problem as my sprite doesn't complete full path around the letter for all the letters but instead e.g. with "P" stops here (and started from the bottom):
I tried adding some points to the path
like so:
CGPathAddLineToPoint(path, nil, 0, 0)
but the result doesn't work probably because the added point is after the <Close>
statement:
<UIBezierPath: 0x7889ff70; <MoveTo {25.950001, 55.800003}>,
<LineTo {25.950001, 95.100006}>,
<LineTo {53.850002, 95.100006}>,
<QuadCurveTo {71.625, 90.075005} - {66, 95.100006}>,
<QuadCurveTo {77.25, 75.450005} - {77.25, 85.050003}>,
<QuadCurveTo {71.625, 60.750004} - {77.25, 65.850006}>,
<QuadCurveTo {53.850002, 55.800003} - {66, 55.650002}>,
<Close>,
<MoveTo {11.700001, 107.10001}>,
<LineTo {11.700001, 0}>,
<LineTo {25.950001, 0}>,
<LineTo {25.950001, 43.800003}>,
<LineTo {58.650002, 43.800003}>,
<QuadCurveTo {83.175003, 52.050003} - {74.850006, 43.650002}>,
<QuadCurveTo {91.5, 75.450005} - {91.5, 60.450001}>,
<QuadCurveTo {83.175003, 98.775002} - {91.5, 90.450005}>,
<QuadCurveTo {58.650002, 107.10001} - {74.850006, 107.10001}>,
<Close>,
<LineTo {0, 0}>