0

我在 Godot 中创建了一个“加法心算计算器”,用户在点击“on_Button_pressed”按钮时会看到一个新问题,总和存储在“global var c”中,然后通过“Lineedit”访问输入并存储在“var d”中。但是,当传递“var c”以与“Lineedit”中的“var d”进行比较时,它传递了一个“Null”值。如何在两个函数之间传递值。

extends Panel

# global variables
var a = " "
var b = " "
var c = " "
var d = " "

func _ready():
    pass

#Generate Random number and store sum in "var c"    
func _on_Button_pressed():

    randomize()
    var a = floor (rand_range(1,100))
    var b = floor (rand_range(1,100))

    var c = a+b
    print (a)
    print (b)
    print ("c = "+ str(c))

    get_node("RichTextLabel").set_text(str(a)+"+"+str(b)+"=")



#User_input
func _on_LineEdit_text_entered( text ):
    d = get_node("LineEdit").text

#pass sum "var c" for comparison with user_input "var d" 

print(c)<-- NUll value being passed.


#Accesing value via node method
#   var e = get_node("Panel").get("c").to_float()<-- Error-"Panel node doesn't exist"
#   print(e)


#convert d to float 
    var f = d.to_float()
    print(f)

#Compare sum with user_input
#   if  f == c:
#       get_node("RichTextLabel").set_text("Right Answer")
#   else:
#       get_node("RichTextLabel").set_text("Wrong Answer")
4

1 回答 1

1

换线试试

var c = a + b

c = a + b

var c 声明了一个在函数范围内工作的新 var。var 关键字应该在代码的最外层声明中。

希望有帮助!

于 2019-01-03T14:18:40.857 回答