reset variable

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

hello, I need some help.

I using randi to create ramdomly three scenes.
but each time the scene is created, the velocity increases.

extends Node2D

var speed = 400

func _physics_process(delta):

var pos_x1 = position.x
pos_x1 -= speed1 * delta
position.x = pos_x1

func create_map():
randomize()
var alpha = load(“res://scenes/map” + str(randi()%3 + 1) + “.tscn”)
var alphainstance = alpha.instance()
alphainstance.position = Vector2(5400,0)
add_child(alphainstance)
$Timer.stop()

func _on_limite_body_entered(body):

if "ninja" in body.name:
	$Timer.start()

func _on_Timer_timeout():
create_map()

:bust_in_silhouette: Reply From: MagnusS

I think I know where your mistake is but I’m not completely sure as your question is missing a lot of information and the provided code is formated horribly (and thus extremely hard to read). Please improve this for future questions.

Now to my possible explaination. Am I right guessing that this script is running in a map? Because if this is the case you would add the new map as a child of the exiting one and therefor the new map moves every time the parent map moves. After the parent has moved itself and the child the child moves itself as well. => Double movement every frame

To fix this try replacing “add_child (alphainstance)” with “get_parent ().add_child (alphainstance)”. If this doesn’t help then we need more information about your code and file structure.

ok, I have (for while) 3 scenes (I did with tilemaps but this doesn’t matter).
I got to create them randomly .

the problem is:
when a new scene is created, it comes faster then previous one.

I don’t know why if I declare all the scenes with the same value:

“var speed = 400”
is there any way to reset this variable somewhere?
I believe it is adding up or multiplying each time I create a new scene.

CB | 2019-03-01 20:38

As I said. This is probably due to you adding every map as a child of the previous map. Change you add_child line to

get_parent().add_child(alphainstance)

If this doesn’t help then we’ll see further.

MagnusS | 2019-03-02 18:43

Hello Magnus, perfect!! Thank you. Now the scenes are created at same speed.

but sometimes bug and does’t appears anything

CB | 2019-03-02 23:47