# Moving around

Obviously, an important aspect of games is the moving around of object instances. Each instance has two built-in variables x and y that indicate the position of the instance. (To be precise, they indicate the place where the origin of the sprite is placed. Position (0,0) is the top-left corner of the room. You can change the position of the instance by changing its x and y variables. If you want the object to make complicated motions this is the way to go. You typically put this code in the step event for the object.

If the object moves with constant speed and direction, there is an easier way to do this. Each object instance has a horizontal speed (hspeed) and a vertical speed (vspeed). Both are indicated in pixels per step. A positive horizontal speed means a motion to the right, a negative horizontal speed mean a motion to the left. Positive vertical speed is downwards and negative vertical speed is upwards. So you have to set these variables only once (for example in the creating event) to give the object instance a constant motion.

There is quite a different way for specifying motion, using a direction (in degrees 0-359), and a speed (should be non-negative). You can set and read these variables to specify an arbitrary motion. (Internally this is changed into values for hspeed and vspeed.) Also there is the friction and the gravity and gravity direction. Finally, there is the function motion_add(dir,speed) to add a motion to the current one.

## Motion properties and methods

To be complete, each instance has the following variables and functions dealing with its position and motion:

x   Its x-position. y   Its y-position. xprevious   Its previous x-position. yprevious   Its previous y-position. xstart   Its starting x-position in the room. ystart   Its starting y-position in the room. hspeed   Horizontal component of the speed. vspeed   Vertical component of the speed. direction   Its current direction (0-360, counter-clockwise, 0 = to the right). speed   Its current speed (pixels per step). friction   Current friction (pixels per step). gravity   Current amount of gravity (pixels per step). gravity_direction   Direction of gravity (270 is downwards). motion_set(dir,speed)   Sets the motion with the given speed in direction dir. motion_add(dir,speed)   Adds the motion to the current motion (as a vector addition).