Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | JCB2001 |
Hello to everyone, I’m new in Godot and now that I’m creating an Air Hockey game I found a problem. At first, I made the ball with a KinematicBody2D and when I thought the game was finished I realized that the ball could work better being a RigidBody2D so I changed the type of ball node into a RigidBody2D. The problem is that when I had the ball as a KinematicBody2D , the points system was working (when a player scored a goal, a point was added, when one of the player get an amount of points the winner message and the restart and quit buttons were visible, etc) but with the RigidBody2D that doesn’t happen, the only thing that happens is that when the ball enters on the Area2D, it disappear, only that. I tried changing the type of area but it didn’t work, I checked if the ball contact monitor and contacts report was correct and yeah, everything’s correct except what I told you. I know that maybe this can be a dumb question for many of you, but I’m new here so I hope you could help me. This is the game scripts:
Player 1:
extends KinematicBody2D
var speed = 400
func _physics_process(delta):
var velocity = Vector2.ZERO
if Input.is_action_pressed("ui_up"):
velocity.y -= speed
if Input.is_action_pressed("ui_down"):
velocity.y += speed
if Input.is_action_pressed("ui_left"):
velocity.x -= speed
if Input.is_action_pressed("ui_right"):
velocity.x += speed
move_and_slide(velocity)
func stop_player1():
speed = 0
func start_player1():
speed = 400
Player 2:
extends KinematicBody2D
var speed = 400
func _physics_process(delta):
var velocity = Vector2.ZERO
if Input.is_action_pressed("go_up"):
velocity.y -= speed
if Input.is_action_pressed("go_down"):
velocity.y += speed
if Input.is_action_pressed("go_left"):
velocity.x -= speed
if Input.is_action_pressed("go_right"):
velocity.x += speed
move_and_slide(velocity)
func stop_computerplayer():
speed = 0
func start_computerplayer():
speed = 400
Main Level:
extends Node
onready var player_score_label = $PlayerScore
onready var computer_score_label = $ComputerScore
onready var ball = $Ball
onready var restart_button = $RestartButton
onready var winner_label = $WinnerLabel
onready var gomenu_button = $Go_Menu
onready var player_1 = $Player1
onready var computer_player = $ComputerPlayer
onready var screen_size = get_viewport().size
var player_score = 0
var computer_score = 0
func _ready():
player_score_label.text = "0"
computer_score_label.text = "0"
restart_button.hide()
winner_label.hide()
gomenu_button.hide()
func _on_CenterTopWall_body_entered(body):
computer_score += 1
check_score()
update_score_labels()
yield(get_tree().create_timer(1,5),"timeout")
ball.position = Vector2(screen_size.x/2, screen_size.y/2)
func _on_CenterButWall_body_entered(body):
computer_score += 1
player_score += 1
check_score()
update_score_labels()
yield(get_tree().create_timer(1,5),"timeout")
ball.position = Vector2(screen_size.x/2, screen_size.y/2)
func update_score_labels():
player_score_label.text = str(player_score)
computer_score_label.text = str(computer_score)
func check_score():
var winner_message
if computer_score == 2 or player_score == 2:
if computer_score > player_score:
winner_message = "¡You Lose!"
else:
winner_message = "¡You Win!"
winner_label.text = winner_message
ball.stop_ball()
restart_button.visible = true
winner_label.visible = true
gomenu_button.visible = true
player_1.stop_player1()
computer_player.stop_computerplayer()
player_1.position = Vector2(144,465)
computer_player.position = Vector2 (147,47)
func _on_RestartButton_pressed():
restart_button.hide()
winner_label.hide()
gomenu_button.hide()
player_score = 0
computer_score = 0
ball.position.x = screen_size.x/2
ball.position.y = screen_size.y/2
player_1.start_player1()
computer_player.start_computerplayer()
ball.start_ball()
update_score_labels()
func _on_Go_Menu_pressed():
get_tree().change_scene("res://Menu.tscn")
Menu:
func _on_PlayButton_pressed():
get_tree().change_scene("res://MainLevel.tscn")
func _on_QuitButton_pressed():
get_tree().quit()
Wait for your answers, thank you.