0

Note: I'm using GameMaker 1.4, not 2. I don't know if this makes a difference, but I'm pointing it out just in case.

Okay, so I'm having an issue in my GameMaker game where I'm making an object move up and down in relation to image_angle. For some reason, it really wants to move across the y axis as normal and completetly disregards the image_angle. This is really annoying and could change the game entirely if not fixed.

My code is for the step event is:

// Mouse controls.
image_angle = point_direction(x, y, mouse_x, mouse_y);

if(keyboard_check(ord('W')))
{
    y -= playerSpeed;
}

if(keyboard_check(ord('S')))
{
    y += playerSpeed;
}

My code for the create event is:

globalvar fuelRemaining;
fuelRemaining = 60;

playerSpeed = 3;
MobyCoding
  • 3
  • 1
  • 5

3 Answers3

0
image_angle = point_direction(x, y, mouse_x, mouse_y);
direction = image_angle;

if(keyboard_check(ord('W'))){
    speed = -playerSpeed;
}
if(keyboard_check_release(ord('W'))){
    speed = 0;
}

if(keyboard_check(ord('S'))){
    speed = playerSpeed;
}
if(keyboard_check_release(ord('S'))){
    speed = 0;
}
Gamio
  • 1
  • note: i'm only paying attention to the top 2 lines because that's all i really need... it doesn't work still... – MobyCoding Jul 08 '17 at 21:48
0

@Gamio code is almost correct (should be _released instead of _release, confused the sign for W and S keys). Here are few improvements:

image_angle = point_direction(x, y, mouse_x, mouse_y);
direction = image_angle;

if point_distance(x, y, mouse_x, mouse_y) < spd
{
   speed = 0;
}
else
{
    var spd = 0;

    if keyboard_check(ord("W")) 
        spd += playerSpeed; // If pressed both W and S, then will stop 

    if keyboard_check(ord("S"))
        spd -= playerSpeed;

    speed = spd;

    if keyboard_check_released(ord("W")) or keyboard_check_released(ord("S"))
        speed = 0;
}

Place the code in Step End event (not Step).

If you want change coords directly, without speed, it is also possible, but little more difficult (you need calculate both x and y deltas using lengthdir_x and lengthdir_y functions and then add these deltas to current coords).

Dmi7ry
  • 1,777
  • 1
  • 13
  • 25
  • well, im not sure if its necessary both to generate var spd here, and also why to check if key has been released 2* each step, which i solved in my ansver differently, and here wont be necessary with proper usage of spd. also when player presses both keys at once then releases only one (in case player switches keys often such scenarios may happen often, without players notice), also you say its necessary to use this code in end step event which seems to not be true, and if it should prevent anything then you should specify the problem a little i guess. –  Jul 19 '17 at 19:16
  • @VojtěchŠvrček It's not a very good solution, it's just improvements of Gamio's code. – Dmi7ry Jul 20 '17 at 05:43
  • thought so much, just thinking about people starting with game maker and how could it end up :p. but well, it works and if they just copy paste codes into project then of course its gonna be a mess. –  Jul 20 '17 at 10:14
0

In your code, you set image_angle and direction, but then change only y coordinate so of course you wont see any changes to x coordinate.

You can use built-in direction and speed variables and game-maker will move any object automatically, or you can apply lengthdir_x and lengthdir_y functions on x and y coordinates.

direction and speed example:

//create event

playerSpeed = 3;

//step event

image_angle = point_direction(x, y, mouse_x, mouse_y);
direction = image_angle;
speed = 0; //reset speed at start of each step event
           //now you wont have to check if key has been released to stop object

if(keyboard_check(ord('W')))
{
    //distance between player and target
    distance = point_distance(x, y, mouse_x, mouse_y)

    if distance < playerSpeed
    {
        //with this, object will move only to mouse position, not further
        speed = distance;
    }
    else
    {
        speed = playerSpeed;
    }
}
else if(keyboard_check(ord('S')))
{
    speed = -playerSpeed;
}

lengthdir_x and lengthdir_y example:

// create event

playerSpeed = 3;

// step event

image_angle = point_direction(x, y, mouse_x, mouse_y);

if keyboard_check( ord("W") )
{
    x += lengthdir_x( playerSpeed, image_angle );
    y += lengthdir_y( playerSpeed, image_angle );
}
else if keyboard_check( ord("S") )
{
    x += lengthdir_x( playerSpeed, image_angle-180 );
    y += lengthdir_y( playerSpeed, image_angle-180 );
}

Please note that all game-maker functions use angle in degrees, with angle 0(360) at right side of circle, increasing counter-clockwise. Any functions work properly with values both under and above 0 to 360 range, while for example -90 = 270 (360-90) and 400 = 40 (400-360). Beware of how game-maker checks true and false. Any value < than 0 will result as false on check.