ios - 如何在 Sprite Kit 的 Action 中运行函数?

标签 ios swift sprite-kit

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

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

但出于某种原因,我不断收到错误提示“'(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)
    }
}

最佳答案

最小的、完整的和可验证的

首先,我们需要一个Minimal, Complete, and Verifiable example像这样:

class GameScene: SKScene {

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

    func createCars() { }
}

error: value of type '(NSObject) -> () -> GameScene' has no member 'createCars'

问题

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

解决方案

但是你可以使用一个惰性属性,它只会在被调用时被评估(那时 self 将可用)。

class GameScene: SKScene {

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

    func createCars() { }
}

关于ios - 如何在 Sprite Kit 的 Action 中运行函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43057965/

相关文章:

ios - iOS 上的 OpenGL。可以在单独的线程上调用 glTexImage2D 吗?

iphone - 具有不同 TableView 标签的 TableView 的问题

swift - 使用 NSUser Defaults 保存当前游戏分数(不是高分)?

swift - 为什么我的代码从未检测到与我的 SKPhysicsBody 有任何接触?

ios - 单个 Storyboard上的所有 View Controller

iphone - 如何在 iOS 上设置向上移动的渐变动画

ios - Swift iOS 8 中的导航验证

ios - 运行项目而无需访问真实设备的框架

ios - 根据(城市)从 firebase 检索数据

ios - 如何让我的节点以较慢的持续时间旋转?