gpt4 book ai didi

swift 游戏无限产卵不起作用

转载 作者:可可西里 更新时间:2023-11-01 01:05:35 25 4
gpt4 key购买 nike

我的 swift 游戏有问题。我想每 2 秒生成两个 Sprite 并像飞扬的鸟管一样移动它们。我尝试过这种方式,但延迟后我的游戏崩溃了。这是 DidMoveToView:(我直接跳到有趣的部分)

let distanceToMove = CGFloat(self.frame.size.width + 140)
let movePipes = SKAction.repeatActionForever(SKAction.moveByX(-distanceToMove, y: 0, duration: NSTimeInterval(0.01 * distanceToMove)))
let removePipes = SKAction.removeFromParent()
moveAndRemove = SKAction.sequence([movePipes,removePipes])


let delay = SKAction.waitForDuration(NSTimeInterval(2.0))
let spawn = SKAction.runBlock({() in self.initPipes()})
let spawnAndDelay = SKAction.sequence([spawn,delay])
let spawnAndDelayForever = SKAction.repeatActionForever(spawnAndDelay)
runAction(spawnAndDelayForever)

这是管道的功能:

func initPipes() {
let pY = arc4random_uniform(UInt32(self.size.height - 250) + 250)
let pipePair = SKNode()
pipePair.position = CGPoint(x: self.frame.size.width + 70, y: 0)
//PIPE 1
pipe1.anchorPoint = CGPointMake(0, 0)
pipe1.position = CGPoint(x: 0, y: Int(pY))
pipe1.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(70, 700), center: CGPointMake(70/2, 700/2))
pipe1.physicsBody?.dynamic = false
pipe1.physicsBody?.affectedByGravity = false
pipePair.addChild(pipe1)

//PIPE 2
pipe2.anchorPoint = CGPointMake(0,1)
pipe2.position = CGPoint(x: 0, y: pipe1.position.y - 150)
pipe2.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(70, 700), center: CGPointMake(35, -700/2))
pipe2.physicsBody?.dynamic = false
pipe2.physicsBody?.affectedByGravity = false
pipePair.addChild(pipe2)

pipePair.runAction(moveAndRemove)
addChild(pipePair)
}

有什么想法吗?谢谢

最佳答案

我认为你的问题在这里

pipePair.addChild(pipe1)

您不是在创建新管道,而是使用在其他地方创建的旧管道。第一次通过时不会有问题,但如果在尝试将 pipe1 添加到新父级之前没有从其父级中删除它,它将崩溃。我会在那个方法中创建你的 pipe1 和 pipe2,而不是在它之外有一个变量。

func initPipes() {
let pY = arc4random_uniform(UInt32(self.size.height - 250) + 250)
let pipePair = SKNode()
pipePair.position = CGPoint(x: self.frame.size.width + 70, y: 0)
//PIPE 1
let pipe1 = //whatever pipe1 is
pipe1.anchorPoint = CGPointMake(0, 0)
pipe1.position = CGPoint(x: 0, y: Int(pY))
pipe1.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(70, 700), center: CGPointMake(70/2, 700/2))
pipe1.physicsBody?.dynamic = false
pipe1.physicsBody?.affectedByGravity = false
pipePair.addChild(pipe1)

//PIPE 2
let pipe2 = //whatever pipe1 is
pipe2.anchorPoint = CGPointMake(0,1)
pipe2.position = CGPoint(x: 0, y: pipe1.position.y - 150)
pipe2.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(70, 700), center: CGPointMake(35, -700/2))
pipe2.physicsBody?.dynamic = false
pipe2.physicsBody?.affectedByGravity = false
pipePair.addChild(pipe2)

pipePair.runAction(moveAndRemove)
addChild(pipePair)
}

或者如果打算一次只调用一组管道

pipe1.removeFromParent()
pipe2.removeFromParent()

之前

pipePair.addChild(pipe1)
pipePair.addChild(pipe2)

希望这是有道理的并且有帮助。

关于 swift 游戏无限产卵不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31374213/

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