在我的课堂场景下,我有:
let health = SKSpriteNode(imageNamed: "healthBar")
当狗对敌人造成伤害时,我希望敌人在其上方生成一个健康条,以显示其当前的健康状况。
如果我将enemy.addChild(health) 行更改为self.addChild(health),下面的代码可以正常工作。但是当它是enemy.addChild(health) 时,生命值条是完全不可见的。我知道健康栏在那里是因为我可以打印enemy.children,它在那里显示但不可见。有什么建议么?
**注意我不能只使用 self.addChild(health) ,因为我需要多个健康条来跟踪多个敌人的位置。这就是为什么我需要每个敌人都有自己的儿童健康栏。
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()
}
}
}
}
}