Hello.
I'm having a little issue that I don't know to resolve:
I'm doing a Pong clone, and I'm coding the section of the ball reseting in the center screen after the player misses it
In my testing, I did a small testing with the print command on my function that detects if the ball goes outside the screen, and it returns some text in the output window.

But the print command responds with two repeated inputs, not only one. One when I start the game, and the other when the ball really goes to the outside area.

And when I add the entire code (ball resetting and stopping in the center for some time), the ball just stays in the middle of the screen when the game is loaded, like it is loading my area2D body enter function right from the start.

I have no idea what is causing this.




It's hard to tell you much without being able to see the rest of your project. You might make sure that you haven't moved any of the children of your objects away from their parent.

Edit: I don't know if this helps any, but here's some code I made to test bouncing a ball around.

# game.gd
extends ColorRect


var vel := Vector2.ZERO
var ball:KinematicBody2D
var wall:StaticBody2D
var bound:Area2D
var timer:Timer


func _ready() -> void:
	randomize()

	wall = StaticBody2D.new()
	wall.name = 'Wall'

	var sh = RectangleShape2D.new()
	sh.extents = Vector2(500, 50)

	var par = [ Vector2(300, -50), Vector2(300, 650) ]
	for c in par:
		var cs = CollisionShape2D.new()
		cs.shape = sh
		cs.position = c
		wall.add_child(cs)

	add_child(wall)

	sh = RectangleShape2D.new()
	sh.extents = Vector2(50, 300)

	par = [ Vector2(-50, 300), Vector2(850, 300) ]
	bound = Area2D.new()
	for c in par:
		var cs = CollisionShape2D.new()
		cs.shape = sh
		cs.position = c
		bound.add_child(cs)

	bound.connect('body_entered', self, 'out_of_bounds')
	add_child(bound)

	ball = KinematicBody2D.new()
	ball.position = Vector2(400, 300)
	ball.name = 'Ball'

	var cs = CollisionShape2D.new()
	sh = RectangleShape2D.new()
	sh.extents = Vector2.ONE * 10
	cs.shape = sh
	ball.add_child(cs)

	var cr = ColorRect.new()
	cr.rect_size = Vector2.ONE * 20
	cr.rect_position = Vector2.ONE * -10
	ball.add_child(cr)

	add_child(ball)

	timer = Timer.new()
	timer.wait_time = 1
	timer.one_shot = true
	add_child(timer)
	timer.connect('timeout', self, 'on_timeout')

	vel = Vector2(-10, 0)


func _physics_process(_delta: float) -> void:
	var coll = ball.move_and_collide(vel)
	if coll:
		print('collision with %s' % [coll.collider.name])
		vel = vel.bounce(coll.normal.rotated((randf() - 0.5) / 5.0))


func out_of_bounds(body):
	print('%s went out of bounds' % [body.name])
	timer.start()


func on_timeout():
	if vel:
		vel = Vector2.ZERO
		ball.position = Vector2(400, 300)
		timer.start()
	else:
		vel = Vector2(10, 0).rotated(2 * PI * randf())

    You might think of having a bool to let the game know when the ball is in an "idle" state. Have a timer attached that starts anytime the ball exits (or ready) and when the timer expires, flip the bool.

    duane
    I accidentally discovered a solution to this: moving my left and right Area2D sections 1px away from the 2D areas that act as a collisor on the top and the bottom of the screen. I have no idea why this is an issue. Do you have any idea why? My function works 100% correctly if I do that

    Then I guess they are touching and triggering events.