0
\$\begingroup\$

Following is my code for my platformer. However, in the //Run with left shift// section when I made the image speed change, the speed is still the same and doesn't change so is there any piece of code that is preventing this task?

///Get player's input
key_right = keyboard_check(vk_right);
key_left = -keyboard_check(vk_left);
key_jump = keyboard_check_pressed(vk_space);
key_jump_held = keyboard_check(vk_space);
key_shoot = keyboard_check(ord("D"));

//React to inputs
move = key_left + key_right;
hsp = move * movespeed;
if (vsp < 10) vsp += grav;

if (place_meeting(x, y+1, Obj_Floor))
{
 vsp = key_jump * -jumpspeed;
}

if (vsp < 0) && (!key_jump_held) vsp = max(vsp,0)

//Run with left shift
if (keyboard_check(vk_lshift)) 
{
 movespeed = 5;
 image_speed = 0.14; 
}

if (keyboard_check_released(vk_lshift))
{
 movespeed = 3;
 image_speed = 0.025;
}

//Shooting direction
if (keyboard_check(vk_right))
{
 mpfacing = 0;
}

if (keyboard_check(vk_left))
{
 mpfacing = 180;
}

//Shooting
if (keyboard_check(ord("D"))) && (canshoot = true)
{
 instance_create(x+13, y-0.5, Obj_MP_Laser);
 canshoot = false;
 alarm[1] = 30;
}


//Horizontal Collision
if (place_meeting(x+hsp, y, Obj_Floor))
{
 while(!place_meeting(x+sign(hsp), y, Obj_Floor))
 {
  x += sign(hsp);
 }
 hsp = 0;
}
x += hsp;

//Vertical Collision
if (place_meeting(x, y+vsp, Obj_Floor))
{
 while(!place_meeting(x, y+sign(vsp), Obj_Floor))
 {
  y += sign(vsp);
 }
 vsp = 0;
}
y += vsp;

//Animate
if (move != 0) 
{
 image_xscale = move;
}

if (place_meeting(x, y+1, Obj_Floor))
{

 if (move != 0) 
 { 
  sprite_index = CalvinWalking;
  image_speed = 0.05;
 } 

 else 
 {
  sprite_index = CalvinStanding;
  image_speed = 0.025;
 }
}

else
{
 if (vsp < 0) sprite_index = CalvinJumping
 else sprite_index = CalvinJumping
}
\$\endgroup\$
1
\$\begingroup\$

The issue with your code seems to be here:

 if (move != 0) 
 { 
      sprite_index = CalvinWalking;
      image_speed = 0.05;
 } 

 else 
 {
      sprite_index = CalvinStanding;
      image_speed = 0.025;
 }

Right now you're saying this:

if player is pressing left or right:
    set sprite_index = CalvinWalking
    set image_speed = 0.05
otherwise if player is not pressing left or right:
    set sprite_index = CalvinStanding
    set image_speed = 0.025

Every frame you're checking whether or not the player is pressing the left or right key, and if they aren't to adjust the speed. Because you do this after you check if the player is pressing the shift key, the players speed never increases.

A few things to note:

  1. I'm fairly certain that GM compiles code much like C++, in that it compiles line by line (don't quote me on this)
  2. When organising code, you should generally try to isolate things; for example, you're managing collision detection, player input, shooting and animations, you should try to isolate these things into separate scripts, this way you can easily turn things on and off and will determine what the issue is.
|improve this answer|||||
\$\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.