使用单例更改另一个节点的颜色

changing color of another node using singleton

我正在尝试从另一个脚本 "Build_Tree" 中更改实例化 "Tree" 节点内的 create_circle 函数的颜色值。应该有一系列的红色圆圈到白色圆圈(底部是红色,随着高度逐渐变白),但是有一堆黑色圆圈。我已经尝试将 Color8(0, 0, 0) 设置为 Tree.gd 中的全局变量,并设置一系列由单例访问的 R G 和 B 值。有趣的是,当我将对象的 pos_x 和 pos_y 设置为 0 时,有一个作为正确的颜色生成。但显然这在左上角形成了一个圆圈并且在它是自己的

代码如下:

Build_Tree.gd

builder() 存储颜色渐变和树中分割的位置

func builder():
    var color_gradient_cache = 255
    var radius = 36
    var anchor_position = 0
    var position_cache_storage = []
    for i in 3:
        if i == 0: 
            position_cache_storage.append(create_section(color_gradient_cache, radius, position_cache))
        elif i == 1:
            anchor_position = 1
            position_cache_storage.append(create_section(color_gradient_cache, radius, position_cache_storage[0], anchor_position))
        elif i == 2:
            anchor_position = 2
            position_cache_storage.append(create_section(color_gradient_cache, radius, position_cache_storage[0], anchor_position))

create_section 创建一个 3 部分的圆然后 returns 最后一个位置所以我可以在那里添加一个拆分

func create_section(color, radius, pos,  anchor_position = 0):
    var anchor_angle
    print(str(anchor_position))
    var current_position = pos
    # 3-4 section of limbs
    for i in rand_range(3,4):
        if anchor_position == 0:
            anchor_angle = deg2rad(-rand_range(75, 105))
            current_position = Vector2(current_position.x + ((radius-10) * cos(anchor_angle)), current_position.y + ((radius-10) * sin(anchor_angle)))
            print(str(current_position))
            create_tree(radius, current_position.x, current_position.y, color)
        elif anchor_position == 1:
            anchor_angle = deg2rad(-rand_range(180, 150))
            current_position = Vector2(current_position.x + ((radius-10) * cos(anchor_angle)), current_position.y + ((radius-10) * sin(anchor_angle)))
            print(str(current_position))
            create_tree(radius, current_position.x, current_position.y, color)
        elif anchor_position == 2:
            anchor_angle = deg2rad(-rand_range(75, 45))
            current_position = Vector2(current_position.x + ((radius-10) * cos(anchor_angle)), current_position.y + ((radius-10) * sin(anchor_angle)))
            print(str(current_position))
            create_tree(radius, current_position.x, current_position.y, color)
    return current_position

创建每个圈子

func create_tree(radius, pos_x, pos_y, color):
    # creates the tree (circle) when called
    print(str(color))
    var TreeObjVar = get_node("/root/Tree_Gd")
    TreeObjVar.pos_x = pos_x
    TreeObjVar.pos_y = pos_y
    TreeObjVar.R = color
    TreeObjVar.G = 0
    TreeObjVar.B = 0
    print (TreeObjVar.G)
    var TreeScene = load("res://Tree.tscn")
    var TreeObj = TreeScene.instance()
    TreeObj.set_position(Vector2(pos_x, pos_y))
    get_node("/root/").call_deferred("add_child", TreeObj)

Tree.gd

extends Node2D

# Declare member variables here. Examples:
var r = 18.0
var pos_x = 0
var pos_y = 0
var R = 0
var G = 0
var B = 0
# Called when the node enters the scene tree for the first time.
func _ready():
    set_process(true)
    randomize()
    pass # Replace with function body.

func _draw():
    draw_circle( Vector2(pos_x, pos_y), r, Color8(R, G, B))


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
    if r < 36.0:
        r += 3.0
    print(str(R) + " " + str(G) + " " + str(B))
    update()

我希望这一切听起来都可以理解,我不太了解自己在做什么,但我断断续续地做游戏设计师太久了,我想我会要求一点帮助而不是再次放弃

我已经检查并标记了代码,并提出了一些建议以及如何插入颜色:

Build_Tree.gd

extends Node2D


## Use exports to parameterize your tree builder node.
export(Color) var color_start = ColorN('red')
export(Color) var color_stop = ColorN('white')
export(float) var radius = 36.0

## If you are going to apply a gradient, you can
## use the gradient resource as a helper.
## I'll show you how to use `Gradient` below.
export(Gradient) var gradient = Gradient.new()


var position_cache = Vector2()

func _ready():
    randomize()
    position_cache = Vector2(position.x, position.y)
    set_process(true)
    set_process_input(true)
    create_small(color_start, color_stop, 36.0, position_cache)

## If you go the exports route you can remove
## these parameters and just use `self`'s properties.
func create_small(color_start, color_stop, radius, pos):
    # anchor_angle holds the random number that holds where the tree deviates from the center
    var anchor_angle

    ## I added iterations here to use it as the weight/offset in
    ## the color lerping functions below.
    var iterations = 3.0
    for i in range(int(iterations)):

        ## Color can be changed here.
        ## `Color` provides the `Color.linear_intepolate()` method to
        ## lerp between two colors:
        create_tree(color_start.linear_interpolate(color_stop, i / iterations))
        ## Alternatively, you can use the `Gradient` resource:
#       create_tree(gradient.get_offset(i / iterations))

        anchor_angle = deg2rad(-rand_range(75, 105))
        position_cache = Vector2(position_cache.x + (radius * cos(anchor_angle)), position_cache.y + (radius * sin(anchor_angle)))


## The `color` parameter should take a `Color` object.
## Or, if you go the exports route, remove the parameters and
## use `self`'s properties.
func create_tree(color):
    var TreeObjVar = get_node("/root/Tree_gd")
    var TreeScene = load("res://Tree.tscn")
    var TreeObj = TreeScene.instance()
    get_node("/root/").call_deferred("add_child", TreeObj)

    ## This only sets `R` on the Singleton `Tree`.
    ## On `Tree`, `R` is not used after initialization.
    TreeObjVar.R = color.r

    ## The color of the newly instanced `Tree` will now be updated.
    TreeObj.color = color

    ## What you want to do is use
    TreeObj.set_position(Vector2(position_cache.x, position_cache.y))

Tree.gd

extends Node2D

#global variables to be accessible by singleton
var pos_x = 0
var pos_y = 0

#radius starts small so it can increase for a flashy entry
var r = 3.0


## Remove these, access color via `color`
#color values start at 'black' so I don't get excited when things are going wrong
var R = 0
var G = 0
var B = 0

var color = Color8(R, G, B)

func _ready():
    set_process(true)

func _draw():
    draw_circle(Vector2(pos_x, pos_y), r, color)

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
    if r < 36.0:
        r += 3.0
    update()