方向 += (浮动)GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4); c# (float) 在这里是什么意思?
direction += (float)GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4); c# what does the (float) mean here?
我正在阅读 Godot 引擎文档,看到了一些对我来说毫无意义的内容。
有人可以解释一下 (float) 或 (Type) 在 class 之前的 是什么意思吗?
direction += (float)GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
mob.Rotation = direction;
或
var mob = (Mob)MobScene.Instance();
这是代码片段:
public void OnMobTimerTimeout()
{
// Note: Normally it is best to use explicit types rather than the `var`
// keyword. However, var is acceptable to use here because the types are
// obviously Mob and PathFollow2D, since they appear later on the line.
// Create a new instance of the Mob scene.
var mob = (Mob)MobScene.Instance();
// Choose a random location on Path2D.
var mobSpawnLocation = GetNode<PathFollow2D>("MobPath/MobSpawnLocation");
mobSpawnLocation.Offset = GD.Randi();
// Set the mob's direction perpendicular to the path direction.
float direction = mobSpawnLocation.Rotation + Mathf.Pi / 2;
// Set the mob's position to a random location.
mob.Position = mobSpawnLocation.Position;
// Add some randomness to the direction.
direction += (float)GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
mob.Rotation = direction;
// Choose the velocity.
var velocity = new Vector2((float)GD.RandRange(150.0, 250.0), 0);
mob.LinearVelocity = velocity.Rotated(direction);
// Spawn the mob by adding it to the Main scene.
AddChild(mob);
}
来自文章The main game scene,是Godot官方文档的一部分。
我找不到相关信息。
一些解释这个的来源或示例会很好,提前致谢。
这是类型转换。这与 class 或其类型无关,而是与函数调用的 return 值有关。您没有提供完整的上下文,但我假设对 GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
return 的调用是 double
类型的值,而变量 direction
是 float
类型的值。由于没有从 double
到 float
的隐式转换,因此需要进行显式转换。 (float)
正是这样做的:它将 RandRange
方法调用的 return 值转换为类型 float
.
我正在阅读 Godot 引擎文档,看到了一些对我来说毫无意义的内容。
有人可以解释一下 (float) 或 (Type) 在 class 之前的 是什么意思吗?
direction += (float)GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
mob.Rotation = direction;
或
var mob = (Mob)MobScene.Instance();
这是代码片段:
public void OnMobTimerTimeout()
{
// Note: Normally it is best to use explicit types rather than the `var`
// keyword. However, var is acceptable to use here because the types are
// obviously Mob and PathFollow2D, since they appear later on the line.
// Create a new instance of the Mob scene.
var mob = (Mob)MobScene.Instance();
// Choose a random location on Path2D.
var mobSpawnLocation = GetNode<PathFollow2D>("MobPath/MobSpawnLocation");
mobSpawnLocation.Offset = GD.Randi();
// Set the mob's direction perpendicular to the path direction.
float direction = mobSpawnLocation.Rotation + Mathf.Pi / 2;
// Set the mob's position to a random location.
mob.Position = mobSpawnLocation.Position;
// Add some randomness to the direction.
direction += (float)GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
mob.Rotation = direction;
// Choose the velocity.
var velocity = new Vector2((float)GD.RandRange(150.0, 250.0), 0);
mob.LinearVelocity = velocity.Rotated(direction);
// Spawn the mob by adding it to the Main scene.
AddChild(mob);
}
来自文章The main game scene,是Godot官方文档的一部分。
我找不到相关信息。
一些解释这个的来源或示例会很好,提前致谢。
这是类型转换。这与 class 或其类型无关,而是与函数调用的 return 值有关。您没有提供完整的上下文,但我假设对 GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
return 的调用是 double
类型的值,而变量 direction
是 float
类型的值。由于没有从 double
到 float
的隐式转换,因此需要进行显式转换。 (float)
正是这样做的:它将 RandRange
方法调用的 return 值转换为类型 float
.