Help With Charged Move

O

Okky Putra Perdana

Guest
I'm still learning with GMS 2, and recently i tried to create a charged mechanic. Basically, what i want to do is when i press "space" key, the player object will stop moving and begin charging. And then, when i release the "space" key, it will gain a boost movement for several time before going back to normal speed. Here is my code:

//Charge
var charge_time = 0;
if (keyboard_check(vk_space)) && charge_time < 1{
charge_time += 1;
mv_spd = 0;
if (charge_time > 5){
charge_time = 5;
}
}
x += hspd;
y += vspd;
if (keyboard_check_released(vk_space)){
mv_spd = 10;
charge_time -= 1;
if (charge_time < 0){
mv_spd = 4;
charge_time = 0;
}
}

The problem i face is when i release the "space" key, my player object didn't get the boost and still having the normal speed. Any suggestion?

Thank you very much.
 

Simon Gust

Member
Does the charge work at all? I feel like it shouldn't.

This
Code:
var charge_time = 0;
will reset charge_time every step to 0. charge_time will never reach any higher than 1.

If charge_time is 1 and you release space, it will subtract 1 again and charge_time will be 0.
Code:
if (charge_time < 0) {
Which means, charge_time cannot be less than 0 ever.

On top of that, releasing a key only triggers once after a key has been pressed.
Your code will only run once.

Code:
//Charge
mv_spd = 4;
if (keyboard_check(vk_space))
{
  if (charge_time <= 0)
  {
    mv_spd = 0;
    charge_time = min(charge_time + 1, 5);
  }
}
else if (charge_time > 0)
{
  mv_spd = 10;
  charge_time -= 1;
}

/*
important! evaluate hspd after setting mv_spd.
*/

x += hspd;
y += vspd;
 
Top