什么是正确的方法,使用“init”或“didmove”?

时间:2023-01-22 23:12:59

Language: Swift 3.0 --- IDE : Xcode 8.0 beta 2 --- Project : iOS Game (SpriteKit)

语言:Swift 3.0 --- IDE:Xcode 8.0 beta 2 ---项目:iOS游戏(SpriteKit)

I create a game for iOS and i know Apple is really strict with their rules to accept the app/game. So i want to know which is the correct way to setup a game.

我为iOS创建了一款游戏,我知道Apple对接受应用/游戏的规则非常严格。所以我想知道哪种是正确的游戏设置方式。

I learned from google to create a new SpriteKit Project and do the following setup :

我从谷歌学到了创建一个新的SpriteKit项目并执行以下设置:

In GameViewController.swift clear viewDidLoad() and add all this :

在GameViewController.swift中清除viewDidLoad()并添加所有这些:

override func viewDidLoad() {
    super.viewDidLoad()
    let skView = self.view as! SKView

    let scene = GameScene(size: skView.bounds.size)
    scene.scaleMode = .aspectFit

    skView.presentScene(scene)
}

In GameScene.swift delete everything and leave this code :

在GameScene.swift中删除所有内容并保留此代码:

import SpriteKit

class GameScene: SKScene {
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(size: CGSize) {
        super.init(size: size)
        // add all the code of the game here...
    }

}

and develop my game inside override init

并在覆盖init中开发我的游戏

But I think thats actually wrong to start the game with init. And that the right way is to use the didMove() method. So should the code be written inside here? :

但我认为用init启动游戏实际上是错误的。而正确的方法是使用didMove()方法。那么代码应该写在这里吗? :

override func didMove(to view: SKView) {
    <#code#>
}

Does anyone know which one is the correct way? And why? Also if its wrong the way i do it, can you explain me how to use didMove method?

有谁知道哪一个是正确的方法?为什么?如果我的方式错了,你能解释一下如何使用didMove方法吗?

Don't know if this is a silly question just bothered me that using init is wrong and wanted to ask if someone knows more about this.

不知道这是一个愚蠢的问题,只是让我感到困扰,使用init是错误的,并想问一下是否有人知道更多。

1 个解决方案

#1


5  

Overriding the SKScene init

You can override the initializers of SKScene like described by @Knight0fDragon

您可以覆盖SKScene的初始化程序,如@ Knight0fDragon所述

class GameScene : SKScene {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()             
    }

    override init() {
        super.init()
        setup()
    }

    override init(size: CGSize) {
        super.init(size: size)
        setup()
    }

    func setup()
    {
        // PUT YOUR CODE HERE

    }
}

Or you can use the didMove(to:)

class GameScene: SKScene {
    override func didMove(to view: SKView) {
        super.didMove(to: view)
        // PUT YOUR CODE HERE <-----
    }
}

The init is called only when the scene is initialised. The didMove is called when the scene is presented into the view.

仅在初始化场景时调用init。当场景呈现在视图中时,将调用didMove。

#1


5  

Overriding the SKScene init

You can override the initializers of SKScene like described by @Knight0fDragon

您可以覆盖SKScene的初始化程序,如@ Knight0fDragon所述

class GameScene : SKScene {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()             
    }

    override init() {
        super.init()
        setup()
    }

    override init(size: CGSize) {
        super.init(size: size)
        setup()
    }

    func setup()
    {
        // PUT YOUR CODE HERE

    }
}

Or you can use the didMove(to:)

class GameScene: SKScene {
    override func didMove(to view: SKView) {
        super.didMove(to: view)
        // PUT YOUR CODE HERE <-----
    }
}

The init is called only when the scene is initialised. The didMove is called when the scene is presented into the view.

仅在初始化场景时调用init。当场景呈现在视图中时,将调用didMove。