animated sprite not playing

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By fodder123

Hello, I’m new to programming and also Godot. After finishing reading the Godot docs I wanted to try and make the game ‘whack-a-mole’. It has 2 scenes, main scene and a Node2D(The Mole) scene with a single animated sprite with 3 animations namely, idle(hole in the ground) mole pop up(mole popping up) and mole hit(mole getting hit and sliding back to the ground).Default animation is set to idle . It has 6 spots where moles would pop up. only 1 would be allowed to pop up at a given time. and that one mole should play from the default idle to mole pop up when playing the scene, but its not, every spot is stuck at the default idle. I can’t seem to figure out why.
My scene tree:
TextureRect #for background
Area2D # for detecting mouse click for hitting mole.
Node2D #instanced scene containing the mole animated sprite with 3 animation
CollisionShape2D # as required by Area2D
Area2D2 # Same as above, duplicated 6 times(6 moles)

Here’s the code:
extends Node2D
func _ready():

               func _process(delta):
                   var active_count
                   if active_count == 0:
	           var randn=randi()%6+1
	           match randn:
			$Area2D/Node2D/"mole pop up",false)
			$Area2D2/Node2D2/"mole pop up",false)
			$Area2D3/Node2D3/"mole pop uo",false)
			$Area2D4/Node2D4/"mole pop up",false)
			$Area2D5/Node2D5/"mole pop up",false)
			$Area2D6/Node2D6/"mole pop up",false)

I’ve tried searching google for similar problems and tried their solutions but I haven’t found any that would solve mine. I’m pretty sure this is just a simple mistake on my part, but any help would highly be appreciated. I apologize for my bad English.

:bust_in_silhouette: Reply From: exuin
var active_count
if active_count == 0:

You never assigned any value (0) to active_count, so its type is null by default. So active_count == 0 evaluates to false. And since the variable is initialized to null every time the function runs each frame, it will never evaluates to true.

You should declare the variable outside the _process function and assign it 0 so that the if statement will evaluate to true.

its now working. thanks!

fodder123 | 2020-10-04 04:22