Hola, queria saber si es posible pasar el siguiente codigo de if a switch, ya que tengo entendido que en casos asi el switch es mas practico. Lo intenté pero me tiró errores.
//Carga Megabuster
if (keyboard_check(ord("A"))) //Si se mantiene presionado el botón de disparo
{
charging += 1;
if (sound_charge_playing == true && charging > 10) //Sonido de megabuster cargando
{
sound_play(snd_megabuster_charge);
sound_charge_playing = true;
}
//Estado idle
if (charging > 10 && charging < 70 && state_id = state_idle) //Megabuster medium
{
image_speed = 0.3;
sprite_index = spr_megaman_charge_medium_idle;
}
else
if (charging > 70 && state_id = state_idle) //Megabuster max
{
image_speed = 0.3;
sprite_index = spr_megaman_charge_max_idle;
}
else
//Estado run
if (charging > 10 && charging < 70 && state_id = state_run) //Megabuster medium
{
image_speed = 0.2 - 0.05;
sprite_index = spr_megaman_charge_medium_run;
}
else
if (charging > 70 && state_id = state_run) //Megabuster max
{
image_speed = 0.2 - 0.05;
sprite_index = spr_megaman_charge_max_run;
}
else
//Estado jump
if (charging > 10 && charging < 70 && state_id = state_jump) //Megabuster medium
{
image_speed = 0.3;
sprite_index = spr_megaman_charge_medium_jump;
}
else
if (charging > 70 && state_id = state_jump) //Megabuster max
{
image_speed = 0.3;
sprite_index = spr_megaman_charge_max_jump;
}
}
No se puede programar con "switch" porque ésa expresión sólo sirve comparar valores exactos (==) y en tu código estás comparando intervalos (<, >). Se puede reducir un poco haciendo "ifs" anidados, por ejemplo:
[gml]
if (charging > 10 && charging < 70 ) //Megabuster medium
{
if( state_id == state_idle ) {
image_speed = 0.3;
sprite_index = spr_megaman_charge_medium_idle;
}
if( state_id == state_run ) {
image_speed = 0.2 - 0.05;
sprite_index = spr_megaman_charge_medium_run;
}
if( state_id == state_jump ) {
image_speed = 0.3;
sprite_index = spr_megaman_charge_medium_jump;
}
}
[/gml]
Aah ya entendí, voy a acomodar un poco los if entonces.