当设备笔直且不倾斜时,如何让我的加速度计不移动我的节点?
How would I get my accelerometer to not move my node when the device is straight and not tilting?
我有一个游戏,当我向左和向右倾斜时移动我的节点。但是当我保持设备笔直时,我希望它停止节点并添加我的节点的图像,这样就不会有灰色和白色的棋盘。我该怎么做?正如您在我的代码中看到的那样,我使用 SKTexture 更改节点图像和移动节点的速度。当设备笔直且不倾斜且节点不移动时,我将如何更改图像?谢谢!
func addTilt() {
if (motionManager.accelerometerAvailable) {
motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue()) {
(data, error) in
if(data.acceleration.x < -0.05) { // tilting the device to the right
self.heroNode.accelerometerActive == true
self.heroNode.physicsBody?.velocity = CGVector(dx: -250, dy: 0)
self.heroNode.texture = SKTexture(imageNamed: "heroNode1")
} else if (data.acceleration.x > 0.05) { // tilting the device to the left
self.heroNode.accelerometerActive == true
self.heroNode.physicsBody?.velocity = CGVector(dx: 250, dy: 0)
self.heroNode.texture = SKTexture(imageNamed: "heroNode2")
}
}
}
}
看起来您可以在当前逻辑中添加一个 else
来处理 X 加速度大于 -0.05 且小于 0.05 时的情况,这主要是直立的,在任一方向上只有一点点倾斜。
此外,您应该使用 =
而不是 ==
进行赋值,它测试相等性并且对您的 accelerometerActive
属性.[= 的值没有影响15=]
而且,如果是我,我会通过放弃额外的括号来使代码更简洁,因为在 Swift 中您不需要它们。
if motionManager.accelerometerAvailable { // No parenthesis
motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue()) { (data, error) in
if data.acceleration.x < -0.05 { // tilting the device to the right
self.heroNode.accelerometerActive = true
self.heroNode.physicsBody?.velocity = CGVector(dx: -250, dy: 0)
self.heroNode.texture = SKTexture(imageNamed: "heroNode1")
} else if data.acceleration.x > 0.05 { // tilting the device to the left
self.heroNode.accelerometerActive = true
self.heroNode.physicsBody?.velocity = CGVector(dx: 250, dy: 0)
self.heroNode.texture = SKTexture(imageNamed: "heroNode2")
} else { // straight
self.heroNode.accelerometerActive = false
self.heroNode.physicsBody?.velocity = CGVector(dx: 0, dy: 0) // No velocity
self.heroNode.texture = SKTexture(imageNamed: "heroNode3") // Image when straight
}
}
}
我有一个游戏,当我向左和向右倾斜时移动我的节点。但是当我保持设备笔直时,我希望它停止节点并添加我的节点的图像,这样就不会有灰色和白色的棋盘。我该怎么做?正如您在我的代码中看到的那样,我使用 SKTexture 更改节点图像和移动节点的速度。当设备笔直且不倾斜且节点不移动时,我将如何更改图像?谢谢!
func addTilt() {
if (motionManager.accelerometerAvailable) {
motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue()) {
(data, error) in
if(data.acceleration.x < -0.05) { // tilting the device to the right
self.heroNode.accelerometerActive == true
self.heroNode.physicsBody?.velocity = CGVector(dx: -250, dy: 0)
self.heroNode.texture = SKTexture(imageNamed: "heroNode1")
} else if (data.acceleration.x > 0.05) { // tilting the device to the left
self.heroNode.accelerometerActive == true
self.heroNode.physicsBody?.velocity = CGVector(dx: 250, dy: 0)
self.heroNode.texture = SKTexture(imageNamed: "heroNode2")
}
}
}
}
看起来您可以在当前逻辑中添加一个 else
来处理 X 加速度大于 -0.05 且小于 0.05 时的情况,这主要是直立的,在任一方向上只有一点点倾斜。
此外,您应该使用 =
而不是 ==
进行赋值,它测试相等性并且对您的 accelerometerActive
属性.[= 的值没有影响15=]
而且,如果是我,我会通过放弃额外的括号来使代码更简洁,因为在 Swift 中您不需要它们。
if motionManager.accelerometerAvailable { // No parenthesis
motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue()) { (data, error) in
if data.acceleration.x < -0.05 { // tilting the device to the right
self.heroNode.accelerometerActive = true
self.heroNode.physicsBody?.velocity = CGVector(dx: -250, dy: 0)
self.heroNode.texture = SKTexture(imageNamed: "heroNode1")
} else if data.acceleration.x > 0.05 { // tilting the device to the left
self.heroNode.accelerometerActive = true
self.heroNode.physicsBody?.velocity = CGVector(dx: 250, dy: 0)
self.heroNode.texture = SKTexture(imageNamed: "heroNode2")
} else { // straight
self.heroNode.accelerometerActive = false
self.heroNode.physicsBody?.velocity = CGVector(dx: 0, dy: 0) // No velocity
self.heroNode.texture = SKTexture(imageNamed: "heroNode3") // Image when straight
}
}
}