|
@@ -322,14 +322,13 @@ Note that a new instance must be added to the scene using ``add_child()``.
|
|
|
.. code-tab:: gdscript GDScript
|
|
|
|
|
|
func _on_MobTimer_timeout():
|
|
|
+ # Create a new instance of the Mob scene.
|
|
|
+ var mob = mob_scene.instance()
|
|
|
+
|
|
|
# Choose a random location on Path2D.
|
|
|
- var mob_spawn_location = get_node("MobPath/MobSpawnLocation");
|
|
|
+ var mob_spawn_location = get_node("MobPath/MobSpawnLocation")
|
|
|
mob_spawn_location.offset = randi()
|
|
|
|
|
|
- # Create a Mob instance and add it to the scene.
|
|
|
- var mob = mob_scene.instance()
|
|
|
- add_child(mob)
|
|
|
-
|
|
|
# Set the mob's direction perpendicular to the path direction.
|
|
|
var direction = mob_spawn_location.rotation + PI / 2
|
|
|
|
|
@@ -340,26 +339,28 @@ Note that a new instance must be added to the scene using ``add_child()``.
|
|
|
direction += rand_range(-PI / 4, PI / 4)
|
|
|
mob.rotation = direction
|
|
|
|
|
|
- # Choose the velocity.
|
|
|
+ # Choose the velocity for the mob.
|
|
|
var velocity = Vector2(rand_range(150.0, 250.0), 0.0)
|
|
|
mob.linear_velocity = velocity.rotated(direction)
|
|
|
|
|
|
+ # Spawn the mob by adding it to the Main scene.
|
|
|
+ add_child(mob)
|
|
|
+
|
|
|
.. code-tab:: csharp
|
|
|
|
|
|
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 PathFollow2D and Mob, since they appear later on the line.
|
|
|
+ // 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();
|
|
|
|
|
|
- // Create a Mob instance and add it to the scene.
|
|
|
- var mob = (Mob)MobScene.Instance();
|
|
|
- AddChild(mob);
|
|
|
-
|
|
|
// Set the mob's direction perpendicular to the path direction.
|
|
|
float direction = mobSpawnLocation.Rotation + Mathf.Pi / 2;
|
|
|
|
|
@@ -373,19 +374,21 @@ Note that a new instance must be added to the scene using ``add_child()``.
|
|
|
// 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);
|
|
|
}
|
|
|
|
|
|
.. code-tab:: cpp
|
|
|
|
|
|
// This code goes in `main.cpp`.
|
|
|
void Main::_on_MobTimer_timeout() {
|
|
|
+ // Create a new instance of the Mob scene.
|
|
|
+ godot::Node *mob = mob_scene->instance();
|
|
|
+
|
|
|
// Choose a random location on Path2D.
|
|
|
_mob_spawn_location->set_offset((real_t)_random->randi());
|
|
|
|
|
|
- // Create a Mob instance and add it to the scene.
|
|
|
- godot::Node *mob = mob_scene->instance();
|
|
|
- add_child(mob);
|
|
|
-
|
|
|
// Set the mob's direction perpendicular to the path direction.
|
|
|
real_t direction = _mob_spawn_location->get_rotation() + (real_t)Math_PI / 2;
|
|
|
|
|
@@ -399,6 +402,9 @@ Note that a new instance must be added to the scene using ``add_child()``.
|
|
|
// Choose the velocity for the mob.
|
|
|
godot::Vector2 velocity = godot::Vector2(_random->randf_range(150.0, 250.0), 0.0);
|
|
|
mob->set("linear_velocity", velocity.rotated(direction));
|
|
|
+
|
|
|
+ // Spawn the mob by adding it to the Main scene.
|
|
|
+ add_child(mob);
|
|
|
}
|
|
|
|
|
|
.. important:: Why ``PI``? In functions requiring angles, Godot uses *radians*,
|