2015-07-13 78 views
2

我的快速游戏存在问题。我想每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) 
} 

任何想法?由于

+0

也许尝试使用NSTimer()来产生管道? – TheCodeComposer

+0

@TheCodeComposer使用NSTimer产生对象可能是一个坏主意,因为它不受视图,场景或节点的暂停状态的影响。因此,使用动作序列或甚至更新产生:方法和它的currentTime传递参数将是SpriteKit中的首选方式。 – Whirlwind

回答

3

我觉得你的问题是在这里

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) 
} 

,或者如果意图是前

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

希望这是有道理的和有利于只有一套管其中的一个时间呼叫

pipe1.removeFromParent() 
pipe2.removeFromParent() 

+0

它的工作原理!非常感谢!从来没有想过问题是外部宣布的管道常量。 –

+0

@ ClaudioD'Agostino乐于助人。随意标记此答案为接受:) –