Changing scenes with Area2D node but only when "ui_accept" pressed

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By suresh9247
:warning: Old Version Published before Godot 3 was released.

hi, i am new to pragramming and godot…
I am trying to change scenes with Area2D node but only when “ui_accept” pressed

I followed this tutorial " https://www.youtube.com/watch?v=P6-fWbua6w0&index=17&list=PLTUoLxm6vZ9YlRe-oPHNH4gF00mR2OKgA "
extends Area2D func _on_Node2D_body_enter( body ): if (Input.is_action_pressed("ui_accept")): get_tree().get_root().get_node("World").queue_free() get_tree().get_root().add_child(load(SceneToMove).instance()) but only working once and when player entering.

i want it to work when my player is in the body not when entering… please help me.

:bust_in_silhouette: Reply From: suresh9247

Here is the answer for my question

extends Area2D
export var SceneToMove = ""
var inside = false
var action = false


func _ready():
	set_fixed_process(true)


func _on_Node2D_body_enter( body ):
	inside = true
	    	

func _on_Node2D_body_exit( body ):
	inside = false
	    	
	
func _fixed_process(delta):
	if Input.is_action_pressed("ui_accept"):
		action = true
		if (action and inside):
			get_tree().get_root().get_node("World").queue_free()
			get_tree().get_root().add_child(load(SceneToMove).instance())
			action = false

You can use _input_event and get_overlapping_bodies/areas too, but not sure if detection will be “instantaneous” as the way you are doing it.

eons | 2017-06-03 11:52