GAME MAKER STUDIO TUTORIAL
MINI TUTORIAL 04 – MOVING FORWARD

separator-blank.png

DESCRIPTION

·         In this mini tutorial, you will learn how to make an object move forward in the direction that it is facing.

RESOURCES

·         Click here.

STEPS

GameMaker Version 1.4.1749

 

1 – Creating our sprite

·         Create the following sprite

o   Name: spr_car

o   Image: car.png (from the resources folder)

Note on angles:

GameMaker uses the following angles for direction:

·         0 is facing right

·         90 is facing up

·         180 is facing left

·         270 is facing down

·         Of course, all angles in between also exist.  So 45 is facing right and up.

GameMaker has two built-in variables that relate to angles.  First, the direction of an object states is where the object is facing.  If the object has a speed, it will move in that direction.

 

Secondly, objects also have an image_angle variable.  Its initial angle is zero.  If we want to make the sprite change angles, we simply change the value of image_angle.  Note that this will note change the sprite's movement though.

 

By having sprites face the right, we assure that their image_angle is in fact going to be the same as their direction.  This simply makes things simpler to code.  So notice the car image is facing right.

 

2 – Creating a car object

·         Create an object

o   Name: obj_car

o   Choose the corresponding sprite.

3 – Creating our room

·         Create a room named room_main.

·         Select the width and height..

·         Under the objects tab, add a car to our room.

4 – Rotating left

·         Inside the car object, add a Keyboard (<left>) event.

·         Inside the above event, add a Set Variable action (control tab).

o   Variable: direction

o   Value: 5

o   Relative: Checked

·         Still inside the above event, add another Set Variable action (control tab).

o   Variable: image_angle

o   Value: 5

o   Relative: Checked

 

Save and test your game.  Your car should now rotate left when you hit the left arrow key.

 

5 – Rotating right

·         Do the same thing as in the previous step.  However, the value of the angles should change by -5 instead.

Save and test your game.  Your car should now rotate both left and right.

 

6 – Moving forward (Accelerating)

·         Inside the car object, add a Keyboard (<up>) event.

·         Inside that event, add a Set Variable action (control tab).

o   Variable: speed

o   Value: 0.2

o   Relative: Checked

Save and test your game.  Your car should now move forward and turn with the arrow keys.

 

7 – Slowing down (Decelerating)

 

·         Inside the car object, adda Keyboard (<down>) event. 

·         Inside that event, add a Set Variable action (control tab).

o   Variable: speed

o   Value: -0.2

o   Relative: Checked

Save and test your game.  Your car should now be able to move and slow down.  It will also go backwards.

 

8 – Maximum speed

·         You can cap your speed to 5 by adding a Test Variable action inside the Keyboard (<up>) event.  Now, we only increase the speed if it is less than 5.

 

Save and test your game.  Your car should now cap off at 5 pixels per step.  If that's too slow, change the Test Variable action above to with the 5 to a value of 10, or whatever you want.

 

9 – Maximum reverse speed

·         Changing the Keyboard (<down>) event to the setup below will cap the reverse speed to 2 pixels per step. 

 



Discussion
Note that if the Test Condition above checked that speed was greater than 0 (instead of -2), then that would stop all movement in reverse.  The down arrow would now effectively be a braking system.

 

Save and test your game.  You now have maximum speeds in both forward and reverse direction.

 

SOURCE

  • Straight from Mr. Campeau's brain.  J

separator-campeau.png