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 theinstance. (To be precise, they indicate the place where the origin of the sprite isplaced. Position (0,0) is the top-left corner of the room. You can change theposition of the instance by changing its x and y variables. If you want theobject to make complicated motions this is the way to go. You typically putthis code in the step event for the object.
If the object moves with constant speed and direction, there is an easierway 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 horizontalspeed mean a motion to the left. Positive vertical speed is downwards andnegative vertical speed is upwards. So you have to set these variables onlyonce (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 setand read these variables to specify an arbitrary motion. (Internally thisis changed into values for hspeed and vspeed.) Alsothere is the friction and the gravity and gravity direction. Finally,there is the function motion_add(dir,speed) to add a motion tothe current one.
To be complete, each instance has the following variables and functions dealing withits 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).
There are a large number of functions available that help you in defining your motions:
place_free(x,y) Returns whether the instance placed at position(x,y) is collision-free.This is typically used as a check before actually moving to the new position.
place_empty(x,y) Returns whether the instance placed at position (x,y) meets nobody.So this function takes also non-solid instances into account.
place_meeting(x,y,obj) Returns whether the instance placed at position (x,y) meets obj.obj can be an object in which case the function returns true is someinstance of that object is met. It can also be an instance id, thespecial word all meaning an instance of any object, or the special word other.
place_snapped(hsnap,vsnap) Returns whether the instance is aligned with the snapping values.
move_random(hsnap,vsnap) Moves the instance to a free random, snapped position, like the corresponding action.
move_snap(hsnap,vsnap) Snaps the instance, like the corresponding action.
move_wrap(hor,vert,margin) Wraps the instance when it has left the room to the other side. horindicates whether to wrap horizontaly and vert indicates whetherto wrap vertically. margin indicates how far the origin of theinstance must be outside the room before the wrap happens. So it is amargin around the room. You typically use this function in the Outsideevent.
move_towards_point(x,y,sp) Moves the instances with speed sp toward position (x,y).
move_bounce_solid(adv) Bounces against solid instances, like the corresponding action. advindicates whether to use advance bounce, that also takes slanted walls into account.
move_bounce_all(adv) Bounces against all instances, instead of just the solid ones.
move_contact_solid(dir,maxdist) Moves the instance in the direction until a contact position with a solidobject is reached. If there is no collision at the current position, theinstance is placed just before a collision occurs. If there already is acollision the instance is not moved. You can specify the maximal distanceto move (use a negative number for an arbitrary distance).
move_contact_all(dir,maxdist) Same as the previous function but this time you stop at a contact withany object, not just solid objects.
move_outside_solid(dir,maxdist) Moves the instance in the direction until it no longer lies within a solid object.If there is no collision at the current position the instance is not moved.You can specify the maximal distance to move (use a negative number for anarbitrary distance).
move_outside_all(dir,maxdist) Same as the previous function but this time you move until outside any object,not just solid objects.
distance_to_point(x,y) Returns the distance of the bounding box of the current instance to (x,y). (If the instancedoes not have a sprite or mask, the result of the function is undefined.)
distance_to_object(obj) Returns the distance of the instance to the nearest instance of object obj. (If the instanceor object does not have a sprite or mask, the result of the function is undefined.)
position_empty(x,y) Returns whether there is nothing at position (x,y).
position_meeting(x,y,obj) Returns whether at position (x,y) there is an instance obj. obj can be an object, an instanceid, or the keywords self, other, or all.
« Game play | Paths »