Why i cant stop my ball?

Godot Version

3

I cant stop it after death.

extends KinematicBody2D

var speed = 750
var increase_amount = 10
var rotation_speed = 2 # Adjust this value for rotation speed
var velocity = Vector2()
var collided = false
var player_dead = false # Add this variable

signal score_increment

func _ready():
	randomize()
	# Start a coroutine to handle the delay before the ball starts moving
	start_countdown()

func start(pos):
	position = pos
	# Ensure the ball starts moving at an angle not too close to horizontal
	var min_angle = deg2rad(45)
	var max_angle = deg2rad(180)
	var initial_angle = rand_range(min_angle, max_angle)
	
	velocity = Vector2(speed, 0).rotated(initial_angle)
	
	# Check if velocity is too horizontal and reassign if necessary
	while abs(velocity.angle()) < min_angle or abs(velocity.angle()) > max_angle:
		initial_angle = rand_range(min_angle, max_angle)
		velocity = Vector2(speed, 0).rotated(initial_angle)
	
	collided = false

func start_countdown():
	# Wait for 3 seconds
	yield(get_tree().create_timer(3), "timeout")
	# After the wait, start the ball movement
	start(position)

func _physics_process(delta):
	if player_dead:
		# Stop the ball by setting velocity to zero
		velocity = Vector2()
	else:
		if velocity != Vector2():  # Check if velocity is initialized
			# Move the ball and check for collision
			var collision = move_and_collide(velocity * delta)
			if collision:
				# Reflect the velocity based on the collision normal
				velocity = velocity.bounce(collision.normal)
				if collision.collider.is_in_group("Paws") and not collided:
					print("Collided with Paw")
					emit_signal("score_increment")
					speed += increase_amount
					# Maintain direction but increase speed
					velocity = velocity.normalized() * speed
					collided = true
					print(speed)
			else:
				collided = false

			# Apply rotation
			rotation += rotation_speed * delta

		else:
			print("Velocity not initialized")

# Call this function to initialize the ball with an initial position
func init_ball(start_pos: Vector2):
	start(start_pos)

# Call this function to stop the ball when the player dies
func set_player_dead(is_dead: bool):
	player_dead = is_dead

where do you call set_player_dead?

I think nowhere

Thats the problem

1 Like

Okay i need to call set_player_dead? when my ball collides with death area?

Yes you have to call “set_player_dead(true)” when you want it to stop

1 Like
extends KinematicBody2D

var speed = 750
var increase_amount = 10
var rotation_speed = 2 # Adjust this value for rotation speed
var velocity = Vector2()
var collided = false
var player_dead = false # Add this variable

signal score_increment

func _ready():
	randomize()
	area.connect("player_death", self, "_on_player_death")
	# Start a coroutine to handle the delay before the ball starts moving
	start_countdown()

func start(pos):
	position = pos
	# Ensure the ball starts moving at an angle not too close to horizontal
	var min_angle = deg2rad(45)
	var max_angle = deg2rad(180)
	var initial_angle = rand_range(min_angle, max_angle)
	
	velocity = Vector2(speed, 0).rotated(initial_angle)
	
	# Check if velocity is too horizontal and reassign if necessary
	while abs(velocity.angle()) < min_angle or abs(velocity.angle()) > max_angle:
		initial_angle = rand_range(min_angle, max_angle)
		velocity = Vector2(speed, 0).rotated(initial_angle)
	
	collided = false

func start_countdown():
	# Wait for 3 seconds
	yield(get_tree().create_timer(3), "timeout")
	# After the wait, start the ball movement
	start(position)

func _physics_process(delta):
	if player_dead:
		# Stop the ball by setting velocity to zero
		velocity = Vector2()
	else:
		if velocity != Vector2():  # Check if velocity is initialized
			# Move the ball and check for collision
			var collision = move_and_collide(velocity * delta)
			if collision:
				# Reflect the velocity based on the collision normal
				velocity = velocity.bounce(collision.normal)
				if collision.collider.is_in_group("Paws") and not collided:
					print("Collided with Paw")
					emit_signal("score_increment")
					speed += increase_amount
					# Maintain direction but increase speed
					velocity = velocity.normalized() * speed
					collided = true
					print(speed)
			else:
				collided = false

			# Apply rotation
			rotation += rotation_speed * delta

		else:
			print("Velocity not initialized")

# Call this function to initialize the ball with an initial position
func init_ball(start_pos: Vector2):
	start(start_pos)

# Call this function to stop the ball when the player dies
func _on_player_death(is_dead: bool):
	player_dead = is_dead
extends Area2D

signal death
signal player_death

func _ready():
	connect("body_entered", self, "_on_Area2D_body_entered")

func _on_Area2D_body_entered(body):
	print("Body entered:", body.name)
	if body.is_in_group("Ball"):
		print("deleted")
		emit_signal("death")
		emit_signal("player_death")
		
		

I tried to connect my signal to call that function but i am getting an error


or maybe there is an another easy method ?

i think its easier to change the death-zone-method:

func _on_Area2D_body_entered(body):
	print("Body entered:", body.name)
	if body.is_in_group("Ball"):
		print("deleted")
		emit_signal("death")
		emit_signal("player_death")
        body.set_player_dead(true)

and dont change anything in ball script ?

it should work without changes in ball-script

1 Like

I deleted area.connect("player_death", self, "_on_player_death") this from my ball script because it was the first error and then after it there is this.

the method-name changed the second time you sent your code:

func _on_Area2D_body_entered(body):
	print("Body entered:", body.name)
	if body.is_in_group("Ball"):
		print("deleted")
		emit_signal("death")
		emit_signal("player_death")
        body._on_player_death(true)
1 Like

Ah really, i cant do this for hours and you just in 5 minutes solves it :joy:.Just genius,thanks a lot :+1:

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.