Aktualisieren node3d cobe.position in task.run? (Ziel ist es, Objekt auf dem kreisförmigen Weg zu animieren.)
Posted: 25 Feb 2025, 19:34
In Godot 4.3, C#. Objekt. In Circle Path. < /p>
muss auch im Code enthalten sein. ..
Erwartete Animationsdauer:
3.6 Sekunden => 50 ms * 360 Grad/5Step;
< Br /> Tatsächliches Verhalten: < /strong> < /p>
Wenn ich diesen Code ausführe, tut UI nichts. Es friert eine Weile ein ...
Code
muss auch im Code enthalten sein. ..
Erwartete Animationsdauer:
3.6 Sekunden => 50 ms * 360 Grad/5Step;
< Br /> Tatsächliches Verhalten: < /strong> < /p>
Wenn ich diesen Code ausführe, tut UI nichts. Es friert eine Weile ein ...
Code
Code: Select all
public partial class Scene1 : Node3D
{
public override void _Ready(){};
public override void _Process(double delta){};
void Button_Move_in_Circle_onClick() => MoveCube_inCirclularPath();
private void MoveCube_inCirclularPath()
{
Task.Run( () =>
{
//cube y is Height, move it to 2meter. above ground;
InvokeHelper.CallDeferred(() =>{
cube.position.x = 2;
});
for (int i = 0; i < 360; i = i +5)
{
Thread.Sleep(50);
var pt = GetCircleCoordinates_FromAngle(i, 4);
InvokeHelper.CallDeferred(() =>{
cube.position.x = pt.x;
cube.position.z = pt.y;
});
}
});
}
public static (double x, double y) GetCircleCoordinates_FromAngle(double angleDegrees, double distance)
{
double angleRadians = angleDegrees * Math.PI / 180.0;
double x = distance * Math.Cos(angleRadians);
double y = distance * Math.Sin(angleRadians);
return (x, y);
}
}
< /code>
InvokeHelper.cs
using Godot;
using System;
public static class InvokeHelper
{
///
/// in winforms equivalent is *this.Invoke( delegate { } )*
///
public static void CallDeferred(this Action action)
{
Callable.From(action).CallDeferred();
}
}