06. Rock Entity


This tutorial will define the Rock entity and add it to our GameScreen so that we can shoot it. This tutorial will focus specifically on the creation of the Rock entity (adding a Sprite) as well as collision. We will revisit the creation of Rock entities in the next tutorial as well when we handle the creation of Rocks over time.

Adding a PNG and Sprite

As you might have guessed, the first step is to add a PNG and a Sprite to the Rock entity. To add the PNGs:

  1. Download the following four images:Rock1.pngRock2.pngRock3.pngRock4.png
  2. Expand the “Rock” Entity in Glue
  3. Right-click on the Files item under Rock
  4. Select “Add File”->”Existing File”
  5. Navigate to where you saved the Rock images
  6. If you saved all four in the same location, then you can select all four images and click OK on the file window.
  7. You should now see the files as part of your Rock Entity


To add the Sprite:

  1. Right-click on the Objects item under Rock
  2. Select “Add Object”
  3. Enter the name “Sprite” and click OK
  4. Change the SourceType to “FlatRedBallType”
  5. Change the SourceClassType to “Sprite”
  6. Scroll the PropertyGrid up and set the TextureScale to 1.0 (this makes our Sprite sized according to the source texture.)
  7. Use the Texture dropdown to select “Rock1” (which you select actually doesn’t matter as we will be changing this value later in the tutorial)

Setting CreatedByOtherEntities to true

Just like Bullets, Rocks will be created dynamically as the game progresses. We will also want to have access to a RockFactory class to allow us to easily add instances to a List in our GameScreen. To do this:

  1. Select the Rock Entity
  2. Set “CreatedByOtherEntities” to True

Considering Rock spawning

The next question we are faced with is how to handle spawning of the rocks (spawning also means to create an Entity at runtime at a time interval). Our spawn code should have the following criteria:

  1. We should be able to control the difficulty of the game by increasing or reducing the rate at which Rocks spawn
  2. Rocks need to appear at random locations so the player cannot predict their movement
  3. Rocks should not spawn on-screen. This both looks unrealistic, and it can also be frustrating to the player if a Rock suddenly appears on top of the player
  4. Rocks should move in all directions. Our game will require the player to rotate the ship to navigate in every direction, so rocks shouldn’t just appear on the top of the screen.

Furthermore, we must also consider where we should put the code for Rock spawning. One option is to put the code in the GameScreen. However, we can keep our code easier to maintain if we separate this logic into its own class. Also, since we will want to have variables to control the initial spawn rate as well as how fast the spawn rate grows, we will create a new Entity to store these variables.

Creating RockSpawner

To create the RockSpawner Entity:

  1. Right-click on the Entities item in Glue
  2. Select “Add Entity”
  3. Enter the name “RockSpawner”

For the RockSpawner to function, you will need to have an instance of it in your GameScreen:

  1. Click+Drag the RockSpawner entity into your GameScreen
  2. If asked if you want to create an instance, click “Yes”
  3. You should now have a RockSpawnerInstance in your GameScreen


Next we’ll add variables for RocksPerSecond, SpawnRateIncrease, MinVelocity, and MaxVelocity:


  1. Expand the RockSpawner Entity
  2. Right-click on the Variables item
  3. Select “Add Variable”
  4. Select the “Create a new variable” option
  5. Select the “float” type
  6. Enter the name RocksPerSecond and click OK
  7. Give the new RocksPerSecond variable a value of 0.2


  1. Right-click on the Variables item again
  2. Select “Add Variable”
  3. Select the “Create a new variable” option
  4. Select the “float” type
  5. Enter the name SpawnRateIncrease and click OK
  6. Give the new SpawnRateIncrease variable a value of 0.015


  1. Right-click on the Variables item again
  2. Select “Add Variable”
  3. Select the “Create a new variable” option
  4. Select the “float” type
  5. Enter the name MinVelocity and click OK
  6. Give the new MinVelocity variable a value of 50


  1. Right-click on the Variables item again
  2. Select “Add Variable”
  3. Select the “Create a new variable” option
  4. Select the “float” type
  5. Enter the name MaxVelocity and click OK
  6. Give the new MaxVelocity variable a value of 100

RockSpawner logic

Now that we have variables to use for spawning, we can create our spawning logic. The logic will be broken up into two parts:

  1. Deciding if it’s time to spawn a rock
  2. Spawning a rock and giving it its initial velocity

First, we’ll add the top-level logic to CustomActivity. To do this:

  1. Go to Visual Studio
  2. Open RockSpawner.cs
  3. Add the following code to CustomActivity:
Now we need to add the IsTimeToSpawn property to do this, add the following code to your RockSpawner.cs file:
Next let’s create the PerformSpawn function:

Vector3 not found?

If Visual Studio draws the red squiggly line underneath Vector3 and complains that it can’t find it, then add the following using statement at the top of the file:

Why do we create so many functions?You may be wondering why we have created so many functions when we implement the logic for our Entities. It would be possible to write all of the code in a single function.

However, there are many reasons for breaking your code up into multiple functions. One of the most important is readability. Looking at a long function can be difficult to understand, and also difficult to maintain. Separating your code into multiple functions makes it far more readable and easy to maintain.

At this point we have two functions left to write – a GetRandomRockPosition function and a GetRandomRockVelocity. Each function will require some math and explanation so we will cover each in its own section.


GetRandomRockPosition will give us a position on the edge of the screen. Conceptually the process will be:

  • Picking a random side on the screen – options are top, right, bottom, and left
  • Picking a random point on the selected side
  • Moving the point off-screen so that the Rock will be fully off-screen when it is spawned

To perform these three steps, add the following code to the RockSpawner.cs file:


GetRandomRockVelocity will give us the velocity that the Rock should move at. This implementation will result in all rocks starting on the edge of the screen and moving towards the center. A more advanced velocity might randomize the angle at which they move so that they don’t always pass through the middle; however, this approach will give us sufficiently random movement.

The steps to this method are:

  1. Find the center of the screen at Z = 0
  2. Get the direction towards the center of the screen
  3. Normalize the direction, then multiply it by the desired speed to get the final velocity

To perform these steps, add the following method to your RockSpawner.cs:

Increasing the spawn rate

If you run the game at this point you will notice that the RockSpawner spawns rocks, but the speed doesn’t increase. To change this, add the following code to the RockSpawner’s CustomActivity:



Now the game is really making progress. If you run the game now you will be able to fly around and fire bullets at rocks. Of course, we haven’t put collision in just yet. We’ll do that next tutorial.

<- 05. Main Ship Behavior06. Collision ->