0 votes

I have a code like this but im a facing a problem, my coins are overlapping each other

func ready():
randomize()
for i in range(3):
var Coins1 = preload("res://Assets/Coins/Coin
Collection1.tscn").instance()
add
child(Coins1)
Coins1.globalposition.x = randrange(-3500,6000)
Coins1.globalposition.x = randrange(-1000,650)
for i in range(1):
var Coins2 = preload("res://Assets/Coins/CoinCollection2.tscn").instance()
addchild(Coins2)
Coins2.global
position.x = randrange(-3500,6000)
Coins2.global
position.x = randrange(-1000,650)
for i in range(4):
var Coins3 = preload("res://Assets/Coins/Coin
Collection3.tscn").instance()
add
child(Coins3)
Coins3.globalposition.x = randrange(-3500,6000)
Coins3.globalposition.x = randrange(-1000,650)
for i in range(2):
var Coins4 = preload("res://Assets/Coins/CoinCollection4.tscn").instance()
addchild(Coins4)
Coins4.global
position.x = randrange(-3500,6000)
Coins4.global
position.x = rand_range(-1000,650)

Godot version 3.2
in Engine by (14 points)

1 Answer

0 votes

Hi,

I am no expert but there may be a few problems with your code (cannot see the indent).

You are always changing position.x never y (might be normal for a platform).
You randomize once in the beginning. that could be the problem.
I would try to make a Global method for randomization :

func roll_dice(start, finish):
    var rng = RandomNumberGenerator.new()
    rng.randomize()
    return rng.randi_range(start, finish)

And call it everytime you need a random number. This way you should not have the same number:

Coins1.globalposition.x = Global.roll_dice(-3500,6000)
Coins1.globalposition.x = Global.roll_dice(-1000,650)

Yet, you could also change the range according to previous coinspawn (to avoid having them too close, or too random), maybe by having a check like :
if a coin position is closer than 200, reroll dices.

Hope this helps.

by (80 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.