I tried to create a selection screen to select characters (male or female) from a youtube video, I tried many variations because it didn’t work for me. I created a Character2d and made 2 animated sprite it’s child named ‘fem’ and ‘male’. The animation names are the same in both animated sprites. I really want that if you click on one character on the selection screen it would use only that animated sprite and ignore the other but I can’t make it happen. I would really appreciate if someone could help me out because I really don’t know where to continue. Here is the script for the Selection screen
extends Node2D
func _on_male_pressed():
var character = 'male'
get_tree().change_scene_to_file("res://Scenes/main.tscn")
func _on_fem_pressed():
var character = 'fem'
get_tree().change_scene_to_file("res://Scenes/main.tscn")
And here is the script for the player:
extends CharacterBody2D
var speed = 200
var health = 100
const jump_velocity = -400
var direction = Vector2()
var jetpack_fuel = 100
var points = 0
var male = true
var fem = false
var helm_on = false
@onready var sprite_2d = $Sprite2D
@onready var fuel_label = $Camera2D/jetpack_fuel
@onready var health_label = $Camera2D/main_health
@onready var health_bar = $Camera2D/health_bar
@onready var fuel_bar = $Camera2D/fuel_bar
@onready var character
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var animation = ''
func _ready():
health_bar.value = health
fuel_bar.value = jetpack_fuel
func damage(delta):
if Input.is_action_just_pressed('Fire'):
health -= 50 * delta
health_bar.value = health
#Function for moving
func _jetpacking(delta):
if Input.is_action_pressed("Jump") and jetpack_fuel > 0:
velocity.y = jump_velocity * 0.5
jetpack_fuel -= 10 * delta
animation = 'jetpacking'
else:
velocity.y += gravity * delta
if not is_on_floor():
animation = 'jumping'
if not is_on_floor():
speed = 250
fuel_bar.value = jetpack_fuel
func _walking_n_running():
direction = Input.get_axis("GoLeft", "GoRight")
if direction != 0:
velocity.x = direction * speed
if speed > 200 and is_on_floor():
animation = "running"
if speed <= 200 and is_on_floor():
animation = "walking"
else:
velocity.x = move_toward(velocity.x, 0, 16)
if is_on_floor():
animation = "idle_helm"
func _jump(delta):
if is_on_floor() and Input.is_action_just_pressed("Jump"):
velocity.y = jump_velocity
animation = 'jumping'
elif not is_on_floor():
_jetpacking(delta)
func _running():
if Input.is_action_pressed('Sprint') and is_on_floor():
speed = 280
else:
speed = 200
func _flip():
if Input.is_action_just_pressed('GoLeft'):
sprite_2d.flip_h = true
if Input.is_action_just_pressed('GoRight'):
sprite_2d.flip_h = false
#Function for moving end
func _male_or_fem():
if male == true:
male.animation = animation
else:
fem.animation = animation
func _physics_process(delta):
#movements
_jump(delta)
_walking_n_running()
_running()
_flip()
move_and_slide()
#labelsetc_
fuel_label.text = str(int(jetpack_fuel))
health_label.text = str(int(health))
_male_or_fem()
#healthchanges
damage(delta)
I have no wish to check all this code, there are a lot of videos about movement. I recommend stat with basics, only left-right, then add jump then add sprint, etc.
I had a working code for movements but now that there are 2 of the characters it’s not working. Maybe it’s bc I have 2 animated sprite instead of one, tbh idk