2017-09-21 1 views
1

다음과 같은 별도의 SKNode 클래스 인 풍선이 있습니다.CMMotionManager 틸트 이동 노드 왼쪽 및 오른쪽 스위프트 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) 
     } 
} 

기본적으로 내가 무엇을 기본적으로 수행하고자하는 것은 어느 방법으로 내 아이폰을 기울 노드를 이동 세로 모드 및 타일 왼쪽 또는 오른쪽에있는 응용 프로그램을 사용합니다. 아래는 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

가속도계 데이터를 받고 있는지 확인할 수 있습니까? 즉, 기기를 기울일 때 정확한 숫자가 입력되면 어떻게됩니까? 올바른 숫자를 얻지 못하면 물리 치료제를 먹을 때 아무 것도 쓸모가 없습니다. – Fluidity

+0

@Fluidity 작동하지만 풍선을 화면 밖으로 이동시킵니다. 나는 기울 이기만하기 위해 휴대폰을 좌우로 기울일 수 없다. 글자 그대로 앱을 올려 놓으면 풍선이 날아가고 나는 움직일 수는 있지만,이 모든 다른 유형의 미친 위치에서 휴대 전화를 켜면 – Dewan

+0

기기가 오작동 할 수 있습니까? 다른 테스트 할 수 있습니까? 또는 왼쪽/오른쪽으로 기울일 때 원시 데이터를 인쇄하여 장치가 올바르게 작동하는지 확인할 수 있습니까? – Fluidity

답변

1

신경 쓰지 마라.

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