Create a new Scene with Node as root
Rename to AsteroidSpawner
Number of asteroids
The Asteroid Scene
Assign the Asteroid scene
Create a function to spawn an asteroid
Push an error if the asteroid scene is not assigned
Instantiate the asteroid scene
Add the scene to the Scene Tree
What is call_deferred( )?
call_deferred() is a programming function that schedules a method call to run later, typically at the end of the current frame or during the next "idle time," rather than immediately. It's crucial for safe operations on the scene tree (like adding/removing nodes) or physics that can't happen mid-frame, preventing errors by waiting until the main loop is free, ensuring everything is ready and avoiding conflicts.
Create a function to spawn multiple asteroids
Use GameUtils to get the screen bounds
Spawn an asteroid and repeat
Test the game - Asteroids should spawn at random positions with random sizes
Set up Layers to handle collisions
Click Player node in Player scene
In the Collision property
Click the 3 dots next to layer
Set the layer names:
Layer 1: player
Layer 2: bullet
Layer 3: asteroid
Layer 4: ufo
Layer 5: ufo_bullet
Now set the Layer and Mask
Now set the Layer and Mask
Connect the body entered signal