在SpriteKit中,我需要沿一個軸(例如穿過精靈中心的那個)旋轉一個精靈,就像用戶旋轉的一個輪子一樣。沿着一個軸旋轉一個精靈
我試着用applyTorque
函數(施加一個只有角度而非線性的力),但我無法處理由屏幕上不同運動引起的不同力(屏幕上的觸摸時間越長,應用)。
有人可以幫我理解如何處理這個問題嗎?
在SpriteKit中,我需要沿一個軸(例如穿過精靈中心的那個)旋轉一個精靈,就像用戶旋轉的一個輪子一樣。沿着一個軸旋轉一個精靈
我試着用applyTorque
函數(施加一個只有角度而非線性的力),但我無法處理由屏幕上不同運動引起的不同力(屏幕上的觸摸時間越長,應用)。
有人可以幫我理解如何處理這個問題嗎?
下面是根據你向左/向右滑動的速度旋轉球的答案:
class GameScene: SKScene {
let speedLabel = SKLabelNode(text: "Speed: 0")
let wheel = SKShapeNode(circleOfRadius: 25)
var recognizer: UIPanGestureRecognizer!
func pan(recognizer: UIPanGestureRecognizer) {
let velocity = recognizer.velocity(in: view!).x
// Play with this value until if feels right to you.
let adjustor = CGFloat(60)
let speed = velocity/adjustor
wheel.physicsBody!.angularVelocity = -speed
}
// Scene setup:
override func didMove(to view: SKView) {
removeAllChildren()
physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
wheel.fillColor = .blue
wheel.physicsBody = SKPhysicsBody(circleOfRadius: 25)
wheel.physicsBody!.affectedByGravity = false
let wheelDot = SKSpriteNode(color: .gray, size: CGSize(width: 5, height:5))
wheel.addChild(wheelDot)
wheelDot.position.y += 20
wheel.setScale(3)
speedLabel.setScale(3)
speedLabel.position.y = (frame.maxY - speedLabel.frame.size.height/2) - 45
recognizer = UIPanGestureRecognizer(target: self, action: #selector(pan))
view.addGestureRecognizer(recognizer)
addChild(wheel)
addChild(speedLabel)
}
override func didSimulatePhysics() {
speedLabel.text = "Speed: \(abs(Int(wheel.physicsBody!.angularVelocity)))"
}
}
太棒了!這正是我需要的! – ThugMazzola
這裏是一個基本的順時針或逆時針旋轉輪子的例子,具體取決於您按下屏幕的左側還是右側。堅持提高速度:
class GameScene : SKScene {
enum Direction { case left, right }
var directionToMove: Direction?
let wheel = SKShapeNode(circleOfRadius: 25)
let speedLabel = SKLabelNode(text: "Speed: 0")
override func didMove(to view: SKView) {
// Scene setup:
anchorPoint = CGPoint(x: 0.5, y: 0.5)
removeAllChildren()
physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
wheel.fillColor = .blue
wheel.physicsBody = SKPhysicsBody(circleOfRadius: 25)
wheel.physicsBody!.affectedByGravity = false
let wheelDot = SKSpriteNode(color: .gray, size: CGSize(width: 5, height:5))
wheel.addChild(wheelDot)
wheelDot.position.y += 20
wheel.setScale(3)
speedLabel.setScale(3)
speedLabel.position.y = (frame.maxY - speedLabel.frame.size.height/2) - 45
addChild(wheel)
addChild(speedLabel)
}
// Change this to touchesBegan for iOS:
override func mouseDown(with event: NSEvent) {
// Change this to touches.first!.location(in: self) for iOS.
let location = event.location(in: self)
// Determine if touch left or right side:
if location.x > 0 {
directionToMove = .right
}
else if location.x < 0 {
directionToMove = .left
}
}
override func mouseUp(with event: NSEvent) {
// Stop applying gas:
directionToMove = nil
print("lol")
}
override func update(_ currentTime: TimeInterval) {
// This is how much speed we gain each frame:
let torque = CGFloat(0.01)
guard let direction = directionToMove else { return }
// Apply torque in the proper direction
switch direction {
case .left:
wheel.physicsBody!.applyTorque(torque)
case .right:
wheel.physicsBody!.applyTorque(-torque)
}
}
override func didSimulatePhysics() {
// Speedometer:
speedLabel.text = "Speed: \(abs(Int(wheel.physicsBody!.angularVelocity)))"
}
}
這是一個很好的起點!此外,如果在這種情況下,由於在iOS中畫布從(0,0)開始,所以左側的方向永遠不會被調用。但是,我需要一種方法來處理應用於輪子的不同力量 – ThugMazzola
將您的定位點設置爲0.5 0.5抱歉,我有一個sks文件@ThugMazzola – Fluidity
你所說的「處理由不同的運動不同勢力的意思屏幕「? – Fluidity
對不起,我的意思是:從輪子開始,如果按下並滑動它必須旋轉;如果滑動時間長意味着用戶想要旋轉很好,而如果滑動時間較短,用戶希望稍微旋轉一下。我需要一種方法來區分這兩種情況並基於此來應用扭矩。 – ThugMazzola
我知道如何做到這一點。你只需要一個'UIPanGestureRecognizer'(或者實現你自己的東西,類似於touchesMoved和friends),然後檢查識別器對象的'.velocity'並根據它來施加一個扭矩。我可以更新我的答案,如果你想,但也許不是今天。 – Fluidity