Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Pacers |
Currently I am making a pong game and I’m having problems deleting the ball when it exits the screen.
CPU Paddle
func _physics_process(delta):
screensize = get_viewport_rect()
if (!BALL.position.y > screensize.end.y):
velocity = (BALL.position - position).normalized() * speed
var new = Vector2(velocity.x,0)
move_and_slide(new)
else:
velocity = 180
BALL.ball_delete()
var old = Vector2(velocity,0)
move_and_slide(old)
Ball scene
func _physics_process(delta):
var bodies = get_colliding_bodies()
for body in bodies:
if body.get_name() == "Player":
speed = linear_velocity.length()
direction = position - body.get_node("Anchor").get_global_position()
velocity = direction * min(speed+SPEEDUP*delta, MAXSPEED*delta)
linear_velocity = velocity
func ball_delete():
queue_free()
print(position)
pos = position
The initial if statement doesn’t seem to work as the velocity always returns a previously freed instance error. I know that the CPU position depends on the position of the ball so if I delete the ball there is a problem with the velocity of the CPU paddle and I was wondering if there is anyway I can delete the ball when it exits the screen
Whrn you delete the ball, you wanna knoe its positiob afterwards? Try to detect the pos before
Schweini | 2019-08-29 16:49
I would like to know the position of the ball before it exits the screen so the CPU paddle can track its position.
Pacers | 2019-08-29 18:26
Yeah. But you can’t detect the position when there’s no object cuz you deleted it
Schweini | 2019-08-29 18:33
So once I delete the ball I should spawn a new ball and somehow reset the position of the CPU paddle, but how should I approach this
Pacers | 2019-08-30 23:25
func delete_ball() : global. pos = position queue_free()
Something like this. global is a singleton: Singletons (AutoLoad) — Godot Engine (3.1) documentation in English
Schweini | 2019-08-31 04:32