Skip to content

Commit d4529ce

Browse files
committed
fix: 结束时自动清空全部剩余小球
1 parent 5dd1aae commit d4529ce

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

main.gd

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ func _ready():
2020
# 生成一个随机位置的球
2121
func spawn_ball():
2222
var ball = ball_scene.instantiate()
23+
ball.add_to_group("active_balls")
2324
add_child(ball)
24-
2525
# 设置随机位置
2626
var screen_size = get_viewport().size
2727
var random_pos = Vector2(
@@ -79,7 +79,17 @@ func game_over():
7979
game_active = false
8080
suction_level = 0
8181
split_level = 0
82+
$GameOver.play()
8283
$Timer.stop()
84+
var balls = get_tree().get_nodes_in_group("active_balls")
85+
86+
# 批量删除
87+
for ball in balls:
88+
ball.queue_free() # 安全删除节点
89+
# 可选:立即强制释放内存
90+
Engine.get_main_loop().process_frame # 等待一帧
91+
RenderingServer.force_draw() # 强制渲染刷新
92+
8393
$CanvasLayer/GameOverLabel.visible = true
8494
$CanvasLayer/RestartButton.visible = true
8595

main.tscn

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
[gd_scene load_steps=7 format=3 uid="uid://dn5j2h175sk1p"]
1+
[gd_scene load_steps=8 format=3 uid="uid://dn5j2h175sk1p"]
22

33
[ext_resource type="Script" path="res://main.gd" id="1_rstm7"]
44
[ext_resource type="Texture2D" uid="uid://dwdx4rlggcx7d" path="res://assets/background.jpg" id="2_w3hb6"]
55
[ext_resource type="AudioStream" uid="uid://b1g1mxc7vpf1f" path="res://assets/sounds/drop.mp3" id="3_u1754"]
6+
[ext_resource type="AudioStream" uid="uid://dxfygh5sbawso" path="res://assets/sounds/ding.mp3" id="4_k32rp"]
67

78
[sub_resource type="LabelSettings" id="LabelSettings_nl362"]
89
font_size = 32
@@ -68,6 +69,7 @@ attenuation = 0.757858
6869
max_polyphony = 10
6970

7071
[node name="GameOver" type="AudioStreamPlayer2D" parent="."]
72+
stream = ExtResource("4_k32rp")
7173

7274
[connection signal="pressed" from="CanvasLayer/RestartButton" to="." method="_on_restart_button_pressed"]
7375
[connection signal="timeout" from="Timer" to="." method="_on_timer_timeout"]

0 commit comments

Comments
 (0)