gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 05:52:35 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com