作成した図形8を描画すると、パスを描いた方法であるため、スプライトが下の円に沿っていることがわかります。
上部または下部の円の半分を上/下から中央に描画し、次に開始点と終了点を中央に追加し、最後にもう半分の円を開始点に追加する必要があります。ここで
も従うことを境界矩形と8の字のパスを描く、例です。
class GameScene: SKScene {
// var sprite = SKSpriteNode()
var figureEightPath = CGMutablePath()
override func didMove(to view: SKView) {
let boundingRect = CGRect(x: -200, y: -400, width: 400, height: 800)
addChild(drawSKShapeNode(fromRect: rect, withWidth: 2, inColour: SKColor.green, called: "rect"))
figureEightPath = drawFigureEight(in: boundingRect)
addChild(drawSKShapeNode(fromPath: figureEightPath, withWidth: 2, inColour: SKColor.red, called: "path"))
let sprite = SKSpriteNode(color: SKColor.yellow, size: CGSize(width: 50, height: 50))
addChild(sprite)
let moveAction = SKAction.follow(figureEightPath, asOffset: false, orientToPath: true, speed: 200)
sprite.run(SKAction.repeatForever(moveAction))
}
func drawSKShapeNode(fromPath path: CGMutablePath,
withWidth width: CGFloat,
inColour colour: SKColor,
called name: String) -> SKShapeNode {
let shape = SKShapeNode(path: path, centered: true)
shape.lineWidth = width
shape.strokeColor = colour
shape.name = name
return shape
}
func drawSKShapeNode(fromRect rect: CGRect,
withWidth width: CGFloat,
inColour colour: SKColor,
called name: String) -> SKShapeNode {
let shape = SKShapeNode(rect: rect)
shape.lineWidth = width
shape.strokeColor = colour
shape.name = name
return shape
}
func drawFigureEight(in rect: CGRect) -> CGMutablePath {
let bottomCentre = CGPoint(x: rect.midX, y: rect.height/4+rect.minY)
let topCentre = CGPoint(x: rect.midX, y:(rect.height/4)*3+rect.minY)
let radius = rect.height/4
let bottom = CGPoint(x: rect.midX, y: rect.minY)
let path = CGMutablePath()
path.move(to: bottom)
//Draw a semi-circle from the bottom counter-clockwise to the middle
path.addArc(center: bottomCentre, radius: radius, startAngle: CGFloat(Double.pi*1.5), endAngle: CGFloat(Double.pi/2), clockwise: false)
//Draw to top circle in a clockwise direction
path.addArc(center: topCentre, radius: radius, startAngle: CGFloat(Double.pi*1.5), endAngle: CGFloat(-Double.pi/2), clockwise: true)
//Draw the rest of the bottom circle by drawing a semi-circle from the middle to the bottom counter-clockwise.
path.addArc(center: bottomCentre, radius: radius, startAngle: CGFloat(Double.pi/2), endAngle: CGFloat(-Double.pi/2), clockwise: false)
return path
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}
ヘルパー関数のカップルあります - drawSKShapeNode - あなたが必要とするがために便利ですありませんそれらが与えられたものからSKShapeNodeを作成することによってCGPathまたはCGRectを '描画'する際のデバッグ
それは今、多くの意味があります。私はちょうどあなたの提案と私のコードを置き換えて、完全に動作します。スティーブの詳細な答えをありがとう! –
スプライトはパスが描かれた順にパスをたどります。あなたの元の例では、サークルの一番下に来たときに、それが上のサークルに到達する経路がなかったため、停止しました。 –