#spring doesn`t work

6 messages · Page 1 of 1 (latest)

fossil crag
#

#Springcode

extends StaticBody2D

func _ready():
add_to_group("Spring") # Add the object to the "Spring" group
$CollisionShape2D.disabled = false # Enable the collider

# Ensure that $Area2D exists correctly
if $Area2D != null:
    $Area2D.connect("area_entered", Callable(self, "_on_area_2d_area_entered"))  # Connect the area_entered signal to the function

# Connect the timer signal to the function so that the spring can return to its original state
$Timer.connect("timeout", Callable(self, "_on_timer_timeout"))  

Function to activate the spring

func activate_spring():
$AnimatedSprite2D.play("bounce") # Play the "bounce" animation for the active spring
$Sound.play() # Play the sound effect
$Timer.start() # Start the timer to reset the spring

Handling collision with the player or other objects

Corrected code

func _on_area_2d_body_entered(body: Node) -> void:
if body.is_in_group("Player"): # Check if the object is the player
print("Colliding with Spring")
if body.velocity.y > 0: # Check if the player is falling onto the spring
activate_spring() # Activate the spring

The timer resets the spring to the inactive state

func _on_timer_timeout():
$AnimatedSprite2D.play("idle") # Set the animation back to the "idle" state

#

#Playercode

extends CharacterBody2D

Speed and jump parameters

var speed: float = 200.0
var max_jump_velocity: float = -440.0
var min_jump_velocity: float = -100.0
var gravity: float = ProjectSettings.get_setting("physics/2d/default_gravity") * 1.2
var jump_time: float = 0.2
var acceleration_ground: float = 800.0
var deceleration_ground: float = 600.0
var acceleration_air: float = 600.0
var deceleration_air: float = 400.0
var lives = 3
var hurt_timer: float = 0.0
var hurt_duration: float = 0.4
var boost_multiplier = 2
var gravity_delay_timer: float = 0.0
var gravity_delay: float = 0.2

Jump-related variables

var jump_timer: float = 0.0
var is_jumping: bool = false

Invincibility settings

var invincibility_timer: float = 0.0
var invincibility_duration: float = 1.5
var is_invincible: bool = false

Animation and sound management

@onready var jump_sound = $Sounds/Jump
@onready var hurt_sound = $Sounds/damage
@onready var game_rules = $"../GameRule"
@onready var sprite = $AnimatedSprite2D

Adding Area2D for collision detection

@onready var collision_area = $Area2D

var last_direction: int = 1
var coins = 0

#

func _on_collision_entered(body: Node) -> void:
if body.is_in_group("Coin"):
coins += 1
body.queue_free()
get_tree().call_group("GUI", "update_coins", coins)
elif body.is_in_group("Spring"):
var collision_normal = (body.global_position - global_position).normalized()
if collision_normal.y > 0:
body.activate_spring()
boost()
elif body.is_in_group("Enemy") and not is_invincible:
hurt()
elif body.is_in_group("Spike") and not is_invincible:
hurt()

func boost() -> void:
velocity.y = max_jump_velocity * boost_multiplier

#

so the spring collision that is Area2d doesn`t activate

#

with sound and animation

fossil crag
#

nevermind