Game Maker

Lasers

 

 
At school we have a policy of non violence, even in our games!

In this exercise we are going to have a monster beam an icy-laser.

Create a new game with a laser and monster sprite and a laser and monster object.

Place the monster object in a room.

 

Open the properties window for the Monster object.

Add a keypress event for when the space bar is pressed. Take care not to use a Keyboard Event! Choose the Main1 tab, and choose the 'Create an Instance of an Object'

Select the following options:

We are creating a laser at the same x coordinate as the monster, but 2 pixels above the monster.  Ticking relative means the coordinates are added to the coordinates of the monster.   (This will make more sense later)

 

Now we need to make the laser move.

Open the properties window for the objLaser.

Add a create event.

From the movement tab, choose Set the Vertical Speed

Set the speed to be -6  (Making the vertical speed -6 means the laser will move upwards)

 
 There is one more thing we need to do.  After the laser disappears from the play area, even though we can't see it, it is still in the game moving upwards forever.  The computer keeps track of all the lasers, so if we don't destroy them after they leave the play area, our game will get slower and slower as the computer tries to keep track of all the lasers.

We can fix this by adding an event to destroy the laser after it leaves the play area.

Open the properties for objLaser.

Add an 'Other' Event > Choose Outside Room

From the Main1 tab, choose Destroy the Instance

  Now try your game.  Press spacebar to beam the laser.

There is still a long way to go before our game is much good to play, but we are on the way!

Activity 1

Make a laser move across the screen until it is outside the play area then destroy it.

Hint:  You will need to set the horizontal speed, not the vertical speed, for the laser.

Save it as moveacross

Activity 2

Try setting both the horizontal and vertical speeds.  What do you think will happen?

Activity 3

Make the laser beam in 8 directions, depending on which key is pressed.  For example, pressing the A key will beam the laser left, S key will beam the laser left and upwards, D key will beam the laser straight upwards etc.

Activity 4

How could you make the laser beam in even more directions?

 

Making an explosion when the laser hits something

When a laser hits something you may want to make an explosion.  This is how you do it.

Make a new game. Add laser and animated explosion sprites and objects to your game

Place the laser at the bottom of the room and make it move up the screen:

We want the laser to explode when it is 50 pixels from the top of the screen. 

Save it as explosion

 

 

Blocks of Code

You will notice in the code above the actions for starting and ending a block of code

This means that if the question at the top is true, the entire block of code (all the actions) will be executed, otherwise if the question is false, none of it will be executed.

Activity 5

Make an object move across the screen in the top half of the play area.

Have another object at the bottom of the screen (say a plane).  When the spacebar is pressed the plane should beam a laser at the moving object.  If it hits it, then the plane and laser should explode and points should be scored.  If the laser misses, it should be destroyed when it leaves the play area.

Extension: Add events to move the plane sideways when the left and right keys are pressed.

Save it as laser

 

 

Changing Direction of Sprites

Sometimes you have a sprite that is facing in a particular direction, for example this plane appears to be flying upwards:

When you change directions to fly left, it does not look right, so you need a sprite that looks like this:

To do this you must first create the sprites to face in each of the directions.  This video will show you how:

When using multiple sprites for the same object you should turn off Precise Collision Checking and the Bounding box should be set to manual.  The bounding box should be identical for all sprites.   The bounding box does not have to surround all pixels of the sprite, it is just the area that is checked for collisions.

Otherwise when the sprite is changed, the object may become stuck.

Once you have created the sprites, you need to set up events to change the sprite for the object whenever the left, right, up or down keys are pressed.  This video will show you how:

Often you want to stop your object to stop  moving when you are no longer pressing a key and you would like the object to change to a 'stopped' sprite.  This video will show you how:

Add a stopped sprite into the activity above.

Beaming a laser in the Direction You are Facing

It can be quite challenging to make your character beam in the direction it is facing.

Here is an example you can download that will show you how it is done.  Read the game information to see how to set it up.