我正在尝试从另一个脚本“Build_Tree”中更改实例“Tree”节点内的 create_circle 函数的颜色值。应该有一系列的红圈到白圈(红色在底部,随着越来越高逐渐变白),但是有一堆黑圈。我尝试将 Color8(0, 0, 0) 设置为 Tree.gd 中的全局变量,并设置一系列可由单例访问的 RG 和 B 值。有趣的是,当我将对象的 pos_x 和 pos_y 设置为 0 时,生成了正确颜色的 ONE。但显然这会在左上角形成一个圆圈,并且它本身并不是很令人印象深刻
这是代码:
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 部分的圆圈,然后返回最后一个位置,这样我就可以在那里添加一个分割
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)
树.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()
我希望这一切听起来都可以理解,我对自己的工作了解不多,但我从事游戏设计师工作太久了,我想我会寻求一点帮助而不是放弃再次