gml

How can I make the object not move the wrong way?


I am learning to use Game Maker, and I thought it would be a good project to make some basic plane movement script. I finished the part that would move the plane and then added the part that would make the plane glide slowly to a halt. The issue is that whenever I press the down arrow key, the plane will move, but it will glide to a halt moving up, not down. Here is the code. Note: plane_speed = 0, plane_acceleration = 0.1.

// Check keyboard input for up and down keys
keyboard_up = keyboard_check(vk_up);
keyboard_down = keyboard_check(vk_down);

// Adjust plane speed based on keyboard input and switch costumes
if keyboard_up == 1 {
    plane_speed = plane_speed + plane_acceleration;
    y -= plane_speed;  // Move plane up (assuming y is decreasing moves up)
    sprite_index = Sprite3;  // Change sprite to Sprite3 when moving up
}

if keyboard_down == 1 {
    plane_speed = plane_speed + plane_acceleration;
    y += plane_speed;  // Move plane down (assuming y is increasing moves down)
    sprite_index = sPlane;  // Change sprite to sPlane when moving down
}

if keyboard_down == 0 && keyboard_up == 0
    if plane_speed != 0 {
        if (plane_speed > 0) {
            plane_speed = plane_speed - plane_acceleration;
            y -= plane_speed;
        }
        if (plane_speed < 0) {
            plane_speed = plane_speed + plane_acceleration;
            y -= plane_speed;
        }

    }

I have tried to switch around some of the plus and minus signs, but it will just make everything worse. Is it possible that I should switch the order of my code, is it a bug, or is it something else?


Solution

  • I believe the problem is that plane_speed will only ever be positive when adjusted by the keyboard inputs in the code provided.

    ...
    if keyboard_up == 1 {
        plane_speed = plane_speed + plane_acceleration; // speed is positive.
        ...
    }
    
    if keyboard_down == 1 {
        plane_speed = plane_speed + plane_acceleration; // speed is positive.
        ...
    }
    ...
    

    Try modifying the "keyboard_up adjustment" section as follows:

    if keyboard_up == 1 {
        plane_speed = plane_speed - plane_acceleration;
        y += plane_speed;
        ...
    }
    

    This way, when keyboard_up == 1, plane_speed will go negative, and this negative value will be added to y (effectively subtracting from y).

    Edit: As user @ipodtouch0218 pointed out, if the plane is only ever supposed to glide downwards when decelerating, you'll also need to make this change:

    ...
    if (plane_speed < 0) {
        plane_speed = plane_speed + plane_acceleration;
        y += plane_speed;
    }
    

    If it helps, you can envision this as two cases:


    This is a little off-topic, and I apologize if I'm stepping out of my lane here since I don't know your game, but I'd like to point out that you may want to cap the plane's speed at some arbitrary value. (i.e. don't add to it if it's already a big positive number, and don't subtract from it if it's already a big negative number). Otherwise, the player might be able to reach ludicrous speeds by holding a keyboard input down. If this is already accounted for, please disregard.