I need to change the image of the enemy SKSpriteNode when the player collides, but in the game I will have many enemies and I want change only the selected.
Enemy.name is the number of lives, i don't now if a can do this differently
func setupPlayer(){
player = SKSpriteNode(imageNamed: "ball")
player.anchorPoint = CGPoint(x: 0.5, y: 0.5)
player.position = CGPoint(x: size.width/2, y: 60)
player.zPosition = Layer.Player.rawValue
player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.width / 2)
player.physicsBody?.mass = 1
player.physicsBody?.restitution = 0.7
player.physicsBody?.dynamic = true
player.physicsBody?.categoryBitMask = playerCategory
player.physicsBody?.contactTestBitMask = enemyCategory
worlNode.addChild(player)
nuevoPlayerControl = true
}
func setupEnemy(){
if nuevoPlayerControl == true && nuevoPlayer == true{
enemy = SKSpriteNode(imageNamed: "ball")
enemy.anchorPoint = CGPoint(x: 0.5, y: 0.5)
enemy.zPosition = Layer.Player.rawValue
enemy.position = CGPoint(x: 250, y: 300)
enemy.physicsBody?.mass = 1
enemy.zRotation = player.zRotation
enemy.size = player.size
enemy.physicsBody = SKPhysicsBody(circleOfRadius: enemy.size.width/2)
enemy.physicsBody?.friction = 1
enemy.physicsBody?.dynamic = true
enemy.physicsBody?.categoryBitMask = enemyCategory
enemy.physicsBody?.contactTestBitMask = playerCategory
enemy.name = "3"
worlNode.addChild(enemy)
}
}
func didBeginContact(contact: SKPhysicsContact) {
var body1 : SKPhysicsBody!
var body2 : SKPhysicsBody!
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
body1 = contact.bodyA
body2 = contact.bodyB
}
else {
body1 = contact.bodyB
body2 = contact.bodyA
}
if body1.categoryBitMask == playerCategory && body2.categoryBitMask == enemyCategory {
//here the enemy lose a live and the image should change
let lives = body2.node?.name
if lives! == "3" {
body2.node?.name = "2"
}
if lives! == "2" {
body2.node?.name = "1"
}
if lives! == "1" {
body2.node?.removeFromParent()
}
print(lives)
}
}
Aucun commentaire:
Enregistrer un commentaire