'D' key is always being registered as pressed when it isn't

C

chrispytoes

Guest
I just started a brand new project, and this movement code is super simple and I have used it for many games in the past. I just started using GM again after some years but in my mind I don't think there's anything wrong with this code.

The up and down directions work fine, but the player is ALWAYS moving to the right. When I press the 'D' key the player just moves twice as fast to the right, and pressing 'A' makes it stop.

[Create]
speed = 5;

[Step]
if (keyboard_check(ord('W'))) {
y -= speed;
image_angle = 90;
}
if (keyboard_check(ord('A'))) {
x -= speed;
image_angle = 180;
}
if (keyboard_check(ord('S'))) {
y += speed;
image_angle = 270;
}
if (keyboard_check(ord('D'))) {
x += speed;
image_angle = 0;
}

There is no other code or objects in the game yet, this is everything. I'm super confused, there is no other place where the 'x' value is being added onto.
 
speed is a builtin variable that automatically adjusts an instance's x and y variables based on the direction variable. direction is set to 0 by default, which faces the right. Therefore, when speed is above 0, the instance starts moving to the right, and because you're also manipulating the x (and y) variables yourself, it doubles the movement distance when pressing D. Pressing A subtracts the current speed, basically cancelling out the effect, which is why it stops (5-5 = 0).

Simple solution - use a different variable name.
 
N

NeonBits

Guest
maybe switch "speed" with something else? "spd"
edit: oops, sorry, haven't seen your reply, should have refreshed the page.
 

Smiechu

Member
Speed is a very strange build in function and it works with conjunction with direction... the implementation of those two is very annoying in GMS...

I.e... when you set the speed to -10 the direction turns 180deg and speed is set to 10...
Than the direction is in degrees but it automatically skips between 359deg and 0 deg... making some calculations relating direction false...

Regarding your code... the guys above have already explained whats wrong...
If you want to use build in varables only for simple movement, than use direction variable as where the object should move and speed must be always positive value, negative value turns the direction 180deg...
 

kupo15

Member
This is why I use as few built in variables as possible and recreate them myself. You have so much less control when using them. Speed, gravity, direction, image_speed, alarms, solid etc.... all gone for me
 
N

NeonBits

Guest
And when you discover GM has a bug in its alarms system, you fear to use any of its shortcuts...
[*frustrated GM beginner emoticon*]
But it is fixed, now, I suppose...
 
Last edited by a moderator:
N

NeonBits

Guest
Setting an alarm didn't worked for me in the past, no matter how much I've tried. Then I've read somewhere in the forum someone saying the alarm system had a problem. I thought it was linked. Since then I've stoped using it. I think it's also in the description of one patch for 1.4.
 
I looked up the patch notes and didn't see anything about problems with setting an alarm. They've always been rock solid and the bugs I did find related to alarms firing off during certain special cases. Nothing wrong with avoiding them though, but doing so out of fear is a bit much.
 
N

NeonBits

Guest
you ve looked all the patch notes? ctrl f? I think there's something written somewhere. Realy, I think there is something written somewhere about the alarms system in one patch. I want to say my mind could be playing against me, but I feel right about it. But that's just me lol Who knows if it's the result of the damn stuff the neighboors keep smoking and making me breath; but everyone seems to feel ok about the idea of altering someone else's state of mind to their own will; try to make anything understand, good luck with that; the futur is hidden somewhere in a cloud... good upcoming rpg...

Alarms firing off is enough for me to not use it; I have enough problem at trying to understand what I try to code, if I got it right and the result is not there, it's sufficiant for me to just make my own timer.

And I've wrote this, somewhere else in the forum: I added a line of codes and got an error when launching the game, so I turned the line into a comment //, error was gone, then deleted the //, the error was still gone and the action was ok. I've put away GM because of that, and decided to try it again recently hoping the new version was ok. I'm always uncertain. But I see what people are able to do and I want to do something too.
 
Last edited by a moderator:
Top