如何在Sprite Kit中的一个动作中运行一个函数?

Pra*_*ran 1 ios sprite-kit swift

对于我正在制作的游戏,我有以下代码.在其中一个部分,我有

let spawn = SKAction.run {
    self.createCars()
}
Run Code Online (Sandbox Code Playgroud)

但由于某种原因,我不断收到一个错误,说"类型的值"(NSObject - >() - > GameScene'没有成员'createCars',即使createCars是我的代码中定义较低的函数.为什么会发生这种情况,什么可以我要解决这个问题吗?

import SpriteKit
import GameplayKit

class GameScene: SKScene, SKPhysicsContactDelegate {
    var lanecounter:CGFloat = 0
    var score:Int = 0
    var player:SKSpriteNode?
    let scoreLabel = SKLabelNode()
    let highScoreLabel = SKLabelNode()
    var direction:Int?
    let noCategory:UInt32 = 0
    let carCategory:UInt32 = 0b1
    let playerCategory:UInt32 = 0b1 << 1
    let pointCategory:UInt32 = 0b1 << 2
    var died:Bool?
    var restartBTN = SKSpriteNode()

    let moveLeft:SKAction = SKAction.moveBy(x: -100, y: 0, duration: 0.065)
    let moveRight:SKAction = SKAction.moveBy(x: 100, y: 0, duration: 0.065)
    let moveDown:SKAction = SKAction.moveTo(y: -800, duration: 1.1)

    let spawn = SKAction.run {
        self.createCars()
    }

    let delay = SKAction.wait(forDuration: 4)

    func createCars(){
        let middlePoints = SKSpriteNode()
        var openLane:CGFloat = 0

        middlePoints.size = CGSize(width: 100, height: 10)
        middlePoints.physicsBody = SKPhysicsBody(rectangleOf: middlePoints.size)
        //middlePoints.color = SKColor.purple
        middlePoints.physicsBody?.categoryBitMask = pointCategory
        middlePoints.physicsBody?.collisionBitMask = playerCategory
        middlePoints.physicsBody?.contactTestBitMask = playerCategory

        var randInt:Int = 0
        randInt = Int(arc4random_uniform(3))

        if lanecounter == -2 {
            if randInt == 0 || randInt == 1 {
                openLane = -1
            }
            if randInt == 2 {
                openLane = -2
            }
        }

        if lanecounter == 2 {
            if randInt == 0 || randInt == 1 {
                openLane = 1
            }
            if randInt == 2 {
                openLane = 2
            }
        }
        if lanecounter == -1 || lanecounter == 0 || lanecounter == 1 {
            if randInt == 0 {
                openLane = lanecounter - 1
            }
            if randInt == 1 {
                openLane = lanecounter
            }
            if randInt == 2 {
                openLane = lanecounter + 1
            }
        }
        //print("lanecounter is", lanecounter)
        //print("open lane is", openLane)

        if openLane != -2 {
            spawnCar(xCord: -2)
        }
        if openLane != -1 {
            spawnCar(xCord: -1)
        }
        if openLane != 0 {
            spawnCar(xCord: 0)
        }
        if openLane != 1 {
            spawnCar(xCord: 1)
        }
        if openLane != 2 {
            spawnCar(xCord: 2)
        }

        middlePoints.position = CGPoint(x: (openLane*100), y: self.frame.height)

        self.addChild(middlePoints)
        middlePoints.run(moveDown)
    }
}
Run Code Online (Sandbox Code Playgroud)

Luc*_*tti 7

最小,完整和可验证

首先,我们需要一个像这样的Minimal,Complete和Verifiable示例:

class GameScene: SKScene {

    let spawn = SKAction.run {
        self.createCars()
    }

    func createCars() { }
}
Run Code Online (Sandbox Code Playgroud)

错误:类型的值'(NSObject) - >() - > GameScene'没有成员'createCars'

问题

出现问题是因为您无法在使用它定义的同一行上填充属性self(事实上self还不存在).

但是你可以使用一个懒惰的属性,只有在被调用时才会被评估(并且在那个时候self可用).

class GameScene: SKScene {

    lazy var spawn: SKAction = {
        SKAction.run { self.createCars() }
    }()

    func createCars() { }
}
Run Code Online (Sandbox Code Playgroud)