从更新功能

时间:2017-05-26 17:08:41

标签: ios swift

// SpawnBlock Init

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

// Spawn函数

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

在模拟器中运行时,节点计数增加,计时器工作并取消精灵,但它们不会出现在场景中。是否有一件我遗失的东西?

1 个答案:

答案 0 :(得分: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


}

为原始初始化函数添加这些代码块会在块中生成。