【问题标题】:Spawning at wrong places godot在错误的地方产卵
【发布时间】:2021-05-08 19:59:28
【问题描述】:
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

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

【问题讨论】:

  • 建议:用它的纹理、它的计时器和信号连接制作一个圆圈……进入一个新场景。然后实例化该场景,而不是在代码中完成所有工作(请参阅Instancing scenes)。您甚至可以将其tree_entered 连接到定位它的脚本。

标签: godot


【解决方案1】:

考虑到rect_position 对应矩形的左上角。而Control(本例中为TextureButton)的大小为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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    相关资源
    最近更新 更多