CMMotionManager倾斜移动节点左右Swift 4

问题描述:

我有一个单独的SKNode类,名为Balloon,位于下方。CMMotionManager倾斜移动节点左右Swift 4

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") 
    } 
} 

然后我在下面的GameScene中使用这个类。

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); 
    } ]; 
    } 
+0

你能确认你正在获取加速计数据吗?也就是说,如果在您倾斜设备时您有正确的号码?如果你没有得到正确的数字,那么无论你送给你的physicsBody将是无用的。 – Fluidity

+0

@Fluidity它可以工作,但它会将气球从屏幕上移开。我甚至无法左右倾斜手机,只能移动我倾斜的方式。我真的把应用程序和气球飞走了,我有点可以移动它,但如果我把我的手机在所有这些不同类型的疯狂职位 – Dewan

+0

是否有可能您的设备出现故障?你可以用另一个测试吗?或者,您是否可以通过在向左/向右倾斜时打印原始数据来确认设备运行正常? – Fluidity

没关系,我明白了。

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) 
      } 
     }