swift - 错误 : Can't capture before it is declared . .. iOS 快速

标签 swift

我的错误出现在“loadMan”的函数中,指出在声明之前无法捕获“loadmanTextures”。我不明白这是怎么回事......我已经在下面添加了“loadmanTextures”的功能。我该怎么办???

import SpriteKit


class GameScene: SKScene {
var man:SKSpriteNode
var runningManTextures = [SKTexture]()


override init(size: CGSize){

    self.man = SKSpriteNode(texture: SKTexture(imageNamed: "man_0"))



    super.init(size:size)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func didMoveToView(view: SKView) {

    func loadMan() {
        loadManTextures()
        man.position.y -= man.size.height/2
        addChild(man)
    }

    func loadManTextures() {
        var runningAtlas = SKTextureAtlas(named: "man")
        for i in 1...3 {
            var textureName = "man_\(i)"
            var temp = runningAtlas.textureNamed(textureName)
            runningManTextures.append(temp)
        }
    }

}

最佳答案

错误消息非常清楚。也许你不明白自己代码的结构?您有意或无意地将两个函数放在另一个函数中(也称为函数中函数本地函数):

override func didMoveToView(view: SKView) {
    func loadMan() {
        loadManTextures()
    }
    func loadManTextures() {
    }
}

我不知道你为什么这么做。但如果您这样做,那么第一个函数就无法调用第二个函数,因为这现在是可执行代码 - loadManTextures 声明现在必须执行,以便该函数>存在,首先,在调用之前:

override func didMoveToView(view: SKView) {
    func loadManTextures() {
    }
    func loadMan() {
        loadManTextures()
    }
}

当然,不使用函数中的函数,而是让它们都是方法会更有意义:

override func didMoveToView(view: SKView) {
}
func loadMan() {
    loadManTextures()
}
func loadManTextures() {
}

现在顺序并不重要。

关于swift - 错误 : Can't capture before it is declared . .. iOS 快速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29453135/

相关文章:

swift - 函数会导致循环引用吗?

objective-c - WKWebView - 替换网络操作

ios - Swift 不能使用函数指针吗?

ios - 衡量强/弱ARC引用的影响-对执行时间的影响

ios - UITableViewCell、图像和纵横比

ios - 如何在 swift 中使用 cocoahttpserver 流式传输音频(设备歌曲)?

swift - 在非沙盒中使用 Cocoa NSSavePanel 会导致断言失败

ios - 以编程方式呈现新的 ViewController [Swift 3]

ios - 子类中是否有符合自定义协议(protocol)的常规空白函数?

json - Swift 传递方法参数 Struct Decodable