Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | NABL |
I asked this question before and got some helpful answers. Unfortunately it is still not working–and I don’t have 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.
The scene tree is set up as (again not sure if this is helpful):
Level1
–BackgroundLevel1
–TileMap
–Player
------Camera2D2
------The Wizard
------CollisionPolygon2D
–Level_Changer
------Hitbox
----------CollisionShape2D
- What type of node is Level_Changer and Hitbox?
- Have you made sure the body entering the area is in the player group?
- Try printing something under
on_body_entered
to determined whether this function is actually getting called, or whether there is an issue with the scene change. - Have you connected the Area2D signal to the function which is supposed to change the scene?
psear | 2021-01-24 19:41
Did you put get_tree().change_scene("res://Level_2")
in your code?
Maybe the problem is that your scene’s name probably is res://Level_2.tscn
yyyyk | 2021-01-24 19:55
Thank you for your response.
-
Level_Changer is a Sprite, and Hitbox is an Area 2D.
-
The body entering the are is in the player group.
-
I tried printing it but nothing appeared, but this error message appears in the debugger.
E 0:00:04.138 emit_signal: Error calling method from signal ‘body_entered’: ‘Sprite(Next_Level.gd)::_on_Hitbox_body_entered’: Method not found…
<C++ Source> core/object.cpp:1260 @ emit_signal()
- I connected the Area2D signal to the function that changes the scene, and the signal icon appears.
Any advice on that error?
thank you!!
NABL | 2021-01-24 20:02
Yes, thank you–I tried both get_tree().change_scene("res://Level_2.tscn")
and without the .tscn
in my code. Not sure if that is what you meant but it didn’t work. Thanks anyway.
NABL | 2021-01-24 20:08
Do you still get the error after connecting the right signal to the right function?
Just point out, the error says it couldn’t call on_hitbox_body_entered
, because the method isn’t found. The function in your code is called on_body_entered
. That might be the source of one issue.
Double check the docs on how to properly connect signals in the gui, that might also help.
psear | 2021-01-24 22:58