Sfoglia il codice sorgente

按钮缩小解决

181404010226 3 mesi fa
parent
commit
efe0d2d7a6
2 ha cambiato i file con 56 aggiunte e 39 eliminazioni
  1. 27 27
      assets/Scenes/GameLevel.scene
  2. 29 12
      assets/scripts/Animations/ButtonManager.ts

+ 27 - 27
assets/Scenes/GameLevel.scene

@@ -164,7 +164,7 @@
     "_prefab": null,
     "_lpos": {
       "__type__": "cc.Vec3",
-      "x": 360,
+      "x": 360.00000000000006,
       "y": 667,
       "z": 0
     },
@@ -373,7 +373,7 @@
     "__prefab": null,
     "_contentSize": {
       "__type__": "cc.Size",
-      "width": 1000,
+      "width": 1000.0000000000001,
       "height": 5
     },
     "_anchorPoint": {
@@ -740,7 +740,7 @@
     "__prefab": null,
     "_contentSize": {
       "__type__": "cc.Size",
-      "width": 720,
+      "width": 720.0000000000001,
       "height": 1334
     },
     "_anchorPoint": {
@@ -852,7 +852,7 @@
     "_prefab": null,
     "_lpos": {
       "__type__": "cc.Vec3",
-      "x": 0,
+      "x": -5.684341886080802e-14,
       "y": 0,
       "z": 0
     },
@@ -9773,7 +9773,7 @@
     "_prefab": null,
     "_lpos": {
       "__type__": "cc.Vec3",
-      "x": 0,
+      "x": -5.684341886080802e-14,
       "y": 0,
       "z": 0
     },
@@ -13426,7 +13426,7 @@
     "_prefab": null,
     "_lpos": {
       "__type__": "cc.Vec3",
-      "x": -310,
+      "x": -310.00000000000006,
       "y": 577.3409999999999,
       "z": 0
     },
@@ -13484,7 +13484,7 @@
     "_prefab": null,
     "_lpos": {
       "__type__": "cc.Vec3",
-      "x": 360,
+      "x": 360.00000000000006,
       "y": 667,
       "z": 0
     },
@@ -13614,7 +13614,7 @@
     "__prefab": null,
     "_contentSize": {
       "__type__": "cc.Size",
-      "width": 720,
+      "width": 720.0000000000001,
       "height": 1334
     },
     "_anchorPoint": {
@@ -18366,7 +18366,7 @@
     "__prefab": null,
     "_contentSize": {
       "__type__": "cc.Size",
-      "width": 720,
+      "width": 720.0000000000001,
       "height": 1334
     },
     "_anchorPoint": {
@@ -18806,7 +18806,7 @@
     "_prefab": null,
     "_lpos": {
       "__type__": "cc.Vec3",
-      "x": 0,
+      "x": -5.684341886080802e-14,
       "y": 97.5,
       "z": 0
     },
@@ -20890,7 +20890,7 @@
     "__prefab": null,
     "_contentSize": {
       "__type__": "cc.Size",
-      "width": 720,
+      "width": 720.0000000000001,
       "height": 1334
     },
     "_anchorPoint": {
@@ -21028,7 +21028,7 @@
     "_prefab": null,
     "_lpos": {
       "__type__": "cc.Vec3",
-      "x": 0.14999999999997726,
+      "x": 0.1500000000000341,
       "y": -588.66,
       "z": 0
     },
@@ -21092,7 +21092,7 @@
     "_prefab": null,
     "_lpos": {
       "__type__": "cc.Vec3",
-      "x": -342.65,
+      "x": -342.6500000000001,
       "y": 0,
       "z": 0
     },
@@ -26480,7 +26480,7 @@
     "__prefab": null,
     "_contentSize": {
       "__type__": "cc.Size",
-      "width": 705.3,
+      "width": 705.3000000000002,
       "height": 130
     },
     "_anchorPoint": {
@@ -34686,7 +34686,7 @@
     "_prefab": null,
     "_lpos": {
       "__type__": "cc.Vec3",
-      "x": 1,
+      "x": 0.9999999999999432,
       "y": -6,
       "z": 0
     },
@@ -36424,7 +36424,7 @@
     "__prefab": null,
     "_contentSize": {
       "__type__": "cc.Size",
-      "width": 720,
+      "width": 720.0000000000001,
       "height": 1334
     },
     "_anchorPoint": {
@@ -36476,7 +36476,7 @@
     "__prefab": null,
     "_contentSize": {
       "__type__": "cc.Size",
-      "width": 720,
+      "width": 720.0000000000001,
       "height": 1334
     },
     "_anchorPoint": {
@@ -36597,7 +36597,7 @@
     "_prefab": null,
     "_lpos": {
       "__type__": "cc.Vec3",
-      "x": -216,
+      "x": -216.00000000000003,
       "y": 138.5,
       "z": 0
     },
@@ -38385,7 +38385,7 @@
     "_prefab": null,
     "_lpos": {
       "__type__": "cc.Vec3",
-      "x": 216,
+      "x": 216.00000000000003,
       "y": 138.5,
       "z": 0
     },
@@ -39688,7 +39688,7 @@
     "__prefab": null,
     "_contentSize": {
       "__type__": "cc.Size",
-      "width": 720,
+      "width": 720.0000000000001,
       "height": 469
     },
     "_anchorPoint": {
@@ -39830,7 +39830,7 @@
     "__prefab": null,
     "_contentSize": {
       "__type__": "cc.Size",
-      "width": 1000,
+      "width": 1000.0000000000001,
       "height": 5
     },
     "_anchorPoint": {
@@ -40204,7 +40204,7 @@
     "_prefab": null,
     "_lpos": {
       "__type__": "cc.Vec3",
-      "x": 1604.706,
+      "x": 1604.7060000000001,
       "y": 0,
       "z": 0
     },
@@ -40367,7 +40367,7 @@
     "_prefab": null,
     "_lpos": {
       "__type__": "cc.Vec3",
-      "x": 257,
+      "x": 257.0000000000002,
       "y": 175.274,
       "z": 0
     },
@@ -44235,7 +44235,7 @@
     "__prefab": null,
     "_contentSize": {
       "__type__": "cc.Size",
-      "width": 609,
+      "width": 609.0000000000005,
       "height": 428
     },
     "_anchorPoint": {
@@ -44417,7 +44417,7 @@
     "_prefab": null,
     "_lpos": {
       "__type__": "cc.Vec3",
-      "x": 281.9,
+      "x": 281.90000000000003,
       "y": 553.12,
       "z": 0
     },
@@ -44614,7 +44614,7 @@
     "_prefab": null,
     "_lpos": {
       "__type__": "cc.Vec3",
-      "x": 281.5,
+      "x": 281.50000000000006,
       "y": 485.124,
       "z": 0
     },
@@ -45737,7 +45737,7 @@
     "__prefab": null,
     "_contentSize": {
       "__type__": "cc.Size",
-      "width": 720,
+      "width": 720.0000000000001,
       "height": 1334
     },
     "_anchorPoint": {

+ 29 - 12
assets/scripts/Animations/ButtonManager.ts

@@ -8,6 +8,9 @@ export class ButtonManager extends Component {
     @property({ type: [Button], displayName: "按钮数组" })
     public buttons: Button[] = [];
     
+    // 存储每个按钮的原始缩放值
+    private buttonOriginalScales: Map<Node, Vec3> = new Map();
+    
     // 动画参数
     private readonly SCALE_REDUCTION = 0.15; // 缩放减少量
     private readonly ANIMATION_DURATION = 0.1; // 动画持续时间
@@ -38,12 +41,13 @@ export class ButtonManager extends Component {
     private initButtonEvents() {
         this.buttons.forEach((button, index) => {
             if (button && button.node) {
+                // 存储按钮的原始缩放值
+                this.buttonOriginalScales.set(button.node, button.node.scale.clone());
+                
                 // 添加点击事件监听
                 button.node.on(Button.EventType.CLICK, () => {
                     this.playClickAnimation(button.node);
                 }, this);
-                
-                // 不强制设置初始缩放,保持按钮原有缩放
             }
         });
     }
@@ -58,18 +62,26 @@ export class ButtonManager extends Component {
         // 停止之前的动画
         Tween.stopAllByTarget(buttonNode);
         
-        // 获取当前缩放值
-        const currentScale = buttonNode.scale;
-        const originalScale = currentScale.clone();
+        // 获取按钮的原始缩放值
+        const originalScale = this.buttonOriginalScales.get(buttonNode);
+        if (!originalScale) {
+            // 如果没有存储原始缩放值,则使用当前缩放值作为原始值
+            const currentScale = buttonNode.scale.clone();
+            this.buttonOriginalScales.set(buttonNode, currentScale);
+            return this.playClickAnimation(buttonNode);
+        }
         
-        // 计算目标缩放值(当前缩放减去0.15)
+        // 先恢复到原始缩放,确保动画基准正确
+        buttonNode.scale = originalScale.clone();
+        
+        // 计算目标缩放值(基于原始缩放减去0.15)
         const targetScale = new Vec3(
-            Math.max(0.1, currentScale.x - this.SCALE_REDUCTION), // 最小缩放限制为0.1
-            Math.max(0.1, currentScale.y - this.SCALE_REDUCTION),
-            currentScale.z
+            Math.max(0.1, originalScale.x - this.SCALE_REDUCTION), // 最小缩放限制为0.1
+            Math.max(0.1, originalScale.y - this.SCALE_REDUCTION),
+            originalScale.z
         );
         
-        // 创建缩放动画:先缩小,然后回到原大小
+        // 创建缩放动画:先缩小,然后回到原大小
         tween(buttonNode)
             .to(this.ANIMATION_DURATION, { scale: targetScale })
             .to(this.ANIMATION_DURATION, { scale: originalScale })
@@ -86,11 +98,12 @@ export class ButtonManager extends Component {
             
             // 为新添加的按钮设置事件
             if (button.node) {
+                // 存储按钮的原始缩放值
+                this.buttonOriginalScales.set(button.node, button.node.scale.clone());
+                
                 button.node.on(Button.EventType.CLICK, () => {
                     this.playClickAnimation(button.node);
                 }, this);
-                
-                // 不强制设置缩放,保持按钮原有缩放
             }
         }
     }
@@ -105,6 +118,8 @@ export class ButtonManager extends Component {
             // 移除事件监听
             if (button.node) {
                 button.node.off(Button.EventType.CLICK, this.playClickAnimation, this);
+                // 清除存储的原始缩放值
+                this.buttonOriginalScales.delete(button.node);
             }
             
             this.buttons.splice(index, 1);
@@ -121,6 +136,8 @@ export class ButtonManager extends Component {
             }
         });
         
+        // 清空原始缩放值存储
+        this.buttonOriginalScales.clear();
         this.buttons = [];
     }
 }