在我的班级场景中,我有:
let health = SKSpriteNode(imageNamed: "healthBar")
当狗狗对敌人造成伤害时,我希望敌人在它上方产生一个健康栏,显示其当前的健康状况。
如果我将enemy.addChild(health)行更改为self.addChild(health),则下面的代码可以正常工作。但是当它是敌人.addChild(健康)时,健康栏是完全不可见的。我知道健康酒吧就在那里,因为我可以打印敌人。孩子们在那里展示但它不可见。有什么建议?
**注意我不能只使用self.addChild(健康),因为我需要多个健康栏跟随多个敌人位置。这就是为什么我需要每个敌人拥有自己的儿童健康栏。
if body1.categoryBitMask == PhysicsCategories.DodgingDogPlayer && body2.categoryBitMask == PhysicsCategories.Enemy{ //if the player has hit an enemy
if dogAttacking == true{
dogAttackAnimation.position = CGPoint(x: dodgingDogPlayer.position.x, y: dodgingDogPlayer.position.y + 80)
dogAttackAnimating()
let waitToFade = SKAction.wait(forDuration: 0.5)
let fadeOutSequence = SKAction.sequence([waitToFade, fadeOut])
dogAttackAnimation.run(fadeOutSequence)
if body2.node?.name == "Orange Cat" || body2.node?.name == "Brown Cat" || body2.node?.name == "Black Cat"{
if ( body2.node?.childNode(withName: "health") != nil) {
print ("already has health")
if let enemy = contact.bodyB.node as? Enemy{
health.xScale = CGFloat(fixedHealthBarLength * CGFloat(enemy.healthStat))
health.position = CGPoint(x: enemy.position.x, y: enemy.position.y + 50)
health.texture?.filteringMode = SKTextureFilteringMode.nearest
enemy.healthStat -= 1
if enemy.healthStat <= 0{
body2.node?.removeFromParent()
}
}
} else if ( body2.node?.childNode(withName: "health") == nil) {
print ("doesnt have health")
health.yScale = 2
health.zPosition = 10
health.name = "health"
if let enemy = contact.bodyB.node as? Enemy{
health.anchorPoint = CGPoint(x: 0.5, y: 0)
health.xScale = CGFloat(fixedHealthBarLength * CGFloat(enemy.healthStat))
health.position = CGPoint(x: enemy.position.x, y: enemy.position.y + 50)
health.alpha = 1
health.texture?.filteringMode = SKTextureFilteringMode.nearest
enemy.addChild(health)
enemy.healthStat -= 1
if enemy.healthStat <= 0{
body2.node?.removeFromParent()
}
}
}
}
}