GAME MAKER STUDIO TUTORIAL
MINI TUTORIAL 03 – SHOOTING AT CURSOR

separator-blank.png

DESCRIPTION

·         In this mini tutorial, you will make an object shoot when the spacebar is clicked.  Afterwards, you will learn how to direct the shooting at the cursor.

RESOURCES

·         Click here for some intense graphics.

STEPS

GameMaker Version 1.4.1749

 

1 – Creating our sprites

·         Create the following 3 sprites

o   spr_platform

o   spr_bullet

o   spr_ground

·         For each sprite above, load the corresponding image from the resource folder.  You can choose the grey or red bullet image.

2 – Creating our objects

·         Create the following 3 objects

o   obj_platform

o   obj_bullet

o   obj_ground

·         For each object, select the corresponding sprite.

3 – Creating our room

·         Create a room named room_main.

·         Select the width and height.  I used 640 x 544.

·         Under the objects tab, choose ground and fill the bottom row of the room with ground.

·         Delete one of the ground objects (near the middle) and place a platform object.

4 – Making bullets appear

·         Inside the platform object, add a Key Released event (<space>).

·         Inside the above event, add a Create Instance action (main1 tab).

o   Object: obj_bullet

o   X:0

o   Y:0

o   Relative: Checked

Note:

·         The above event/action will make it so that when you hit the spacebar, a bullet will appear where the platform is located.  For now, it won't be perfectly placed but we'll fix that later.

5 – Making bullets move

·         Inside the bullet object, add a Create event.

·         Inside that event, add a Speed Vertical action (move tab)

o   Vertical Speed: -5

Note:

·         A negative vertical speed signifies moving upwards.  So our bullets will now move 5 pixels per step in the upwards direction.

 

Save and test your game.  When you hit SPACE, bullets should be shot from your platform.  Sorta.

 

Discussion: We have to figure out a better start location for the bullets.  Essentially, we want the bullets to appear over the middle of the platform.  And, seeing the platform image is 32 by 32, the middle is therefore at (16,16) in the image.

 

6 – Moving bullets start locations over

·         Go inside the platform object's Key Release event.

·         Inside the Create Instance action, change the X and Y so they are both 16.  Keep relative checked.

 

7 – Centering our bullet

·         Go inside the bullet sprite.  Under Origin, hit Center.

Save and test your game.  The bullets should now appear at a good starting location.

 

8 – Shooting at the mouse cursor

·         Inside the bullet object's Create event, remove the Speed Vertical action.

·         Still inside the Create event, add a Move Towards action (move tab).

o   X: mouse_x

o   Y: mouse_y

o   Speed: 5

Note:

Both mouse_x and mouse_y are variables that hold the value of the cursor's x and y location.

 

SOURCE

  • Straight from Mr. Campeau's brain.  J

separator-campeau.png