2017-05-26 63 views
0

//初始化SpawnBlock定製SKShapeNode沒有出現在現場的更新功能,被稱爲

class SpawnBlock : SKShapeNode { 

var timer: Timer? 
init(circleOfRadius radius: CGFloat) { 
    super.init() 

    self.alpha = 1 
    self.zPosition = 2 
    self.fillColor = UIColor.blue 
    self.strokeColor = UIColor.blue 
    self.physicsBody = SKPhysicsBody(circleOfRadius: radius) 
    self.physicsBody?.affectedByGravity = false 
    self.physicsBody?.categoryBitMask = Collisions.spawnBlock.rawValue 
    self.physicsBody?.contactTestBitMask = Collisions.block.rawValue 

} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
    } 
} 

//產卵的功能

func spawnABlock() { 
    let spawnBlock = SpawnBlock(circleOfRadius: 10) 
    let randomPos = CGPoint(x: CGFloat(arc4random_uniform(UInt32(self.frame.width))), y: CGFloat(arc4random_uniform(UInt32(self.frame.height)))) 

    print("\(randomPos)") 

    spawnBlock.timer = Timer.scheduledTimer(withTimeInterval: 10, repeats: false, block: { (Timer) in 
     print("timer done") 
     spawnBlock.removeFromParent() 
    }) 
    spawnBlock.position = randomPos 
    self.addChild(spawnBlock) 

} 

override func update(_ currentTime: TimeInterval) { 
    count += 1 
    print("\(count)") 
    if count == maxCount { 
     count = 0 
     spawnABlock() 
    } 
} 

當在模擬器中運行,節點數的增加,當定時器的工作和精靈的精靈,但他們不出現在現場。有一件我失蹤或什麼?

回答

0
override init() { 
    super.init() 
} 

convenience init(width: CGFloat) { 
    self.init() 
    self.init(circleOfRadius: width/2) 

    print("initialsed") 

    self.alpha = 1 
    self.zPosition = 2 
    self.fillColor = UIColor.blue 
    self.strokeColor = UIColor.blue 
    self.physicsBody = SKPhysicsBody(circleOfRadius: width/2) 
    self.physicsBody?.affectedByGravity = false 
    self.physicsBody?.categoryBitMask = Collisions.spawnBlock.rawValue 
    self.physicsBody?.collisionBitMask = Collisions.block.rawValue 
    self.physicsBody?.fieldBitMask = Collisions.noField.rawValue 


} 

將這些代碼塊添加到原始初始化函數中的塊中。