Legacy GM [SOLVED] Horizontal movement screwed up

A

Anti-Icarus

Guest
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?
 

Simon Gust

Member
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.
 

TheouAegis

Member
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.
 
A

Anti-Icarus

Guest
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.
I had it like that earlier. I decapitalized the letters and the results were the same as before.
 

Simon Gust

Member
Oh I see something. The ' or " shouldn't be put on non-ord functions
Code:
var jump = keyboard_check(vk_space);
 
A

Anti-Icarus

Guest
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.

Oh I see something. The ' or " shouldn't be put on non-ord functions
Code:
var jump = keyboard_check(vk_space);
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!

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.
By the way, I decreased the hspeed so that the movement would be controlled better. So thank you for pointing that out as well.
 
Top