在错误的地方产卵戈多

Spawning at wrong places godot

func spawnCircle(): #Spawn circle at random position every 3 seconds
    var circle = TextureButton.new()
    var texture = load("res://Assets/Circle.png")
    circle.set_normal_texture(texture)
    circleSpawnTimer.start(2.5)  #spawn next circle
    randomize()
    add_child(circle)
    circle.rect_position.x = randi() % int(rect_size.x)
    circle.rect_position.y = randi() % int(rect_size.y)
    circle.connect("pressed", self, "on_Circle_pressed", [circle]) #connect circle pressed signal and pass in its reference
    var circleDisappearTimer = Timer.new()  #Timer for circles to disappear if left unclicked
    circle.add_child(circleDisappearTimer)
    circleDisappearTimer.connect("timeout", circle, "queue_free") #delete circle on timeout
    circleDisappearTimer.start(4) #After 5seconds left unclicked , the circle will disappear

我有一个圆圈答题器游戏,除了圆圈有时会出现在屏幕边缘,使其只有一半可见或只有四分之一可见外,一切似乎都正常运行。我如何才能使圆圈仅在允许它完全可见的地方生成?

考虑到 rect_position corresponds to the rectangle top-left corner. And the size of the Control (TextureButton in this case) is rect_size

如果Controlrect_position比它的垂直尺寸(rect_size.y)更靠近底部,或者比它更靠近右边它的水平尺寸 (rect_size.x).

因此,我们需要一个从 0 到屏幕大小减去控件大小的范围。

按照您使用随机数生成器的方式,将是:

circle.rect_position.x = randi() % int(rect_size.x - circle.rect_size.x)
circle.rect_position.y = randi() % int(rect_size.y - circle.rect_size.y)