Godot Version
4
Question
i was making a flappy bird clone and i finished the base game and i wanted to add a score counter that uses numbered images. it works at first, but when i enter a game over? and i try to restart the game by clicking the screen, the texture rect node that is a child of my main node disappears ive seen it disappear when i look at the remote tab of my scene. this also causes alot of errors. so sorry if my formatting is bad, this is my first question posted here
extends Node2D
var obstacle_pair = preload("res://scene/obstacle_pair.tscn")
var game_over_scene = preload("res://scene/game_over_state.tscn")
var screen_size
var score = 0
var container
var game_active
var game_over_state
func _ready():
screen_size = get_viewport_rect().size
container = $ScoreCounter
container.position.x = screen_size.x / 2
new_game()
func _input(event):
if game_over_state == false:
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
if game_active == false:
begin_game()
else:
if $Plane.flying_state:
$Plane.flap()
if game_over_state == true:
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
new_game()
container = $ScoreCounter
add_child(container)
get_tree().call_group('rocks','queue_free')
func _process(delta):
if game_active == false:
$ParallaxBackground.scroll_speed = 0
if game_active == true:
$ParallaxBackground.scroll_speed = 100
#clean the current displayed score
for number in container.get_children():
number.queue_free()
var score_as_string:String = str(score)
for character in score_as_string: #this will parse the string from left to right
var texture_rect:TextureRect = TextureRect.new()
texture_rect.texture = load("res://assets/Tappy Plane/PNG/Numbers/number" + character + ".png")
container.add_child(texture_rect)
func spawnObstaclePair():
var rocks = obstacle_pair.instantiate()
#spawn in a random location on the y axis
var randomY = randf_range(150, 400)
$ParallaxBackground/ParallaxLayer2.add_sibling(rocks)
rocks.position = Vector2(screen_size.x + 40, randomY)
rocks.hit.connect(bird_hit)
rocks.scored.connect(points_scored)
func game_over():
game_active = false
game_over_state = true
$Plane.flying_state = false
$Plane.falling_state = true
get_tree().call_group('rocks','stop_rock_movement')
var game_over_screen = game_over_scene.instantiate()
add_child(game_over_screen)
func new_game():
container = $ScoreCounter
container.position.x = screen_size.x / 2
game_active = false
game_over_state = false
score = 0
$Plane.reset()
func begin_game():
game_active = true
$Plane.flying_state = true
$Plane.flap()
func _on_timer_timeout():
spawnObstaclePair()
func points_scored():
score += 1
func bird_hit():
print('hit')
$Timer.stop()
game_over()