From 4daa6239de3c3e2e963da6319ec49445c317d31b Mon Sep 17 00:00:00 2001 From: Nathan Date: Thu, 30 May 2024 17:11:17 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Update:=20touchesEnded?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Scene/SpriteMeeple.swift | 32 +++++++------------ 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/DouShouQi_App/DouShouQi_App/Components/Scene/SpriteMeeple.swift b/DouShouQi_App/DouShouQi_App/Components/Scene/SpriteMeeple.swift index 23463f6..aeaaaa3 100644 --- a/DouShouQi_App/DouShouQi_App/Components/Scene/SpriteMeeple.swift +++ b/DouShouQi_App/DouShouQi_App/Components/Scene/SpriteMeeple.swift @@ -58,29 +58,19 @@ class SpriteMeeple : SKNode { } override func touchesEnded(_ touches: Set, with event: UIEvent?) { - var x: CGFloat = 0 - var y: CGFloat = 0 - let position = touches.first?.location(in: parent!) ?? CGPoint(x: 0, y: 0) - if (position.x < -400){ - x = -400 - } - else if (position.x > 500){ - x = 500 - } - else{ - x = (abs(x.truncatingRemainder(dividingBy: 100)) < 50) ? floor(position.x/100) * 100 : ceil(position.x/100) * 100 - } - if (position.y < -300){ - y = -300 - } - else if (position.y > 400){ - y = 400 - } - else{ - y = (abs(y.truncatingRemainder(dividingBy: 100)) < 50) ? floor(position.y/100) * 100 : ceil(position.y/100) * 100 + if let parent = parent, let position = touches.first?.location(in: parent) { + // Arrondir à la case la plus proche pour x et y + let x = round(position.x / 100) * 100 + let y = round(position.y / 100) * 100 + + // Assurer que les coordonnées sont dans les limites + let clampedX = min(max(x, -300), 300) + let clampedY = min(max(y, -400), 400) + + self.position = CGPoint(x: clampedX, y: clampedY) } - self.position = CGPoint(x: x, y: y) } + override func touchesMoved(_ touches: Set, with event: UIEvent?){ self.position = touches.first?.location(in: parent!) ?? CGPoint(x: 0, y: 0)