2015-07-13 111 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樂於助人。隨意標記此答案爲接受:) –

相關問題