SOLVED Switch statement doing weird

AyC

Member
Hey guys, I'm using a switch statement, which apparently uses multiple cases, when depending on the value, it should just perform one task. I'm using an enum and variable from my main player object and transfer the data to my "hand/gun" which only appears after pressing specific button. I've been looking into multiple topics, but can't find my issue, so anyway.

The player object

Creation Code, just setting up the variables:

GML:
enum beams {

    none,

    powerb,

    plasma

}

global.beam = beams.none;
Step event, simply just controlling the value for testing purposes:

GML:
if (key_scroll_up){

    global.beam +=1;

} else if (key_scroll_down){

    global.beam -=1;

}

if (global.beam >=9) global.beam = 9;

if (global.beam <=0) global.beam = 0;
The Hand object

Step Event, which controlls what eventually shoot out of it, I'll just paste everything in for context sake:

GML:
x = obj_playerN.x-0.7;

y = obj_playerN.y-12.5;



image_angle = point_direction(x,y,mouse_x,mouse_y);



if (image_angle > 90) && (image_angle < 270){

    image_yscale =-1;

} else {

    image_yscale =1;

}



firingdelay = firingdelay -1;

recoil = max(0,recoil -1);



if (mouse_check_button_pressed(mb_left)) && (firingdelay < 0){

    switch (global.beam){

        case beams.powerb: {

            recoil = 5;

            firingdelay = 25;

            with (instance_create_layer(x,y+7,"Bullets",oBeam)){

                speed = 10;

                direction = other.image_angle;

                image_angle = direction;

                audio_play_sound(PowerBeam,1,false);

                break;

            }

        } case beams.plasma: {

            recoil = 5;

            firingdelay = 30;

            with (instance_create_layer(x,y+7,"Bullets",oBeamPlasma)){

                speed = 7;

                direction = other.image_angle;

                image_angle = direction;

                audio_play_sound(BeamPlasma,1,false);

                break;

            }

    } default: {

        return 0;

    }

}



if (mouse_check_button_pressed(mb_right)) && (firingdelay < 0){

    recoil = 4;

    firingdelay = 35;

    with (instance_create_layer(x,y+7,"Bullets",oRocket)){

        speed = 13;

        direction = other.image_angle;

        image_angle = direction;

        audio_play_sound(RocketLaunch,1,false);

    }

}



x = x - lengthdir_x(recoil,image_angle);

y = y - lengthdir_y(recoil,image_angle);

}
When beam value = 0, nothing fires. Value 1 fires both cases and value 2 only fires the plasma case. Why is value 1 doing both?
 
Last edited:

TheouAegis

Member
The following are technically "loops" in GameMaker:
  • repeat runs same code N times
  • do until runs same code as long as until condition is false
  • while runs same code as long as condition is true
  • for runs first statement, then same code in block, then same code in final statement as long as middle condition is true
  • with runs same code for all specified instances currently active
  • switch jumps to case matching the condition, then runs all following cases
Loops can be manipulated slightly:
  • break stops the immediate loop
  • continue stops the current code, but not the loop
Switch loops are different in that you can't use continue with them, but they are still loops.

You can nest loops inside loops, but the program can only focus on one loop at a time. In your code, you had a with loop inside a switch loop. This means a break encountered inside the with loop's code block will only apply to the with loop, because that is the immediate loop.
 
Top