Comunidad Game Maker

Ayuda => Preguntas y respuestas => Mensaje iniciado por: Kain88 en Junio 22, 2015, 11:00:06 PM

Título: Duda para pasar codigo de if a switch
Publicado por: Kain88 en Junio 22, 2015, 11:00:06 PM
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;
    }   
}
Título: Re:Duda para pasar codigo de if a switch
Publicado por: Clamud en Junio 23, 2015, 01:27:59 AM
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]
Título: Re:Duda para pasar codigo de if a switch
Publicado por: Kain88 en Junio 23, 2015, 02:44:50 AM
Aah ya entendí, voy a acomodar un poco los if entonces.