我的快速游戏存在问题。我想每2秒产卵两个精灵并将它们像飞扬的鸟管一样移动。我尝试过这种方式,但延迟后我的游戏崩溃了。这里是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:
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)
}
任何想法?由于
也许尝试使用NSTimer()来产生管道? – TheCodeComposer
@TheCodeComposer使用NSTimer产生对象可能是一个坏主意,因为它不受视图,场景或节点的暂停状态的影响。因此,使用动作序列或甚至更新产生:方法和它的currentTime传递参数将是SpriteKit中的首选方式。 – Whirlwind