08. Rock States


When we added a Sprite to the Rock Entity, we added four different images. At this point our game only uses one image (the smallest one). This tutorial will create states for each of the sizes, and will implement Rocks breaking up into smaller rocks.

Creating States

The first step is to create four states – one for each size of rock. We will call the states Size1, Size2, Size3, and Size4. Size1 will be the smallest and Size4 will be the largest.

First we’ll create the four states:

  1. Expand the Rock Entity
  2. Right-click on the States item
  3. Select “Add State”
  4. Enter the name “Size1”
  5. Repeat the steps above to create Size2, Size3, and Size4

Once finished you should see the four states:


Whenever states are added to a Screen or Entity, Glue will automatically add an enum for you with matching names. In this case the enumeration will contain Size1, Size2, Size3, and Size4:


Use GlueView!If you haven’t been using GlueView, now would be a good time to fire it up. GlueView lets you view states in real-time, even while you are editing them. For more information see this page.

Creating Variables for the States

For a state to modify a variable, it must exist as a variable in the Entity. Our States will initially modify two variables:

  1. The Rock Sprite’s Texture
  2. The Rock Collision’s Radius

Therefore, we need to create these two variables. We will “tunnel” to these two variables. To do this:

  1. Right-click on the Variables item under Rock
  2. Select “Add Variable”
  3. Select “Tunnel a variable in a contained object”
  4. Select “Sprite” as the object
  5. Select “Texture” as the Variable
  6. Repeat the steps above, but instead select “CircleInstance” as the Object and “Radius” as the variable

Setting the State variables

Next we will set the State variables. These variables represent the current state of the Rock when it has a given State selected. Therefore, States with larger Textures will also have a larger Collision Radius.

To set the State values:

  1. Select the Size1 State in Rock
  2. Set SpriteTexture to “Rock1”
  3. Set CollisionRadius to 6 (this is the value we’ve been using so far prior to having made any states)
  4. Select the Size2 State in Rock
  5. Set SpriteTexture to “Rock2”
  6. Set CollisionRadius to 12
  7. Select the Size3 State in Rock
  8. Set SpriteTexture to “Rock3”
  9. Set CollisionRadius to 20
  10. Select the Size4 State in Rock
  11. Set SpriteTexture to “Rock4”
  12. Set CollisionRadius to 30

Creating Large Rocks Initially

Rocks in Rock Blaster will default to State4 – the largest. Once they are shot they will break down into smaller rocks – much like the original Asteroids game.

We can change the default size of Rocks with one line of code. To do this:

  1. Switch to Visual Studio
  2. Open RockSpawner.cs
  3. Find the PerformSpawn function
  4. Add the following line of code after Rock rock = RockFactory.CreateNew();

If you now play the game, the rocks will be very large:RockBlasterLargeRocks.png

Breaking Rocks

Currently when a Rock collides with a Bullet the game calls Destroy on the Rock instance. Instead, we will want the Rock to decide whether it should break up into smaller rocks before it is destroyed.

First, let’s replace the Destroy call with a TakeHit call:

  1. Open GameScreen.cs in Visual Studio
  2. Navigate to the BulletVsRockCollisionActivity function
  3. Change Destroy to TakeHit. Your code should look like:

Next we’ll create a TakeHit function:

  1. Open Rock.cs
  2. Add the following code:

Finally, we’ll create a BreakIntoPieces function in Rock.cs:

Defining Breaking Variables

Notice that we are using two variables which haven’t been defined:

  1. NumberOfRocksToBreakInto
  2. RandomSpeedOnBreak

Although we could have added these variables directly into code, it’s much better to escalate these variables to Glue variables so that they can be easily modified at a later time. To do this:

  1. In Glue, expand the Rock Entity
  2. Right-click on its Variables item
  3. Select “Add Variable”
  4. Select the “Create a new variable” option
  5. Select the “int” type
  6. Enter the name “NumberOfRocksToBreakInto” and click OK
  7. Set NumberOfRocksToBreakInto’s DefaultValue to 2
  8. Right-click on Variables again
  9. Select “Add Variable”
  10. Select the “Create a new variable” option
  11. Enter the name “RandomSpeedOnBreak” and click OK
  12. Set RandomSpeedOnBreak’s DefaultValue to 50


RockBlasterRocksBrokenUp.pngIf you now run your game and shoot the rocks, you will see that they will break up into smaller pieces. Our game is becoming far more playable (and difficult). Next we’ll add a HUD and logic for scoring.

<- 07. Collision09. Hud ->