05. Main Ship Behavior


At this point we have a ship which is visible on Screen, but it doesn’t do anything. This tutorial will add behavior to our MainShip Entity so that it can move, turn, and shoot.

Adding Variables

The MainShip movement will be to continually move forward at a constant speed, and to give the user the ability to turn left and right.

Before we begin writing any code we’ll create two variables in our MainShip: MovementSpeed and TurningSpeed. To do this:

  1. Expand the MainShip Entity in Glue
  2. Right-click on Variables
  3. Select “Add Variable”
  4. Select the “Create a new variable” option
  5. Leave the type as “float”
  6. Enter the name MovementSpeed
  7. Repeat the steps above to add a TurningSpeed variable

Next let’s give the variables some default values:

  1. Select MovementSpeed
  2. Change the DefaultValue to 100. This is the number of pixels the MainShip will travel in one second.
  3. Select TurningSpeed
  4. Change the value to 3.14. This is the maximum number of radians the MainShip will rotate in one second.

Applying Movement

To apply movement we will need to write some C# code. To do this:

  1. Open the project in Visual Studio (or switch to Visual Studio if you already have it open)
  2. Navigate to MainShip.cs. The folder structure will likely be:Entities\MainShip.cs
  3. Scroll to the CustomActivity method

Add the following line of code in CustomActivity:


Next add the MovementActivity to your class (outside of any functions). The code should look like this:

void MovementActivity()
   this.Velocity = this.RotationMatrix.Up * this.MovementSpeed;

If you now run the game you will see the ship move upward, then eventually move off-screen.

What is “RotationMatrix.Up”?: If you are unfamiliar with the RotationMatrix property, or with matrices in general then you may be wondering what the RotationMatrix.Up variable is, and why we’re using it.

The RotationMatrix property contains information about how an Entity, Sprite, or any other PositionedObject is rotated. One of the pieces of information that this property contains is an Up value. The Up value tells you which way is up for the object. This value is in “object space” meaning that if the object rotates, then this value will rotate along with the object. This is especially convenient for this tutorial because this game will have the ships always moving forward. This means that the code above will work regardless of which way the MainShip is rotated – something which we’ll see in the coming sections.

Assigning a GamePad

The next step is to assign a GamePad so that this Entity can turn. This approach will be very similar to the approach taken in BeefBall, so I will list the steps without explaining what they do. If you need a refresher, see the Beefball Controlling an Entity tutorial.

To add controls to your MainShip:

  • Add the following variable to MainShip.cs outside of any function:
Xbox360GamePad mGamePad;
  • Add the following to CustomInitialize:
// This is temporary, eventually we'll want to assign this
// based off of the PlayerIndex variable
mGamePad = InputManager.Xbox360GamePads[0];
  • Add the following to CustomActivity:
  • Add the TurningActivity function to MainShip.cs:
 void TurningActivity()
   // We use a negative value because we want holding left to turn
   // counterclockwise - but left is a negative value and turning counterclockwise 
   // is a positive value
   this.RotationZVelocity = -mGamePad.LeftStick.Position.X * TurningSpeed;

Note that all of the code we have written uses coefficients (MovementSpeed and TurningSpeed) defined in Glue. This means that you can modify these values in the MainShip Entity at any time if you want to tune how the game feels.

No Xbox360 controller available?That’s okay, you can remap input so that you can use a keyboard as an Xbox360GamePad. For information on how to do this, see the ButtonMap property on the Xbox360GamePad class.

Defining the Bullet Entity

Next we will give the Bullet a PNG file and a Sprite to use. This process is essentially the same as when we added PNG files and a Sprite to our MainShip Entity so you may find these steps familiar.

To add the PNG:

  1. Download the following file to your computer:Bullet1.png
  2. Expand the Bullet entity in Glue
  3. Right-click on the Files item under Bullet
  4. Select “Add File”->”Existing File”
  5. Navigate to where you saved Bullet1.png

To add the Sprite:

  1. Right-click on the Objects item under Bullet
  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 PixelSize to .5 (this makes our Sprite sized according to the source texture.
  7. Use the Texture dropdown to select “Bullet1”


The next step is to add firing bullets. One problem which we encounter at this point is how to pass the bullets from our MainShip into the BulletList within our GameScreen. Fortunately, Glue has functionality built specifically for this scenario.

First we will mark the Bullet Entity as an object which is created by other Entities. To do this:

  1. Select the Bullet Entity in Glue
  2. Find the “CreatedByOtherEntities” property and set it to True


This property does a number of things, but most importantly it creates a class called static BulletFactory which we can use to create new Bullet instances in MainShip. When the BulletFactory is used, if the current Screen has a list of a Bullets then newly-created Bullets will automatically be added to this list for us.

If you are interested in reading more about CreatedByOtherEntities and the associated Factories, see the tutorial on this topic.

Next we will need to define a bullet speed. To do this:

  1. Expand the Bullet entity in Glue
  2. Right-click on Variables
  3. Select “Add Variable”
  4. Select the “Create a new variable” option
  5. Enter the name “MovementSpeed”
  6. Set the DefaultValue to 300

Now we can use BulletFactory to create bullets when the player shoots. To do this:

  • Go to MainShip.cs in Visual Studio
  • Add the following code to CustomActivity:
  • Add the ShootingActivity method to your class:
private void ShootingActivity()
    if (mGamePad.ButtonPushed(Xbox360GamePad.Button.A))
        // We'll create 2 bullets because it looks much cooler than 1
        Bullet firstBullet = BulletFactory.CreateNew();
        firstBullet.Position = this.Position;
        firstBullet.Position += this.RotationMatrix.Up * 12;
        // This is the bullet on the right side when the ship is facing up.
        // Adding along the Right vector will move it to the right relative to the ship
        firstBullet.Position += this.RotationMatrix.Right * 6;
        firstBullet.RotationZ = this.RotationZ;
        firstBullet.Velocity = this.RotationMatrix.Up * firstBullet.MovementSpeed;
        Bullet secondBullet = BulletFactory.CreateNew();
        secondBullet.Position = this.Position;
        secondBullet.Position += this.RotationMatrix.Up * 12;
        // This bullet is moved along the Right vector, but in the nevative
        // direction, making it the bullet on the left.
        secondBullet.Position -= this.RotationMatrix.Right * 6;
        secondBullet.RotationZ = this.RotationZ;
        secondBullet.Velocity = this.RotationMatrix.Up * secondBullet.MovementSpeed;

Error The name ‘BulletFactory’ does not exist in the current context:If you are seeing an error about BulletFactory it is because you have not “fully qualified” the BulletFactory class. This basically means that we either need the full name of the BulletFactory, or we need to have a “using” statement to tell Visual Studio where to find BulletFactory. Fortunately, this is easy to do. For information on how to qualify types, see this page.

Notice that we are using the RotationMatrix of the MainShip to adjust the initial positions of the bullets, as well as their velocity

If you run the game you should be able to fly, turn, and shoot.


Although we have a long way to go, this is a big milestone for Rock Blaster. You can now see how the game feels for the very first time. Since relevant coefficients are set in Glue, you can change the values to make the game feel differently.

Now that we can shoot bullets we’ll need something to shoot at. The next tutorial will add the Rock Entity.

<- 04. Main Ship Entity06. Rock Entity ->