按钮单击戈多时场景无法加载



声音播放,但场景没有改变,我不知道该怎么办。我的游戏基本上是一个飞机商店,但场景没有改变。感谢任何帮助!这是我的代码:'

extends Control
onready var default_plane_button = $DefaultPlaneButton
onready var plane_1_button = $BuyPlane1
onready var plane_2_button = $BuyPlane2
onready var plane_3_button = $BuyPlane3
onready var plane_4_button = $BuyPlane4

func _on_BuyPlane1_button_up():
var read_dictionary = str2var(change_file().get_as_text())
var new_dictionary = read_dictionary
if new_dictionary["plane_1_bought"] == true:
new_dictionary["plane_equipped"] = 1
change_file().store_string(var2str(new_dictionary))
change_file().close()
plane_1_button.text = "Equipped"
elif new_dictionary["coins"] >= 1000:
global.button_click_sound.play()
new_dictionary["coins"] -= 1000
print(new_dictionary)
new_dictionary["plane_1_bought"] = true
change_file().store_string(var2str(new_dictionary))
change_file().close()
plane_1_button.text = "Equip"
else:
print("insufficient coins")
global.no_click_sound.play()

func _on_BuyPlane2_button_up():
var read_dictionary = str2var(change_file().get_as_text())
var new_dictionary = read_dictionary
if new_dictionary["plane_2_bought"] == true:
new_dictionary["plane_equipped"] = 2
change_file().store_string(var2str(new_dictionary))
change_file().close()
plane_2_button.text = "Equipped"
elif new_dictionary["coins"] >= 2000:
global.button_click_sound.play()
new_dictionary["coins"] -= 2000
new_dictionary["plane_2_bought"] = true
change_file().store_string(var2str(new_dictionary))
change_file().close()
plane_2_button.text = "Equip"
else:
print("insufficient coins")
global.no_click_sound.play()
func _on_BuyPlane3_button_up():
var read_dictionary = str2var(change_file().get_as_text())
var new_dictionary = read_dictionary
if new_dictionary["plane_3_bought"] == true:
new_dictionary["plane_equipped"] = 3
change_file().store_string(var2str(new_dictionary))
change_file().close()
plane_3_button.text = "Equipped"
elif new_dictionary["coins"] >= 3000:
global.button_click_sound.play()    
new_dictionary["coins"] -= 3000
new_dictionary["plane_3_bought"] = true
change_file().store_string(var2str(new_dictionary))
change_file().close()
plane_3_button.text = "Equip"
else:
print("insufficient coins")
global.no_click_sound.play()
func _on_BuyPlane4_button_up():
var read_dictionary = str2var(change_file().get_as_text())
var new_dictionary = read_dictionary
if new_dictionary["plane_4_bought"] == true:
new_dictionary["plane_equipped"] = 4
change_file().store_string(var2str(new_dictionary))
change_file().close()
plane_4_button.text = "Equipped"
elif new_dictionary["coins"] >= 4269:
global.button_click_sound.play()
new_dictionary["coins"] -= 4269
new_dictionary["plane_4_bought"] = true
change_file().store_string(var2str(new_dictionary))
change_file().close()
plane_4_button.text = "Equip"
else:
print("insufficient coins")
global.no_click_sound.play()
func _on_Back_button_up():
global.button_click_sound.play()
get_tree().change_scene("res://Assets/Room/Shop.tscn")
func _on_DefaultPlaneButton_button_up():
global.no_click_sound.play()
var read_dictionary = str2var(change_file().get_as_text())
var new_dictionary = read_dictionary
new_dictionary["plane_equipped"] = 0
change_file().store_string(var2str(new_dictionary))
change_file().close()
change_file().close()
default_plane_button.text = "Equipped"
func change_file():
var file = File.new()
var err = file.open(global.SAVE_FILE, File.READ_WRITE)
if err == OK:
return file
print("open file success")
else:
print("error opening file, error: ", err)
func _on_NextButton_button_up():
global.button_click_sound.play()
get_tree().change_scene("res://Assets/Room/ShopPlane2.tscn")

func _on_BackButton_pressed():
global.button_click_sound.play()
get_tree().change_scene("res://Assets/Room/ShopPlane1.tscn")

func _on_NextButton1_button_up():
global.button_click_sound.play()
get_tree().change_scene("res://Assets/Room/ShopPlane3.tscn")
func _on_NextButton23_button_up():
global.button_click_sound.play()
get_tree().change_scene("res://Assets/Room/ShopPlane.tscn")

func _on_BackButton232_button_up():
global.button_click_sound.play()
get_tree().change_scene("res://Assets/Room/ShopPlane.tscn")

func _on_BackButton321312_button_up():
global.button_click_sound.play()
get_tree().change_scene("res://Assets/Room/ShopPlane2.tscn")

func _on_NextButton23123_button_up():
global.button_click_sound.play()
get_tree().change_scene("res://Assets/Room/ShopPlane4.tscn")

func _on_BackButton2212412_button_up():
global.button_click_sound.play()
get_tree().change_scene("res://Assets/Room/ShopPlane3.tscn")

声音播放,但场景没有改变,我不知道该怎么办。我的游戏基本上是一个飞机商店,但场景没有改变。感谢任何帮助!

我编辑了我的答案。在某个地方,我发现了一种非常适合我的改变场景的方法。我没有参考资料了(如果有人有,请告诉我),但这是方法:

添加全局变量。使用以下代码自动加载(Project->Project settings-> autolload):

extends Node
var current_scene = null
func _ready():
var root = get_tree().root
current_scene = root.get_child(root.get_child_count() - 1)
func goto_scene(path, params=null):
# This function will usually be called from a signal 
# callback,
# or some other function in the current scene.
# Deleting the current scene at this point is
# a bad idea, because it may still be executing code.
# This will result in a crash or unexpected behavior.
# The solution is to defer the load to a later time, when
# we can be sure that no code from the current scene is 
# running:
call_deferred("_deferred_goto_scene", path)

func _deferred_goto_scene(path):
# It is now safe to remove the current scene
current_scene.free()
# Load the new scene.
var s = ResourceLoader.load(path)
# Instance the new scene.
current_scene = s.instance()
# Add it to the active scene, as child of root.
get_tree().root.add_child(current_scene)
# Optionally, to make it compatible with the 
SceneTree.change_scene() API.
get_tree().current_scene = current_scene

然后,当你想改变场景时,这样做:

Global.goto_scene("res://path/yourscene.tscn")?

最新更新