Hello, I come to you because I am in front of a problem that I do not understand. I would like to perform an action of a "player" if one clicks on the left and do another action on one clicks on the right. But when I click once on the right and once on the left, the two actions are done continuously without stopping (a left shot and a right shot), I do not understand why while I stop the actions at the beginning of the function
thank you
override func touchesMoved(_ touches: Set, with event: UIEvent?) {
for t in touches { self.touchMoved(toPoint: t.location(in: self)) }
ChangementTexture()
}
func ChangementTexture () {
let Frontiere11 = self.Vueprincipale.childNode(withName: "Frontiere1") as? SKSpriteNode
if PositionDoigt.x<player1.position.x && PositionDoigt.y<Frontiere11!.frame.minY {
GestionAnimationPlayer1(Direction: "Gauche")
print("Gauche")
}
if PositionDoigt.x>player1.position.x && PositionDoigt.y<Frontiere11!.frame.minY {
GestionAnimationPlayer1(Direction: "Droite")
print("Droite")
}
}
func GestionAnimationPlayer1(Direction:String) {
//Suppression de tous les child de player
if let child = self.Vueprincipale.childNode(withName: "player1") as? SKSpriteNode {
positionDuPlayer = child.position
child.removeFromParent()
child.removeAllChildren()
child.removeAllActions()
child.removeAction(forKey: "MarcherGaucheAction1")
child.removeAction(forKey: "MarcherDroiteAction1")
}
//Texture
for i in 1...4 {
let textureName = "Marcher\(Direction)\(i)"
TexturePlayer1.append(SKTexture(imageNamed: textureName))
}
/*if (TexturePlayer1.count>1) {
player1 = SKSpriteNode(imageNamed: "Marcher\(Direction)1")
}*/
let action1 = (SKAction.repeatForever(SKAction.animate(with: TexturePlayer1, timePerFrame: 0.2)))
player1.name = "player1"
player1.run(action1, withKey: "Marcher\(Direction)Action1")
Vueprincipale.addChild(player1)
if let child = self.Vueprincipale.childNode(withName: "player1") as? SKSpriteNode {
child.position = positionDuPlayer
}
//Calcul du temps de placement du player pour que ce soit lineaire
let calculPythagorePourBougerPlayer1Lineairement = sqrt(abs(PositionDoigt.x-player1.position.x)+abs(PositionDoigt.y-player1.position.y))
let calculPythagorePourBougerPlayer1Lineairement1 = sqrt((view!.frame.width)+(view!.frame.height))
var calculPourTempsPlayer = 2*((calculPythagorePourBougerPlayer1Lineairement)/calculPythagorePourBougerPlayer1Lineairement1)
if calculPourTempsPlayer<0 { calculPourTempsPlayer = calculPourTempsPlayer*(-1) }
print("\(calculPourTempsPlayer)")
let tempsDeDeplacementDuPlayer:TimeInterval = TimeInterval(calculPourTempsPlayer)
var actionArray = [SKAction]()
actionArray.append(SKAction.move(to: PositionDoigt, duration: tempsDeDeplacementDuPlayer))
player1.run(SKAction.sequence(actionArray))
self.run(SKAction.wait(forDuration: tempsDeDeplacementDuPlayer)) {
if let child = self.Vueprincipale.childNode(withName: "player1") as? SKSpriteNode {
child.removeAllActions()
child.removeAction(forKey: "MarcherGaucheAction1")
child.removeAction(forKey: "MarcherDroiteAction1")
self.player1.texture = SKTexture(imageNamed:"Repos1")
}
}
}