English 中文(简体)
我如何改变新创建的事例的变数?
原标题:How do I change variables of a newly created instance?

我一直在努力在即将到来的游戏中找到一个简单的障碍。 它的眼光是,在一定时间之后射杀激光器。 我把激光器的时间推移到迄今为止的工作,但出于某种原因,更新激光器的最初位置,并告知激光器应当面对的方向,这不仅仅是一项挑战。

The relevant code is this:

    // 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();
    }

For additional context:
probe is an Area2D located 20 units in front of the eye.
The game is tile based (square) and each tile is 20 units big
Direction is an integer variable on both the Eye and the laser it shoots
and the only code that s causing issues is the stuff inside of if (pause == 0)

Im 接收错误

 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

for just a bit more context, here s both relevent properties under the inspector: EvilEye inspector screenshotLazer inspector screenshot

我确实不敢肯定我做错做什么,而且与“指示”等出口变量一起陷入困境,对我来说是完全新的,因此我大概会错过。

问题回答

此处为<代码>包装的Lazer。 ∗ 待印发。

var packedLazer = GD.Load<PackedScene>("res://Entities/Lazer.tscn");

<代码>instantiatemeth of PackedScene Return a Node, 载于zer。 页: 1

var lazer = packedLazer.Instantiate();

这些错误告诉你是正确的,Node没有Position>上的定义。

您应将<代码>Instantiate的结果输入适当的类别,例如,如果类型为。 女士们:

var lazer = packedLazer.Instantiate() as Lazer;

然后,它应当发挥作用。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...