在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