So in my clicker game a sprite 2d should become visible when the apple = 5. and idk what to do in terms of code to trigger that, and i tried a couple of things but I’m completely lost. here’s my scene code:
var money = 0
var apple = 0
var bread = 0
var milk = 0
var knife = 0
var pepperspray = 0
func _process(_delta: float) -> void:
$money_label.text = "Money: " + str(money)
$Label.text = "Apples: " + str(apple)
$Label2.text = "Bread: " + str(bread)
$Label3.text = "Milk: " + str(milk)
$Label4.text = "Pocket Knife (optional): " + str(knife)
$Label5.text = "Pepper Spray (optional): " + str(pepperspray)
func _on_wallet_pressed() -> void:
money += 1
func _on_apple_pressed() -> void:
if money >= 5 and apple < 5:
money -= 5
apple += 1
this should not be in process, use signals and a method for setting each item.
when an event happens that would up (for example) apple, there you set apple’s text after adding 1.
process runs every frame, you don’t need to update everything every frame.
it is a good idea to set variable types, as that will help you in debugging and extending your game and will also make it faster (over 10% faster)
var money : int = 0
when you click you test for things and if the conditions are met, you can set a sprite to visible = true
func _on_wallet_pressed() -> void:
money += 1
$money_label.text = "Money: " + str(money)
if money >= 5:
money -= 5
apple += 1
if apple == 5:
$apple_sprite.visible = true
a better way would be to use a tween to spawn and then delete and object, giving it an animation
you start by instantiating a scene, then create a tween that will be tied to the object.
in the tween you can define a series of changes to the properties. for example, you can move the sprite to the right over 0.5 seconds.
you can then chain more events that will play one after the last finishes.
finally, use tween_callback queue_free to delete the node.
var my_node : Sprite2D = my_packed_scene.instantiate() as Sprite2D
if my_node:
add_child(my_node)
my_node.position = Vector2(128.0, 128.0)
var tween : Tween = my_node.create_tween()
tween.tween_property(my_node, "position", Vector2(512.0, 0.0), 0.5)
tween.tween_property(my_node, "position", Vector2(0.0, 512.0), 0.5)#move up
tween.tween_callback(my_node.queue_free)
Oh ok thank you this is super helpful! The only problem is that the sprite is visible before any of these conditions are met and when i tried to set a precedent of them being invisible they don’t appear at all, even when all the conditions are met.