Difference between revisions of "Screen Shake"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "In order to complete this tutorial you will need a working monogame project, it will need to draw something to be able to see the screen shake. ==Variables== Within the Game1...")
 
Line 8: Line 8:
 
Vector2 shakeOffset = new Vector2(15,0);
 
Vector2 shakeOffset = new Vector2(15,0);
 
int shakeCount = 0;
 
int shakeCount = 0;
 +
in maxShakes = 10;
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
The boolean will set when the screen should shake, the vector2 called shakeOffset will be the direction and amount of shake (15 is the X value and 0 is the Y value). Finally shakeCount will be incremented so we can stop the screen shake when we hit a number of shakes.
 +
 +
==Update Method==
 +
Now in the update method we need to have something to control when to shake, the code below uses the 'S' key. The shake is achieved by multiplying the shakeOffset by -1. This will alternate the shakeOffset between (15,0) and (-15,0):
 +
 +
<syntaxhighlight lang=c#>
 +
            if (Keyboard.GetState().IsKeyDown(Keys.S))
 +
                shakeScreen = true;
 +
 +
            if (shakeScreen && shakeCount<maxShakes)
 +
            {
 +
                shakeOffset *= -1;
 +
                shakeCount++;
 +
            }
 +
            else
 +
            {
 +
                shakeCount = 0;
 +
                shakeScreen = false;
 +
            }
 +
</syntaxhighlight>
 +
 +
We also use shakeCount and MaxShakes to get the same number of shakes every time. The code above also switches the shakeScreen boolean back to false. So pressing the 'S' key once will do the full number of shakes and then stop.
 +
 +
==Draw Method==
 +
We are going to use the shakeOffset and apply it to the spriteBatch when the shakeScreen boolean is true:
 +
<syntaxhighlight lang=c#>
 +
if (shakeScreen)
 +
    spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null, Matrix.CreateTranslation(shakeOffset.X, shakeOffset.Y, 0));
 +
else
 +
    spriteBatch.Begin();
 +
</syntaxhighlight>
 +
If we want to shake the screen we use one spriteBatch.Begin() which applies a translation offset, and if we do not want a shake we use just a normal spriteBatch.Begin().

Revision as of 11:42, 9 February 2019

In order to complete this tutorial you will need a working monogame project, it will need to draw something to be able to see the screen shake.

Variables

Within the Game1.cs class, add the following variables with your existing ones:

bool shakeScreen = false;
Vector2 shakeOffset = new Vector2(15,0);
int shakeCount = 0;
in maxShakes = 10;

The boolean will set when the screen should shake, the vector2 called shakeOffset will be the direction and amount of shake (15 is the X value and 0 is the Y value). Finally shakeCount will be incremented so we can stop the screen shake when we hit a number of shakes.

Update Method

Now in the update method we need to have something to control when to shake, the code below uses the 'S' key. The shake is achieved by multiplying the shakeOffset by -1. This will alternate the shakeOffset between (15,0) and (-15,0):

            if (Keyboard.GetState().IsKeyDown(Keys.S))
                shakeScreen = true;

            if (shakeScreen && shakeCount<maxShakes)
            {
                shakeOffset *= -1;
                shakeCount++;
            }
            else
            {
                shakeCount = 0;
                shakeScreen = false;
            }

We also use shakeCount and MaxShakes to get the same number of shakes every time. The code above also switches the shakeScreen boolean back to false. So pressing the 'S' key once will do the full number of shakes and then stop.

Draw Method

We are going to use the shakeOffset and apply it to the spriteBatch when the shakeScreen boolean is true:

if (shakeScreen)
    spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null, Matrix.CreateTranslation(shakeOffset.X, shakeOffset.Y, 0));
else
    spriteBatch.Begin();

If we want to shake the screen we use one spriteBatch.Begin() which applies a translation offset, and if we do not want a shake we use just a normal spriteBatch.Begin().