1
私はSpriteKitでRPG Birds-eyeスタイルのゲームを作っています。ジョイスティックを作ったのは、D-Padがプレイヤーにキャラクターを十分にコントロールできないからです。SpriteKit:これに基づいてジョイスティックの角度とスプライトを変更します
ジョイスティックの親指ノードの角度に基づいてキャラクターのスプライトを変更するために必要なデータを計算する方法を私の頭脳で包むことはできません。私は今設定している方法は、クロスでジョイスティックの位置の正または負の状態をテストUpdateメソッド
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
if xJoystickDelta > 0 && yJoystickDelta < 0 {
print("forward")
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if isTracking == false && base.contains(location) {
isTracking = true
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location: CGPoint = touch.location(in: self)
if isTracking == true {
let v = CGVector(dx: location.x - base.position.x, dy: location.y - DPad.position.y)
let angle = atan2(v.dy, v.dx)
let deg = angle * CGFloat(180/Double.pi)
let Length:CGFloat = base.frame.size.height/2
let xDist: CGFloat = sin(angle - 1.57079633) * Length
let yDist: CGFloat = cos(angle - 1.57079633) * Length
print(xDist,yDist)
xJoystickDelta = location.x * base.position.x/CGFloat(Double.pi)
yJoystickDelta = location.y * base.position.y/CGFloat(Double.pi)
if base.contains(location) {
thumbNode.position = location
} else {
thumbNode.position = CGPoint(x: base.position.x - xDist, y: base.position.y + yDist)
}
}
}
}
を使用しています。ここ
が私のコードです親ノードが下の4つのマークされたセクションの内側にあるところに基づく方法。
私はそれはそれは親指のノードが実際にそうように私のジョイスティックベースの内側に向いている場所に基づいてスプライトを変更するよう
が、私はそれを設定するにはどうすればよいことをやりたいいけません。私は今3日間これで苦労してきた
ので、任意の助けいただければ幸いです。
ありがとうございます!あなたは救命救助者です。私は今これを数日間やろうとしています。 P.S.ダウンとアップを切り替える必要があります –
@ E.Huckabee:座標系によって異なります。 UIKitでは、原点が左上隅にあります。そのため、正のΔyを「下」とみなしています。 SpriteKitでは逆もあります。 –
私はそれがとても簡単だったことを理解していなかったあなたの迅速なanwser –