Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dodge the Creeps: Set the physics parameters of the mob before adding it to the scene tree #687

Merged
merged 1 commit into from
Mar 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions 2d/dodge_the_creeps/Main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ func new_game():


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")
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

Expand All @@ -48,6 +47,8 @@ func _on_MobTimer_timeout():
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)

func _on_ScoreTimer_timeout():
score += 1
Expand Down