方向 += (float)GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4); c# 这里的 (float) 是什么意思?
我正在阅读 Godot 引擎文档,发现一些对我来说毫无意义的内容。
有人可以解释一下弯括号中的(float)或(Type)在类之前意味着什么吗?
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);
}
它来自文章主要游戏场景是Godot官方文档的一部分。
我找不到有关它的一些信息。
一些解释这一点的来源或示例会很棒,提前致谢。
I am reading the Godot Engine documentation and saw something that makes no sense to me.
Can somebody explain what a (float) or (Type) in curved brackets means before a class?
direction += (float)GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
mob.Rotation = direction;
OR
var mob = (Mob)MobScene.Instance();
This is the code snippet:
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);
}
It is from the article The main game scene which is part of the official documentation of Godot.
I could not find some information about it.
Some Source or examples that explains this would be great, thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个类型转换。它与类或其类型无关,而是与函数调用的返回值有关。您没有提供完整的上下文,但我假设调用
GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
返回一个double 类型的值
code> 且变量direction
的类型为float
。由于没有从double
到float
的隐式转换,因此需要显式转换。(float)
正是这样做的:它将RandRange
方法调用的返回值转换为float
类型。It's a type cast. It's not about the class or its type, but about the return value of the function call. You don't give full context, but I assume the call to
GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
returns a value of typedouble
and the variabledirection
is of typefloat
. Since there's no implicit conversion fromdouble
tofloat
an explicit conversion is required.(float)
does exactly that: It converts the return value of theRandRange
method call to typefloat
.