![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | eviking |
So I have two scripts making up a simple brick breaker game. The idea is that when the ball exits the screen, the counter gets “reset” back to 1, and I should be able to do a left mouse button click after that, and it should spawn a new ball. I had it working previously where I would spawn several balls at once before any of them exited the screen, but that’s not what I’m going for - just one ball on the screen at one time. I am following a tutorial from youtube, but this is just something extra that wasn’t covered and I am trying to get it to work.
Paddle script:
extends KinematicBody2D
const BALL_SCENE = preload("res://Mini Scenes/Ball.tscn")
var current_ball_count = 1
var ball = BALL_SCENE.instance()
func _ready():
set_physics_process(true)
set_process_input(true) #alows
func _input(event):
if(current_ball_count == 1 && event is InputEventMouseButton && event.is_pressed()):
current_ball_count += 1
print("event triggered", current_ball_count)
var ball = BALL_SCENE.instance()
ball.set_position(get_position()-Vector2(0, 16))
get_tree().get_root().add_child(ball)
func _physics_process(delta: float):
var y = get_position().y
var mouse_x = get_viewport().get_mouse_position().x - 320
set_position(Vector2(mouse_x,y))
func check_if_dead():
if ball.dead_ball == true:
current_ball_count -= 1
print(current_ball_count)
Ball script:
extends RigidBody2D
export var speedup = 4
const MAX_SPEED = 400
var dead_ball = false
func _ready() -> void:
set_physics_process(true)
func _physics_process(delta: float):
delta*100
var bodies = get_colliding_bodies()
var ball_amount = null
for body in bodies:
if body.is_in_group("Bricks"):
get_node("/root/World")._score += 5
body.queue_free()
print("Brick Killed")
if body.get_name() == "Paddle":
var speed = get_linear_velocity().length()
var direction = get_position() - body.get_node("Anchor").get_global_position()
var velocity = direction.normalized() * min(speed+speedup, MAX_SPEED)
set_linear_velocity(velocity)
print(str(speed+speedup))
func _on_VisibilityNotifier2D_screen_exited():
queue_free()
dead_ball = true
print(dead_ball, "Ball is dead")
The above scripts results in the following outputs:
event triggered2
TrueBall is dead
After the ball leaves the screen I am unable to click again to spawn a new ball (current_ball_count -= 1 at the bottom of paddle script seems to be getting ignored.)
Sorry if this is a bit much but I have been scratching my head for some time now, and trying different approaches. Perhaps I am simply approaching this the wrong way.Trying to wrap my head around all of this so any help would be great. Thank you!
current_ball_count -= 1
at the bottom of paddle script seems to be getting ignored
From the scripts you provided it looks like you’re never even calling check_if_dead
?
njamster | 2020-04-27 12:21
I have tried calling it both in _input(event) and _ready() but the check_if_dead function still doesn’t seem to work.
I think I might scrap this logic and just have a title screen show after the ball leaves the screen, giving the player the option to start a new game.
eviking | 2020-04-27 13:46