2017-05-29 8 views
0

8または無限記号の形をしたCGPathを作成しようとしています。私は、パスの作成が望ましい結果を返すべきだと思いますが、私はまだそれをここに掲載します:SKAction followPathが2つの円で正しく動作しない(八角形)

let path = CGMutablePath() 

path.addEllipse(in: CGRect(x: -height/4, y: -height/2, width: height/2, height: height/2)) 
path.addEllipse(in: CGRect(x: -height/4, y: 0.0, width: height/2, height: height/2)) 

path.closeSubpath() 

return path 

は、今私は、私はちょうどので、同じようSKActionを使用して動作するだろうと思ったこの道を歩むべきで別のノードを持っています:

highlighter.run(SKAction.repeatForever(SKAction.follow(shape.path!, asOffset: false, orientToPath: true, duration: 5.0))) 

ハイライトは私のノードの名前であり、シェイプは私のパスを割り当てたSKShapeNodeの名前です。しかしながら、ノードは、円のうちの1つを描き、所望の形状を描画しない。私はこの問題をグーグルで見つけましたが、類似のケースを見つけることができず、1日以上これに座っていたので、何か明白ではないかもしれません。どんな洞察も大いに評価されるだろう!

答えて

1

作成した図形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を '描画'する際のデバッグ

+0

それは今、多くの意味があります。私はちょうどあなたの提案と私のコードを置き換えて、完全に動作します。スティーブの詳細な答えをありがとう! –

+0

スプライトはパスが描かれた順にパスをたどります。あなたの元の例では、サークルの一番下に来たときに、それが上のサークルに到達する経路がなかったため、停止しました。 –

関連する問題