Help for a noob in scene changing

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

Hi, I am trying to figure out how to switch levels in a game I made. I used this script

extends Sprite

func _on_Hitbox_area_entered(area: Area2D) -> void:
	if area.is_in_group("Player"):
		get_tree().change_scene("res://Scenes/Level_2")

Nothing happens when the player collides with the Hitbox. Does anyone know what I am doing wrong – is there a good tutorial on this?

I don’t see anything wrong with the code here. Can you provide more information? Did you connect the signal properly?

exuin | 2021-01-24 03:27

Please see my reply to TchnlgPsnt below for more information–thanks for any help you can give.

NABL | 2021-01-24 17:02

:bust_in_silhouette: Reply From: TchnlgPsnt

I see that your script extends Sprite, so I’m thinking maybe the collision isn’t being picked up. The _on_Hitbox_area_entered function will only detect nodes of type Area2D that enter it, so try making sure that the node you want to collide with the hitbox is of type Area2D and has a CollisionShape2D or CollisionPolygon2D child.

Alternatively, you could instead use the on_body_entered signal to detect when physicsbodies enter the hitbox. If your player is a physicsbody, you will have more control over its movement. In this case, both nodes involved in the collision would still need to have a CollisionShape2D or CollisionPolygon2D child.

Thank you for your help,

Yes, the collision is definitely not being picked up; you are right.

I have a Collisionpolygon2D for my Player and a CollisionShape2D for my Sprite as children.

Unfortunately it is still not working–and I don’t an error message, the player just passes right through the Sprite. Here is the code as it looks now:

extends Sprite

func on_body_entered(area: KinematicBody2D) -> void:
	if area.is_in_group("Player"):
		get_tree().change_scene("res://Level_2")

And, this is the script for my Player (in case this helps solve the problem):

    extends KinematicBody2D

var run_speed = 400
var jump_speed = -1100
var gravity = 2000

var velocity = Vector2()

export (int) var speed = 200



	
func get_input():
	velocity.x = 0
	var right = Input.is_action_pressed('right')
	var left = Input.is_action_pressed('left')
	var jump = Input.is_action_just_pressed('jump')



	if is_on_floor() and jump:
		velocity.y = jump_speed
	if right:
		velocity.x += run_speed
	if left:
		velocity.x -= run_speed

func _physics_process(delta):
	velocity.y += gravity * delta
	get_input()
	velocity = move_and_slide(velocity, Vector2(0, -1))

I used a signal to link the on_body_entered and connected it to my Sprite. Not sure if I connected it correctly.

Many thanks for any other thoughts!!

NABL | 2021-01-24 15:06

The scene tree is set up as (again not sure if this is helpful):

Level_1
–BackgroundLevel_1
–TileMap
–Player
------Camera2D2
------The Wizard
------CollisionPolygon2D
–Level_Changer
------Hitbox
----------CollisionShape2D

NABL | 2021-01-24 15:10

Just to be clear about the signal: you link the hitbox’s collisionshape2d to itself, and not the player’s collisionpolygon2d, right?

exuin | 2021-01-25 00:51