1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. NOTICE: We will be applying a Xenforo update on Tuesday 25th of February. This means that from approximately 10:00 to 14:00 BST the forums will be offline (or possibly longer). Sorry for the inconvenience! Official Announcement here.

GM:S 1.4 [SOLVED] Horizontal movement screwed up

Discussion in 'Programming' started by Anti-Icarus, Aug 20, 2017.

  1. Anti-Icarus

    Anti-Icarus Member

    Joined:
    Jun 21, 2016
    Posts:
    34
    I'm having problems with programming horizontal movement in my prototype game. I've been trying to get my character to move left and right. Simple enough, but for some bizarre reason I can't comprehend, I've been making my character jump when it wasn't supposed to during my latest play tests. Here is the Create event for my character object:

    Code:
    /// Initialize variables
    global.ice_power = 0;
    power_up = 0;
    hspeed = 0;
    vspeed = 0;
    gravity = 10;
    gravity_direction = 270;
    audio_play_sound(snd_hoverboard, 10, true);
    And here is the first part of the Step event where the movement is supposed to be working:

    Code:
    /// Main controls
    
    //Initialize controls for simplicity
    var lm = keyboard_check('a');
    var rm = keyboard_check('d');
    var jump = keyboard_check('vk_space');
    var iceshoot = keyboard_check('s');
    var icewave = keyboard_check('w');
    
    //Left and right movements
    
    if (lm) {
        hspeed -= 8;
    }
    
    if (rm) {
        hspeed += 8;
    }
    
    //When not moving
      
    if ((!lm && !rm) || (lm && rm))
        hspeed = 0;
    
    //Jumping
        
    if (jump) {
        gravity = 0;
        gravity_direction = 90;
        vspeed = 15;
        sound_play(snd_jump);
    }
    else {
        if (vspeed < 16) {
            vspeed -= 5;
            gravity = 0;
            gravity_direction = 270;
        } 
    }
    But instead of moving to the right on the D key and to the left on the A key, the character keeps jumping upon pressing one of the two keys even when the space bar is not being pressed. It's like GameMaker is confusing the D key for the Space Bar. I can't identify the bug that's been causing this. Is there a way to fix this bug so that the player character can only move to the right on D, to the left on A, and only jump on the Space Bar?
     
  2. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,262
    Maybe it's because in the ord() functions, letters have to be capitalized.
    Code:
    var lm = keyboard_check('A');
    var rm = keyboard_check('D');
    var jump = keyboard_check('vk_space');
    var iceshoot = keyboard_check('S');
    var icewave = keyboard_check('W');
    
    Just like your keyboard in front of you suggests. Could be wrong though.
     
  3. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,449
    Code:
    if (lm) {
       hspeed -= 8;
    }
    
    if (rm) {
       hspeed += 8;
    }
    That's going to make you move VERY VERY fast. Like, you'll be gone in probably less than 60 steps.
     
  4. Anti-Icarus

    Anti-Icarus Member

    Joined:
    Jun 21, 2016
    Posts:
    34
    I had it like that earlier. I decapitalized the letters and the results were the same as before.
     
  5. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,262
    Oh I see something. The ' or " shouldn't be put on non-ord functions
    Code:
    var jump = keyboard_check(vk_space);
    
     
  6. Anti-Icarus

    Anti-Icarus Member

    Joined:
    Jun 21, 2016
    Posts:
    34
    Thanks to the suggestions provided, I managed to resolve the horizontal movement issues so that my player object will only move left on the A key and right on the D key.

    Upon reading "non-ord functions" in that suggestion, I looked up the word 'ord' in the GameMaker: Studio help manual and ended up altering my control initialization variables so that they would look more like this:

    Code:
    var lm = keyboard_check(ord('A'));
    var rm = keyboard_check(ord('D'));
    var jump = keyboard_check(vk_space);
    var iceshoot = keyboard_check(ord('S'));
    var icewave = keyboard_check(ord('W'));
    That has made a lot of difference in the way the overall code handles the horizontal movements. Thank you very much for your input!

    By the way, I decreased the hspeed so that the movement would be controlled better. So thank you for pointing that out as well.
     

Share This Page