2017-05-05 2 views
3

내 GameplayScene에서 다른 클래스를 사용하는 것에 대한 질문이 있습니다. 내가하려고하는 것은 휴대 전화의 X 축의 동작을 좌우로 움직이는 것입니다. 여기에 내가 이 클래스를 다른 클래스로 활성화하는 방법

import SpriteKit 
import CoreMotion 

class MotionClass: SKScene { 

    var player: Player? 

    var motionManager = CMMotionManager() 
    var destX: CGFloat = 0.0 

    override func sceneDidLoad() { 

      motionManager.accelerometerUpdateInterval = 0.2 
      motionManager.startAccelerometerUpdates(to: OperationQueue.current!) { (data, error) in 
       if let myData = data { 

        let currentX = self.player?.position.x 

        if myData.acceleration.x > 0.2 { 
         self.destX = currentX! + CGFloat(myData.acceleration.x * 100) 
         print("Tilted Right") 

        } else { 

        if myData.acceleration.x < -0.2 { 
         self.destX = currentX! + CGFloat(myData.acceleration.x * 100) 
         print("Tilted Left") 
        } 
       } 
      } 
     } 
    } 

    override func update(_ currentTime: TimeInterval) { 

     let action = SKAction.moveTo(x: destX, duration: 1) 
     self.player?.run(action) 
    } 

} 

지금 나는 motionBegan 기능 내 GameplayScene.swift이 클래스를 호출하기 위해 노력하고있어 내 MotionClass.swift에있는,하지만 난 그 일에 대해 이동하는 방법을 모르는 것입니다. MotionClass에 변수 'grapple'이 있습니까? 그러나 나는 거기에서 어디로 가야할지 모른다. 누구든지이 일을 잘 수행 할 수 있습니까?

답변

1

여러분의 MotionClass가 현재 가지고있는 SKScene 하위 클래스의 목적에 대해 혼란 스러울 수도 있습니다. (주된 아이디어는) 한 번에 하나의 SKScene 만 사용하는 것입니다 : MotionClass의 내용이 필요하다면 SKScene 서브 클래스가 아닌 일반 클래스로 만들어야합니다. 난 당신이 또한뿐만 아니라 좀 더 OOP을 숙지해야 할 수 있습니다 생각

... static 특성/기능의 외부, 당신은 객체를 만드는 (클래스, 당신은를 인스턴스화 "전화"하지 않습니다 : 당신은 당신이 GamePlayClass에 액세스 할 MotionClass에 케이크가있는 경우])

그래서, 당신은 나는 ... 이것은 간단한 전역 변수와 함께 할 수있는 MotionClass 객체

에 대한 참조가 필요합니다 GameViewController에 넣으십시오 .swift :

// Here is a global reference to an 'empty' motion class object.. 
var global_motionClassObject = MotionClass() 

class GameViewController: UIViewController { 

    override func viewDidLoad() { 
    super.viewDidLoad() 

    // ... 
    if let view = self.view as! SKView? else { 
     let scene = MotionClass(size: view.frame.size) 
     // Assign our global to the new scene just made: 
     global_motionClassObject = scene 
     scene.scaleMode = .aspectFit 

     view.presentScene(scene) 
    } 

    // ... 
} 

이제 GamePlayClass, 또는 어디든지 다른 사람의 내부에, 당신은 당신이 다른 무언가로 MotionClass을 구조 조정해야 할 수도 있습니다 걱정 때문에 원하는 결과를 제공하지 않을 수 있습니다, 그러나

global_motionClassObject를 호출하여 MotionClass에 액세스 할 수 있습니다 SKScene보다 :)

+1

나는 이미 scene = GameplayScene을 가지고있다. 나는 그것을 바꿔야 만한다. –

+0

그럴 수 없습니다 .. 만약 motionclass에'.presentScene'이 없다면 GamePlayScene의 _inside_를 인스턴스화 할 수 있습니다 ... 장면 사이를 앞뒤로 바꾸지 않는 한 전역 적 필요 없음 – Fluidity

+1

그냥하고 싶습니다. 초기 var motionManager : MotionClass? 이후에 내 GameplayScene에이를 구현하는 방법을 알고 싶습니다. –

관련 문제