How to show and hide a sprite?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MDordio
:warning: Old Version Published before Godot 3 was released.

I am making a quiz game and i want to show a sprite(“this_image”) that contains an image when the correct answer is pressed and then changes question.
what i am doing is this:

func _on_lbl_answer1_pressed():
    if(oi3 == correct_answer):
        correct_answer()
        this_image.show()
        Globals.get("array_pergunta").append(i+1)
    elif(oi3 != correct_answer):
        wrong_answer()
    i+=1
    generate_table()

func generate_table():
    if(i>=dict.size()):
         get_tree().change_scene('res://Scenes/Scoreboard_M.tscn')
     
    else:
         this_image.hide()
         var k = dict[keys2[i]]
         correct_answer = k["res_c"]
         lbl_question.set_text(k["question"])

    randomize()
    for i in range(keys.size()):
        var random_index = randi() % keys.size()
        var k1 = keys[i]
        var k2 = keys[random_index]
        keys[i] = k2
        keys[random_index] = k1       


    
    lbl_answer1.set_text(k[keys[0]])
    lbl_answer2.set_text(k[keys[1]])
    lbl_answer3.set_text(k[keys[2]])
    lbl_answer4.set_text(k[keys[3]])
    oi = lbl_answer1.get_text()
    oi1 = lbl_answer2.get_text()
    oi2 = lbl_answer3.get_text()
    oi3 = lbl_answer4.get_text()

The code above in fact shows the image when i press the correct button and then changes the question but the problem is the image stays visible and i would like to “turn it off” as soon as the question change.

Is there away to do this?

PS: if needed i will provide the rest of the code

You are showing the image in func _on_lbl_answer1_pressed() and calling generate_table() where it immediately gets hidden again. You will need a timer if you want to show it for a given amount of time before hiding it again. Alternately, you can wait for the user’s input for the next answer and hide it then.
I hope I’m understanding your intentions properly.

Michael Paul | 2017-12-15 01:08

:bust_in_silhouette: Reply From: Michael Paul

Sure. Just use

this_image.hide()

i did try that method before but it doesn´t work, i guess because the show and hide command both “go down” one right before the other the moment the question changes and the result is just “nothing happens”

even if i put the “this_image.hide()” on the function that generates the next question the problem remains, i guess it would need some kinda delay that shows the “this_image” and then change question.

MDordio | 2017-12-14 13:47

I thought there must be more to it than just missing the “hide()” statement :). It does work as advertised. It’s hard to say what is happening without seeing the rest of your code, but I’m guessing that you are hiding it and then re-showing it immediately again. Have you tried putting breakpoints in to test?

Michael Paul | 2017-12-14 14:34

i already edited the thread with the rest of the code, if that helps

MDordio | 2017-12-14 15:14