gpt4 book ai didi

ios - 尝试使用 SpriteKit 场景 (SWIFT) 在 didMoveToView 中使用 waitForDuration 来延迟生成节点,但不起作用

转载 作者:行者123 更新时间:2023-11-30 13:25:03 28 4
gpt4 key购买 nike

我试图阻止我的节点在游戏开始时掉落一两秒。所以我的问题是,当我按下启动按钮时,节点已经位于屏幕的中间位置。我还尝试更改节点的启动高度,但这似乎是一个成本高昂的解决方案,因为我想小心不要让 FPS 变得太低。在我的代码中,我尝试在 didMoveToView 中执行此操作,并且我使用 waitForDuration 但它不起作用。

Example Image of Nodes Falling Down

有SpriteKit高手知道我该怎么办吗?我正在使用 swift 。这是我的代码:

    override func didMoveToView(view: SKView) {

let wait = SKAction.waitForDuration(2.5)
let run = SKAction.runBlock {
self.spawnNumbers()
}
numContainer.runAction(SKAction.sequence([wait, run]))
}

func spawnNumbers() {

let minValue = self.size.width / 8
let maxValue = self.size.width - 36

let spawnPoint = CGFloat(arc4random_uniform(UInt32(maxValue - minValue)))
let action = SKAction.moveToY(-300, duration: 2)

numContainer = SKSpriteNode(imageNamed: "Circle")
numContainer.name = "Circle"
numContainer.size = CGSize(width: 72, height: 72)
numContainer.anchorPoint = CGPointMake(0, 0)
numContainer.position = CGPoint(x: spawnPoint, y: self.size.height)
numContainer.runAction(SKAction.repeatActionForever(action))
numContainer.zPosition = 2

let numberLabel = SKLabelNode(fontNamed: "AvenirNext-Bold")
numberLabel.text = "\(numToTouch)"
numberLabel.name = "Label"
numberLabel.zPosition = -1
numberLabel.position = CGPointMake(CGRectGetMidX(numContainer.centerRect) + 36, CGRectGetMidY(numContainer.centerRect) + 36)
numberLabel.horizontalAlignmentMode = .Center
numberLabel.verticalAlignmentMode = .Center
numberLabel.fontColor = UIColor.whiteColor()
numberLabel.fontSize = 28

addChild(numContainer)
numContainer.addChild(numberLabel)
numContainerArray.append(numContainer)
numToTouch += 1
}

最佳答案

我认为你必须将 2.5 秒转换为 NSTimeInterval:

 let wait = SKAction.waitForDuration(NSTimeInterval(2.5))

您还可以尝试将 Action 放入场景的 init 函数中:

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

//your code from before
}

另外,不确定这是否重要,但这就是我通常所做的并且它有效,

 let wait = SKAction.waitForDuration(NSTimeInterval(2.5))
let action = SKAction.runBlock({() in self.spawnNumbers()})
let actionThenWait = SKAction.sequence([wait, action])
self.runAction(actionThenWait)

关于ios - 尝试使用 SpriteKit 场景 (SWIFT) 在 didMoveToView 中使用 waitForDuration 来延迟生成节点,但不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37331389/

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