我正在制作一个游戏,如果敌人与玩家的颜色相同,并且它与它接触并没有真正发生,但如果敌人的颜色与玩家的颜色不同并且它会接触,那么球员受到伤害。问题是一切都开始变得完美,直到不同颜色的敌人接触到玩家;相同颜色的敌人开始对玩家造成伤害。
if hits < 3 && circuloPrincipal.color != enemigo.color{
circuloPrincipal.runAction(SKAction.scaleBy(1.5, duration:0.1))
circuloPrincipal.runAction(SKAction.sequence([SKAction.colorizeWithColor(backgroundColor, colorBlendFactor: 1.0, duration: 0.1), SKAction.colorizeWithColor(UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0), colorBlendFactor: 1.0, duration: 0.1)]))
enemigo.removeAllActions()
enemigo.physicsBody?.affectedByGravity = false
enemigo.physicsBody?.dynamic = true
enemigo.removeFromParent()
hits++
}else if circuloPrincipal.color == enemigo.color {
enemigo.physicsBody?.affectedByGravity = true
enemigo.physicsBody?.dynamic = true
enemigo.removeFromParent()
}else {
gameStarted = false
enemigo.removeAllActions()
enemigo.removeFromParent()
enemigoTimer.invalidate()
}
}
答案 0 :(得分:1)
试试这个......不要试着比较精灵的.color。当SpriteKit通过SKAction为您的精灵分配新颜色时,这不是什么。如果你想知道原因,请参阅此结尾。
而是在类中设置一个变量来存储该信息并比较该变量。
var colorCurrent: UIColor = UIColor.red()
当敌人和玩家的颜色发生变化时,您将为此变量指定新颜色。
然后检测你的玩家和敌人类之间的联系,使用SpriteKit的didBeginContact和didEndContact内置函数。
在你的玩家与敌人联系后,你可以调用一个名为colorCheck()的函数来检查颜色是相同还是不同
我刚尝试使用MovingPole类(Pole的子类)和我的Player类。如果我有相同的颜色或不同的颜色,它的工作原理。
e.g。我在GameScene课程中的功能
func colorCheck(movingPole: MovingPole, player: Player) {
if movingPole.colorCurrent == player.colorCurrent {
print("no damage will occur")
} else {
print("damage to player will occur")
}
}
e.g。在我的didBeginContact(Swift 3)
中@objc(didBeginContact:) func didBeginContact(contact: SKPhysicsContact) {
let firstBody: SKPhysicsBody
let secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if ( firstBody.categoryBitMask == BodyType.Player.rawValue && secondBody.categoryBitMask == BodyType.Pole.rawValue){
if let somePlayer:Player = firstBody.node as? Player{
if (somePlayer == thePlayer){
if let theMovingPole = secondBody.node as? MovingPole {
colorCheck(movingPole: theMovingPole, player: somePlayer)
}
}
}
}
}
这是为什么?它与self.color的工作方式有关,它似乎与Spritekit用来分配新颜色的方式分开。
我们怎么知道? 好吧,如果我这样做,这类似于你在Swift 3中的代码,看看我的打印语句,它给出了答案。在课堂上自己尝试
print("Our initial color for self.color is \(self.color)")
// Red Color
self.color = UIColor.red()
self.currentColor = UIColor.red()
print("Red color")
print("self.color = \(self.color)")
print("self.currentColor = \(self.currentColor)")
// Green Color
self.color = UIColor.green()
self.currentColor = UIColor.green()
print("Green color")
print("self.color = \(self.color)")
print("self.currentColor = \(self.currentColor)")
// Color 1
self.currentColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0)
self.run(SKAction.sequence([SKAction.colorize(with: UIColor.black(), colorBlendFactor: 1.0, duration: 0.1), SKAction.colorize(with: currentColor, colorBlendFactor: 1.0, duration: 0.1)]))
print("Color 1 color")
print("self.color = \(self.color)")
print("self.currentColor = \(self.currentColor)")
// Something different
self.currentColor = UIColor(red: 0.4, green: 0.5, blue: 0.1, alpha: 1.0)
self.run(SKAction.sequence([SKAction.colorize(with: UIColor.black(), colorBlendFactor: 1.0, duration: 0.1), SKAction.colorize(with: currentColor, colorBlendFactor: 1.0, duration: 0.1)]))
print("A different color")
print("self.color = \(self.color)")
print("self.currentColor = \(self.currentColor)")
// Same as Color 1
self.currentColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0)
self.run(SKAction.sequence([SKAction.colorize(with: UIColor.black(), colorBlendFactor: 1.0, duration: 0.1), SKAction.colorize(with: currentColor, colorBlendFactor: 1.0, duration: 0.1)]))
print("Color 1 color")
print("self.color = \(self.color)")
print("self.currentColor = \(self.currentColor)")
如果你看一下打印控制台,你会看到这个
Our initial color for self.color is UIExtendedSRGBColorSpace 1 0.149131 0 1
Red color
self.color = UIExtendedSRGBColorSpace 1 0 0 1
self.currentColor = UIExtendedSRGBColorSpace 1 0 0 1
Green color
self.color = UIExtendedSRGBColorSpace 0 1 0 1
self.currentColor = UIExtendedSRGBColorSpace 0 1 0 1
Color 1 color
self.color = UIExtendedSRGBColorSpace 0 1 0 1
self.currentColor = UIExtendedSRGBColorSpace 0.2 0.2 0.2 1
A different color
self.color = UIExtendedSRGBColorSpace 0 1 0 1
self.currentColor = UIExtendedSRGBColorSpace 0.4 0.5 0.1 1
Color 1 color
self.color = UIExtendedSRGBColorSpace 0 1 0 1
self.currentColor = UIExtendedSRGBColorSpace 0.2 0.2 0.2 1