Files
QtGameMaker/help/files/403_01_moving.html
T

189 lines
7.5 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Moving around</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body background="images/back.gif">
<!--START-->
<h3>Moving around</h3>
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.
<p>
If the object moves with constant speed and direction, there is an easier
way to do this. Each object instance has a horizontal speed (<tt>hspeed</tt>)
and a vertical speed (<tt>vspeed</tt>). 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.
<p>
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 <tt>hspeed</tt> and <tt>vspeed</tt>.) Also
there is the friction and the gravity and gravity direction. Finally,
there is the function <tt>motion_add(dir,speed)</tt> to add a motion to
the current one.
<p>
To be complete, each instance has the following variables and functions dealing with
its position and motion:
<p>
<blockquote>
<tt><b>x</b></tt>
Its x-position.<br>
<tt><b>y</b></tt>
Its y-position.<br>
<tt><b>xprevious</b></tt>
Its previous x-position.<br>
<tt><b>yprevious</b></tt>
Its previous y-position.<br>
<tt><b>xstart</b></tt>
Its starting x-position in the room.<br>
<tt><b>ystart</b></tt>
Its starting y-position in the room.<br>
<tt><b>hspeed</b></tt>
Horizontal component of the speed.<br>
<tt><b>vspeed</b></tt>
Vertical component of the speed.<br>
<tt><b>direction</b></tt>
Its current direction (0-360, counter-clockwise, 0 = to the right).<br>
<tt><b>speed</b></tt>
Its current speed (pixels per step).<br>
<tt><b>friction</b></tt>
Current friction (pixels per step).<br>
<tt><b>gravity</b></tt>
Current amount of gravity (pixels per step).<br>
<tt><b>gravity_direction</b></tt>
Direction of gravity (270 is downwards).<br>
<tt><b>motion_set(dir,speed)</b></tt>
Sets the motion with the given speed in direction dir.<br>
<tt><b>motion_add(dir,speed)</b></tt>
Adds the motion to the current motion (as a vector addition).<br>
</blockquote>
<p>
There are a large number of functions available that help you in defining your motions:
<p>
<blockquote>
<tt><b>place_free(x,y)</b></tt>
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.<br>
<tt><b>place_empty(x,y)</b></tt>
Returns whether the instance placed at position (x,y) meets nobody.
So this function takes also non-solid instances into account.<br>
<tt><b>place_meeting(x,y,obj)</b></tt>
Returns whether the instance placed at position (x,y) meets obj.
obj can be an object in which case the function returns true is some
instance of that object is met. It can also be an instance id, the
special word all meaning an instance of any object, or the special word other.<br>
<tt><b>place_snapped(hsnap,vsnap)</b></tt>
Returns whether the instance is aligned with the snapping values.<br>
<tt><b>move_random(hsnap,vsnap)</b></tt>
Moves the instance to a free random, snapped position, like the corresponding action.<br>
<tt><b>move_snap(hsnap,vsnap)</b></tt>
Snaps the instance, like the corresponding action.<br>
<tt><b>move_wrap(hor,vert,margin)</b></tt>
Wraps the instance when it has left the room to the other side. <tt>hor</tt>
indicates whether to wrap horizontaly and <tt>vert</tt> indicates whether
to wrap vertically. <tt>margin</tt> indicates how far the origin of the
instance must be outside the room before the wrap happens. So it is a
margin around the room. You typically use this function in the Outside
event.<br>
<tt><b>move_towards_point(x,y,sp)</b></tt>
Moves the instances with speed sp toward position (x,y).<br>
<tt><b>move_bounce_solid(adv)</b></tt>
Bounces against solid instances, like the corresponding action. adv
indicates whether to use advance bounce, that also takes slanted walls into account.<br>
<tt><b>move_bounce_all(adv)</b></tt>
Bounces against all instances, instead of just the solid ones.<br>
<tt><b>move_contact_solid(dir,maxdist)</b></tt>
Moves the instance in the direction until a contact position with a solid
object is reached. If there is no collision at the current position, the
instance is placed just before a collision occurs. If there already is a
collision the instance is not moved. You can specify the maximal distance
to move (use a negative number for an arbitrary distance).<br>
<tt><b>move_contact_all(dir,maxdist)</b></tt>
Same as the previous function but this time you stop at a contact with
any object, not just solid objects.<br>
<tt><b>move_outside_solid(dir,maxdist)</b></tt>
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 an
arbitrary distance).<br>
<tt><b>move_outside_all(dir,maxdist)</b></tt>
Same as the previous function but this time you move until outside any object,
not just solid objects.<br>
<tt><b>distance_to_point(x,y)</b></tt>
Returns the distance of the bounding box of the current instance to (x,y). (If the instance
does not have a sprite or mask, the result of the function is undefined.)<br>
<tt><b>distance_to_object(obj)</b></tt>
Returns the distance of the instance to the nearest instance of object obj. (If the instance
or object does not have a sprite or mask, the result of the function is undefined.)<br>
<tt><b>position_empty(x,y)</b></tt>
Returns whether there is nothing at position (x,y).<br>
<tt><b>position_meeting(x,y,obj)</b></tt>
Returns whether at position (x,y) there is an instance obj. obj can be an object, an instance
id, or the keywords <tt>self</tt>, <tt>other</tt>, or <tt>all</tt>.<br>
</blockquote>
<!--END-->
</body>
</html>
<!-- KEYWORDS
motion
distance
moving
instance variables
positions
x
y
xprevious
yprevious
xstart
ystart
hspeed
vspeed
direction
speed
friction
gravity
gravity_direction
motion_set()
motion_add()
place_free()
place_empty()
place_meeting()
place_snapped()
move_random()
move_snap()
move_wrap()
move_towards_point()
move_contact_solid()
move_contact_all()
move_outside_solid()
move_outside_all()
move_bounce_solid()
move_bounce_all()
distance_to_point()
distance_to_object()
position_empty()
position_meeting()
-->