CMMotionManager倾斜以向左移动节点&;右Swift 4
我有一个单独的SKNode类,名为Balloon,如下所示CMMotionManager倾斜以向左移动节点&;右Swift 4,swift,sprite-kit,cmmotionmanager,Swift,Sprite Kit,Cmmotionmanager,我有一个单独的SKNode类,名为Balloon,如下所示 import Foundation import SpriteKit class Balloon: SKNode { init(image: SKSpriteNode) { super.init() let atlas = SKTextureAtlas(named: "balloons") var textureArray = [SKTexture]() se
import Foundation
import SpriteKit
class Balloon: SKNode {
init(image: SKSpriteNode) {
super.init()
let atlas = SKTextureAtlas(named: "balloons")
var textureArray = [SKTexture]()
self.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "red_balloon1"), size: CGSize(width: image.size.width, height: image.size.height))
self.physicsBody?.affectedByGravity = false
self.physicsBody?.categoryBitMask = balloonCategory
self.physicsBody?.contactTestBitMask = floorCategory
self.physicsBody?.collisionBitMask = floorCategory
self.position = CGPoint(x: 0, y: -UIScreen.main.bounds.height / 2)
self.zPosition = 1
for i in 1...atlas.textureNames.count {
let Name = "red_balloon\(i).png"
textureArray.append(SKTexture(imageNamed: Name))
}
image.run(SKAction.repeatForever(SKAction.animate(with: textureArray, timePerFrame: 0.1, resize: false, restore: true)))
self.addChild(image)
}
// Enables the ability to drag the ballon along the x axis
func move(touchLocation: CGPoint) {
if self.calculateAccumulatedFrame().contains(touchLocation) {
self.position.y = touchLocation.y
self.position.x = touchLocation.x
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
然后我在我的游戏场景中使用这个类,如下所示
let balloon = Balloon(image: SKSpriteNode(imageNamed: "red_balloon1"))
override func didMove(to view: SKView) {
self.addChild(balloon)
}
override func update(_ currentTime: TimeInterval) {
if let accelerometerData = motionManager.accelerometerData {
balloon.physicsBody?.velocity = CGVector(dx: accelerometerData.acceleration.y * 50, dy: accelerometerData.acceleration.x * 50)
}
}
基本上,我想做的是在纵向模式下使用应用程序,向左或向右移动节点,无论我倾斜iPhone的方向如何。下面是我在Objective C时代使用的东西,但我不确定这些天Swift 4的效果如何,或者是否有更好的东西。希望我提供了足够的信息,为一些提示和任何有帮助的
#define BALLOON 25
-(void)startAccelerometerData {
motionManager = [[CMMotionManager alloc] init];
motionManager.accelerometerUpdateInterval = 1/60.0;
[motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
valueX = accelerometerData.acceleration.x*26.0;
newX = (balloon.center.x +valueX);
if (newX > 320-BALLOON)
newX = 320-BALLOON;
else if (newX < 0+BALLOON)
newX = 0+BALLOON;
balloon.center = CGPointMake(newX, balloon.center.y);
} ];
}
#定义引出序号25
-(无效)StartAccelerMeterData{
motionManager=[[CMMotionManager alloc]init];
motionManager.AccelerometrUpdateInterval=1/60.0;
[motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]带处理程序:^(CMAccelerometerData*accelerometerData,NSError*错误){
valueX=加速度计数据加速度x*26.0;
newX=(balloon.center.x+valueX);
如果(newX>320-BALLOON)
newX=320-1;
else if(newX<0+引出序号)
newX=0+气球;
balloon.center=CGPointMake(newX,balloon.center.y);
} ];
}
没关系,我明白了
if let accelerometerData = motionManager.accelerometerData {
if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft {
balloon.physicsBody?.velocity = CGVector(dx: accelerometerData.acceleration.x * -500.0, dy: 0)
} else {
balloon.physicsBody?.velocity = CGVector(dx: accelerometerData.acceleration.x * 500.0, dy: 0)
}
}
您能否确认您正在获取加速计数据?也就是说,如果在倾斜设备时输入了正确的数字?如果你没有得到正确的数字,那么你给你的身体提供的任何东西都将是无用的。@fluity它起作用,但它会把气球从屏幕上移开。我甚至不能左右倾斜我的手机,因为它只能按我倾斜的方式移动。我真的挂上了应用程序,气球飞走了,我可以移动它,但是如果我把手机调到所有这些不同的疯狂位置,你的设备有可能出现故障吗?你能用另一个测试吗?或者,当你向左/向右倾斜时,你能通过打印原始数据来确认你的设备是否正常工作吗?@FLUITY我不这么认为,我相信我会找出明显的故障排除错误