2016-03-30 7 views
2

私はSprite-kitを使って実験を進めています。私は自分のキャラクターを首尾よく配置し、彼を私の円形の道に従わせました。スプライトキット:円の軌道上にスプライトを配置する方法

私は何を達成しようとしていることは以下の通りです:

  • 赤いボールがメインキャラクターである[完了]
  • 白いポリゴンはコイン

schema

// Adding the big circle 
let runway = SKSpriteNode(imageNamed: "runway") 
runway.position = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame)) 
addChild(runway) 
// Adding the player 
player = SKSpriteNode(imageNamed: "player") 
player.position = CGPointMake(CGRectGetMidX(frame) , (CGRectGetMidY(frame) + runway.size.width/2)) 
// Calculating the initial position of the player and creating a circular path around it 
let dx = player.position.x - frame.width/2 
let dy = player.position.y - frame.height/2 

let radian = atan2(dy, dx) 
let playerPath = UIBezierPath(
    arcCenter: CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame)), 
    radius: (runway.frame.size.width/2) - 20, 
    startAngle: radian, 
    endAngle: radian + CGFloat(M_PI * 4.0), 
    clockwise: true) 

let follow = SKAction.followPath(playerPath.CGPath, asOffset: false, orientToPath: true, speed: 200) 
player.runAction(SKAction.repeatActionForever(follow)) 
です

私の問題は今、同じパスに私の硬貨を置く方法ですか? 同じパスを使用してポジションを生成する方法はありますか、各コインに特定のパスを作成し、毎回パスcurrentPointを抽出する必要がありますか?

私の問題を解決する簡単な方法はありますか?

おかげ

+0

画像へのリンクが壊れています...パスを作成する方法と場所を追加して、より良い回答を得ることができます。しかし、一般的には、あなたの循環路の中心と半径はどこにあるのかを知る必要があります。それに基づいて、周りの点を簡単に計算することができます(あなたが話している実際の経路です)。 – Whirlwind

+0

最初の投稿をいくつかのコードで更新しました。他の人が私の問題を理解するのに役立つことを願っています。私はイメージを見ることができ、それは私のために壊れていない。奇妙な? –

+0

うんうん。私はそれをデバイスやコンピュータから開くことはできません。コメントにリンクを貼り付けてください(好きな場合)...更新されたコードでも十分です。 – Whirlwind

答えて

1

私はあなたが持っている(あなたが(画面の「中央」であるCGPoint(x: frame.midX, y:frame.midY)であるあなたのケースでは)パスの中心がどこにあるかを知る必要があり、あなたが半径を知っている必要があり、言ったようにそれは、すでにあなたがパスを作成していた時に計算)と円周(coinX,coinY)上の点までの中央(frame.midX,frame.midY)から光線が正のx軸となすあなたは角度を必要とする:

import SpriteKit 

class GameScene: SKScene { 

    let player = SKSpriteNode(color: .purpleColor(), size: CGSize(width: 30, height: 30)) 

    override func didMoveToView(view: SKView) { 
     /* Setup your scene here */ 

     // Adding the big circle 
     let runway = SKSpriteNode(color: .orangeColor(), size: CGSize(width: 150, height: 150)) 
     runway.position = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame)) 
     addChild(runway) 
     // Adding the player 

     player.position = CGPointMake(CGRectGetMidX(frame) , (CGRectGetMidY(frame) + runway.size.width/2)) 
     addChild(player) 
     // Calculating the initial position of the player and creating a circular path around it 
     let dx = player.position.x - frame.width/2 
     let dy = player.position.y - frame.height/2 

     let radius = (runway.frame.size.width/2) - 20.0 

     let radian = atan2(dy, dx) 
     let playerPath = UIBezierPath(
      arcCenter: CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame)), 
      radius: radius, 
      startAngle: radian, 
      endAngle: radian + CGFloat(M_PI * 4.0), 
      clockwise: true) 

     let follow = SKAction.followPath(playerPath.CGPath, asOffset: false, orientToPath: true, speed: 200) 
     player.runAction(SKAction.repeatActionForever(follow)) 

     let numberOfCoins = 8 
     for i in 0...numberOfCoins { 

      let coin = SKSpriteNode(color: .yellowColor(), size: CGSize(width: 10, height: 10)) 

      let angle = 2 * M_PI/Double(numberOfCoins) * Double(i) 

      let coinX = radius * cos(CGFloat(angle)) 
      let coinY = radius * sin(CGFloat(angle)) 

      coin.position = CGPoint(x:coinX + frame.midX, y:coinY + frame.midY) 
      addChild(coin) 
     } 
    } 
} 

だけ追記:スプライト-キットで0ラジアンの角度は、正のx軸を指定します。正の角度は反時計回りの方向です。ソース - Building Your Scene

+0

ありがとうございました。本当にありがとうございます。 –

+0

「あなたのシーンを構築する」へのリンクが壊れています。ありがとう! – nbloqs

+1

@nbloqsああ。それを指摘していただきありがとうございますupvote!私はリンクを修正しました。 – Whirlwind

関連する問題