Why this simple mini pong game isn't working?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Shlopynoobnoob

I put the 2d circle in an area2d node, and I assigned to it this script:

extends Area2D

var screen_size
var direction = Vector2(1.0, 0.0)
var ball_speed = 89

func _ready():
    screen_size = get_viewport_rect().size

func _process(delta):
	var ball_pos = .get_transform().get_origin()
	ball_pos += direction * ball_speed * delta
	if ((ball_pos.x < 0 and direction.x < 0) or (ball_pos.x > screen_size.x and direction.x > 0)):
		direction.x = -direction.x

but it’s not working, why?

:bust_in_silhouette: Reply From: estebanmolca

Use position:
position += direction * ball_speed * delta

I can change the origin of any object but that does not mean that it moves

also:

 if ((position.x < 0 and direction.x < 0) or (position.x > screen_size.x and direction.x > 0)):
        
direction.x = -direction.x

If the ball goes too fast the edge detection may fail, for that use:

if position.x> screen_size.x:
     position.x = screen_size.x 
     direction.x = -direction.x

estebanmolca | 2019-12-24 17:52

thanks a lot, that worked perfectly

Shlopynoobnoob | 2019-12-24 17:52

that didn’t work, the ball keeps respawning in one direction but I tried this:

if (position.x < 0  or position.x > screen_size.x):
		direction.x = -direction.x

Shlopynoobnoob | 2019-12-24 17:59