hspeed (Game Maker)
hspeed
( Game Maker 6.1, 7 )
hspeed - horizontal speed of an instance.
Description
hspeed is a local variable that contains the current horizontal speed of a an instance, in pixels per step. A positive hspeed will cause the instance to move right and a negative hspeed will cause the instance to move left.
Examples
Example of Use
hspeed = -4;
In this example, the instance will move 4 pixels left every step.
Limiting hspeed
if(abs(hspeed) > 20)
{
hspeed = 20;
}
If this is placed in a Step event, it will limit an object's hspeed to 20.
Detecting Left/Right Movement
if(hspeed = abs(hspeed))
{
//instance is moving right
}
else
{
//instance is moving left
}
The code in this example will tell you if an instance is moving left or right during runtime.
Reversing hspeed
You can reverse the hspeed by multiplying it by -1.
hspeed *= -1;
Useful Info
- direction, if it is not set manually, is calculated by using the hspeed and vspeed of an object. If the hspeed and vspeed of an object is 0, then direction will be "calculated" to 0. This causes a few problems as 0 is right, not "no direction".
- Changing an object's x/y-position is considered "teleporting" by Game Maker and will not influence or change hspeed.
Related Pages
Variables
- direction - the direction an object is moving, in degrees.
- speed - an objects speed in relation to direction.
- vspeed - the vertical equivalent of hspeed
Actions
page revision: 17, last edited: 06 Aug 2012 17:33