gpt4 book ai didi

ios - 从更新函数调用时,自定义 SKShapeNode 未出现在场景中

转载 作者:行者123 更新时间:2023-11-30 12:26:02 26 4
gpt4 key购买 nike

//SpawnBlock 初始化

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

在模拟器中运行时,节点计数增加,计时器工作并取消初始化 Sprite ,但它们不会出现在场景中。我是否遗漏了什么?

最佳答案

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


}

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

关于ios - 从更新函数调用时,自定义 SKShapeNode 未出现在场景中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44206867/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com