我一直在尝试在即将到来的游戏中完成一个简单的障碍。它是一只在经过一定时间后发射激光的眼睛。到目前为止,我已经让计时器和激光器实例化工作了,但由于某种原因,更新激光器的初始位置并告知它应该面向哪个方向已经不仅仅是一个挑战了。
相关代码是这样的:
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
probe = GetNode<Area2D>("FacingProbe");
lidSprite = GetNode<AnimatedSprite2D>("EvilEyeLid");
ballSprite = GetNode<AnimatedSprite2D>("EvilEyeBall");
ballSprite.Play("Idle");
switch (direction)
{
case 1: lidSprite.Rotation = 3.1416f; probe.Position = new Vector2(-20,0); break;
case 2: lidSprite.Rotation = 0; probe.Position = new Vector2(20,0); break;
case 3: lidSprite.Rotation = 4.712f; probe.Position = new Vector2(0,-20); break;
case 4: lidSprite.Rotation = 1.5708f; probe.Position = new Vector2(0,20); break;
}
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
if (pause == 0)
{
var packedLazer = GD.Load<PackedScene>("res://Entities/Lazer.tscn");
var lazer = packedLazer.Instantiate();
lazer.Position = probe.Position;
lazer.Direction = Direction;
AddChild(lazer);
}
animations();
}
更多背景信息:
“探针”是位于眼睛前方 20 个单位的 Area2D。
该游戏基于图块(正方形),每个图块有 20 个单位大
“方向”是眼睛和它发射的激光上的整数变量
唯一导致问题的代码是
if (pause == 0)
内部的内容
我收到的错误是
'Node' does not contain a definition for 'Position' and no accessible extension method 'Position' accepting a first argument of type 'Node' could be found
'Node' does not contain a definition for 'Direction' and no accessible extension method 'Direction' accepting a first argument of type 'Node' could be found
The name 'Direction' does not exist in the current context
我真的不确定我做错了什么,并且实例化以及像“方向”这样的导出变量对我来说是全新的,所以我很可能会陷入初学者的错误。
这里
packedLazer
的类型将是 PackedScene
:
var packedLazer = GD.Load<PackedScene>("res://Entities/Lazer.tscn");
并且
instantiate
的 PackedScene
方法返回一个 Node
,所以这里 lazer
将是一个 Node
。
var lazer = packedLazer.Instantiate();
错误告诉您的是正确的,
Node
没有Position
或Direction
的定义。
您应该将
Instantiate
的结果转换为适当的类型,例如,如果类型是Lazer
,您可以这样做:
var lazer = packedLazer.Instantiate() as Lazer;
然后它应该可以工作。