1
\$\begingroup\$

I am using game maker because I am new to game development and I think it's the easiest to understand because I took a course on it but I put in all the events so that my playerobj moves when you press the A and D keys but when I load up the game it doesn't work here is the GML code below that I am using:

//Speed of player: 5 Pixels per frame
 playerSpeed = 5;

 //Shortcuts for keypresses
 MOVELEFT = keyboard_check(ord('A'));
 MOVERIGHT = keyboard_check(ord('D'));
 MOVEUP = keyboard_check(ord('W'));
 MOVEDOWN = keyboard_check(ord('S'));

 //Move Player
 if (MOVELEFT && x > sprite_width/2) 
 { 
x -= playerSpeed; 
 }

 if (MOVERIGHT && x < room_width - sprite_width/2) 
 { 
x += playerSpeed; 
 }

 if (MOVEUP && y > room_height/2) 
 { 
y -= playerSpeed; 
 }

 if (MOVEDOWN && y < room_height - sprite_width/2) 
 { 
y += playerSpeed; 
 }
\$\endgroup\$
  • \$\begingroup\$ You need to supply more information than that... \$\endgroup\$ – Eric Mar 19 '17 at 0:19
  • \$\begingroup\$ ... including which company's Game Maker you're using. There have been at least six game development environments named Game Maker published since 1985. \$\endgroup\$ – GlennRay Mar 19 '17 at 3:08
  • \$\begingroup\$ @GlennRay I am using YOYO Games one and I have changed it so that it's GML code but it still doesn't work I tried it out in a test room I made but it still doesn't move I have added it into the question \$\endgroup\$ – jonathan connery Mar 19 '17 at 12:03
  • \$\begingroup\$ @Eric I have changed it so that I am using GML code now it is listed above but it still doesn't work do you think it might be something wrong with the player character \$\endgroup\$ – jonathan connery Mar 19 '17 at 12:07
0
\$\begingroup\$

Your player isn't moving because you associated this very code to the wrong instance event. Try this:

Create Event

/// Declare variables
playerSpeed = 5;    // 5 pixels per step
MOVELEFT = 0;       // Checks "A" presses
MOVERIGHT = 0;      // Checks "D" presses
MOVEUP = 0;         // Checks "W" presses
MOVEDOWN = 0;       // Checks "S" presses

Declaration of all variables is important for operations, as it's good practice to not let an assignment in further events be treated as a declaration too.

Step Event

///Move player
if ( (MOVELEFT) && (x>sprite_width/2) )
{
    x -= playerSpeed;
}

if ( (MOVERIGHT) && (x<room_width-sprite_width/2) )
{
    x += playerSpeed;
}

if ( (MOVEUP) && (y>room_height/2) )
{
    y -= playerSpeed;
}

if ( (MOVEDOWN) && (y<room_height-sprite_width/2) )
{ 
    y += playerSpeed;
}

You need to put this part of code in the instance's Step Event in order to check keys continuously and move your player every step accordingly. This should work fine.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.