Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 获取雪碧';他在另一个班级的职位_Ios_Swift_Sprite Kit - Fatal编程技术网

Ios 获取雪碧';他在另一个班级的职位

Ios 获取雪碧';他在另一个班级的职位,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,这可能是个愚蠢的问题,但我的情况是这样的 我在屏幕上有一个从左到右的SKShapeNoderect,如下所示: // GameScene.swift var rect = SKShapeNode() var counter = 0{ didSet{ rect.position = CGPoint(x: CGFloat(counter) , y: frame.midY) if CGFloat(counter) > fram

这可能是个愚蠢的问题,但我的情况是这样的

我在屏幕上有一个从左到右的
SKShapeNode
rect,如下所示:

      // GameScene.swift

  var rect = SKShapeNode()
  var counter = 0{

    didSet{  
        rect.position = CGPoint(x: CGFloat(counter) , y: frame.midY)

        if CGFloat(counter) > frame.width{
            counter = 0
      }}}


override func update(_ currentTime: TimeInterval) {
    counter =       counter + 4     
}
在ViewController.swift中,我尝试像这样获取
rect.position
,我知道这是错误的,因为它创建了一个新实例

//ViewController.swift

 let gameScene = GameScene()

@IBAction func button(_ sender: Any) {

//        gameScene.rect.position = games.frame.CGPoint(x: 200, y: 400)
          print(gameScene.rect.position)         // Always returns (0,0)

}
问题:如何从另一个类实时获取
rect.position
。因此,每当我按下按钮时,我都会得到
rect
的实际位置

更新

根据Ron的建议,我更新了我的
ViewController.swift
中的
viewDidLoad
方法,如下所示:

 let gameScene = GameScene()

   override func viewDidLoad() {
    super.viewDidLoad()


    if let view = self.spriteView {
        // Load the SKScene from 'GameScene.sks'
        if let scene = SKScene(fileNamed: "GameScene") {

            // Set the scale mode to scale to fit the window
            scene.scaleMode = .aspectFill

            // Present the scene
            view.presentScene(scene)
              }}
此:

   var gameScene : GameScene!

   override func viewDidLoad() {
    super.viewDidLoad()

    if let view = self.spriteView {
        // Load the SKScene from 'GameScene.sks'
        if let scene = GameScene(fileNamed: "GameScene") {    // SKScene changed to GameScene

            self.gameScene = scene                           // scene assigned to gameScene variable

            // Set the scale mode to scale to fit the window
            scene.scaleMode = .aspectFill

            // Present the scene
            view.presentScene(scene)
        }
    }
意图 我想在单击
play
按钮时获取移动条的位置。 请注意,
游戏场景
仅代表实际屏幕的一部分


当您第一次过渡到游戏场景时(假设您直接从GameViewController过渡到游戏场景),请为游戏场景创建一个类级别变量。然后,当您需要来自游戏场景的信息时,使用相同的变量,而不是创建新的游戏场景变量

var gameScene: GameScene!

override func viewDidLoad() {

    super.viewDidLoad()

    if let view = self.view as! SKView? {

        // Load the SKScene from 'GameScene.sks'
        if let gameScene = GameScene(fileNamed: "GameScene")  {
            self.gameScene = gameScene
            gameScene = .aspectFill
            // Present the scene
            view.presentScene(gameScene)
        }
    }
}

func getCoords() {
    print("gameScene.rect.position \(gameScene.rect.position)")
}

print(gamesecene.rect.position)
看起来不错。我怀疑你是对的,但实际上是
(0,0)
@shallow尽管他使用的游戏场景实例与对象在其中移动的实例不同。它将始终为0,0,因为它是的新空白实例GameScene@RonMyschuk我也这么认为,但由于游戏场景是一个常量,因此不可能在其他地方重新初始化它。这里的部分问题是我们不知道它是否是常量。这是断章取义的,他没有向我们展示他是如何实例化他的游戏场景的。这可能是一个常数,但他也可能永远不会将他的游戏场景实例指定给它variable@shallowThought实际上,我刚刚意识到他不能将GameSecene的实例分配给该变量,因为它是一个“let”。是的,虽然这是一个常量变量,但它不是他需要的变量。从我们没有从OP那里得到任何反馈来看,我想我们比他更关心这个问题;)