2016-01-22 70 views
1

我有一個問題,爲什麼我的SpriteKit節點沒有從下面的代碼中從我的父節點中刪除。SpriteKit節點沒有被從父節點刪除

我的項目目前有兩個部分,一個是GameScene類,它在您創建SpriteKit項目時出現默認值,另一個是我在遊戲中的每個圓上操作的Circles類。

的圓圈被存儲在數組中的位置:

var circlesInPlay = [Circles]() 

基本上,我試圖設計一簡單的遊戲,圓圈尺寸減小,並且當它們是0寬度的,從屏幕除去。

我的代碼要做到這一點就在這裏,在

override func update(currentTime: CFTimeInterval) { 


    timeSinceUpdate = timeSinceUpdate + 1 
    print("time since update: " + String(timeSinceUpdate)) 

    if timeForUpdate == timeSinceUpdate { 
     newCircle() 
     timeSinceUpdate = 0 
     //timeForUpdate = Int(arc4random_uniform(100) + 1) 
     timeForUpdate = 100 
    } 

    //Check to see if circle transparancies are 0 

    if checkGameOver() { 

     gameOver() 
    } 

    updateCircles() 
    removeCircles() 

的timeSinceUpdate變量是自上次圈已經被放置到屏幕上,它通過一個每幀更新增加了時間。

updateCircles()調用此,

func updateCircles() { 

    for c in circlesInPlay { 

     c.update() 

    } 

} 

其中要求在圓類我在另一個迅速文件進行更新():

func update() { 

    transparancy = transparancy - transparancyDecrease 
    size = size - ds 

    if (size <= 0) { 
     node.removeFromParent() 
    } 

    node.size.height = size 
    node.size.width = size 

} 

從更新功能的其他調用removeCircles( )在這裏

func removeCircles() { 

    var posn = circlesInPlay.count - 1 

    for c in circlesInPlay { 

     if (c.size < 0) { 

      c.produceNode().removeFromParent() 
      circlesInPlay.removeAtIndex(posn) 
      posn = posn - 1 

     } 

    } 

} 

我真的得到的是我不知道爲什麼圈子是索姆etimes卡住或沒有被從屏幕上移除。

任何幫助,非常感謝!

整個遊戲代碼如下:

import SpriteKit 

class GameScene: SKScene { 

    var bgImage = SKSpriteNode(imageNamed: "background.png") 

    let screenSize = UIScreen.mainScreen().bounds 

    var timeSinceUpdate: Int = 0 

    var circlesInPlay = [Circles]() 

    var timeForUpdate = Int(arc4random_uniform(200) + 1) 

    override func didMoveToView(view: SKView) { 

     bgImage.position = CGPointMake(self.size.width/2, self.size.height/2) 
     bgImage.zPosition = -100 
     self.addChild(bgImage) 






    } 

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     /* Called when a touch begins */ 

     for touch in touches { 
      let location = touch.locationInNode(self) 

      if !isTouchInCircle(location) { 

       gameOver() 
      } 


     } 
    } 

    override func update(currentTime: CFTimeInterval) { 
     /* Called before each frame is rendered */ 

     timeSinceUpdate = timeSinceUpdate + 1 
     print("time since update: " + String(timeSinceUpdate)) 

     if timeForUpdate == timeSinceUpdate { 
      newCircle() 
      timeSinceUpdate = 0 
      //timeForUpdate = Int(arc4random_uniform(100) + 1) 
      timeForUpdate = 100 
     } 

     //Check to see if circle transparancies are 0 

     if checkGameOver() { 

      gameOver() 
     } 

     updateCircles() 
     removeCircles() 

    } 

    func newCircle(){ 

     let sizeX = UInt32(CGRectGetMaxX(self.frame)) 
     let randomx = CGFloat(arc4random_uniform(sizeX)) 

     let sizeY = UInt32(CGRectGetMaxY(self.frame)) 
     let randomy = CGFloat(arc4random_uniform(sizeY)) 

     //let randomx = CGFloat(arc4random_uniform(UInt32(self.size.width))) 
     //let randomy = CGFloat(arc4random_uniform(UInt32(self.size.height))) 

     if (circlesInPlay.count < 5) { 

      circlesInPlay.append(Circles.init(x: randomx, y: randomy)) 
      self.addChild((circlesInPlay[circlesInPlay.count - 1]).produceNode()) 
     } 

    } 

    func checkGameOver() -> Bool { 


     for c in circlesInPlay { 

      if c.getTransparancy() == 0 { 

       return false 

      } 

     } 

     return true 
    } 

    func isTouchInCircle(touch: CGPoint) -> Bool { 

     for c in circlesInPlay { 

      if (c.getX() <= (touch.x * 1.10)) { 

       return true 
      } 

     } 

     return false 

    } 


    func updateCircles() { 

     for c in circlesInPlay { 

      c.update() 

     } 

    } 

    func removeCircles() { 

     var posn = circlesInPlay.count - 1 

     for c in circlesInPlay { 

      if (c.size < 0) { 

       c.produceNode().removeFromParent() 
       circlesInPlay.removeAtIndex(posn) 
       posn = posn - 1 

      } 

     } 

    } 


    func gameOver() { 


    } 

} 



import Foundation 
import SpriteKit 
import GameKit 


class Circles { 


    var node = SKSpriteNode() 

    var size: CGFloat = 200 
    var ds: CGFloat = 2 

    var colorC: String; 


    var xpos: CGFloat 
    var ypos: CGFloat 

    var transparancy: Int 
    var transparancyDecrease: Int = 1 



    let arrayColors = ["circleRed.png", "circleBlue.png", "circlePink.png", "circleGreen.png", "circleYellow.png"] 


    //The innitial constructor 
    init(x: CGFloat, y: CGFloat) { 

     self.xpos = x 
     self.ypos = y 

     self.transparancy = 100 

     let randomIndex = Int(arc4random_uniform(UInt32(arrayColors.count))) 

     colorC = arrayColors[randomIndex] 

     node.texture = SKTexture(imageNamed: colorC) 
     node.position = CGPointMake(self.xpos, self.ypos) 
     node.size.height = self.size 
     node.size.width = self.size 

    } 

    func produceNode() -> SKSpriteNode { 

     return node 

    } 


    func setColor() { 

     let randomIndex = Int(arc4random_uniform(UInt32(arrayColors.count))) 

     colorC = arrayColors[randomIndex] 



    } 


    func update() { 

     transparancy = transparancy - transparancyDecrease 
     size = size - ds 

     if (size <= 0) { 
      node.removeFromParent() 
     } 

     node.size.height = size 
     node.size.width = size 

    } 

    func getX() -> CGFloat { 

     return xpos 
    } 

    func getY() -> CGFloat { 

     return ypos 
    } 


    /* 
    func getColor() -> SKColor { 

     return colorC 

    }*/ 

    func getTransparancy() -> Int { 

     return transparancy 

    } 

} 
+0

如果c.size <= 0 {...}而不是c.size <0,則在removeCircles()方法中。 – Whirlwind

回答

1

的問題是,在這個片段:

 var posn = circlesInPlay.count - 1 

    for c in circlesInPlay { 

     if (c.size < 0) { 

      c.produceNode().removeFromParent() 
      circlesInPlay.removeAtIndex(posn) 
      posn = posn - 1 

     } 

    } 

你總是從你收集,而不是你是元素去掉最後一個元素在迭代中使用。在迭代thrue時更改集合時也要小心。如果從集合中刪除元素,索引將會更改。