Switch Statement not Switching Case?

Discussion in 'Programming' started by KC Frye, Jul 11, 2019.

  1. KC Frye

    KC Frye Member

    Joined:
    Apr 15, 2019
    Posts:
    10
    I've created a switch statement with a series of cases as a means to create a cutscene. However, I've noticed that after case 7, the code suddenly stops running.

    As part of the code, when the cutscene object itself exist, the player character cannot move. However, when I reach case 7, the player is suddenly able to move, which should only happen when the object is deleted. What am I doing wrong?

    if (currentState = cutSceneStates.Active)
    {
    switch (currentStep) {

    case 0:
    obj_Wayfarer.sprite_index = spr_WayfarerRight
    counter = 0;
    if (counter == 60){
    counter = 0;
    ++currentStep;
    }

    else {
    ++counter;}
    break;

    case 1: //"Horatio!"
    fnt = fntGenDialogue;
    whichBox = obj_DialogueBox;
    whichFaceSpr = faceSprWayfarer;
    hasExpression = true;
    counter = 0;
    if (!DialogueSystemOn){
    messageGiver = self;
    PCtalking = obj_Player_Arete;
    index1 = 0;
    index2 = 0;
    currentExpression = 20;
    isTalking = true;
    scr_dialogue();
    }
    break;

    case 2: //dialogue system off
    DialogueSystemOn = false;
    if (counter == 60)
    {
    counter = 0;
    ++currentStep
    }
    else ++counter;
    break;
    break;

    case 3:
    fnt = fntGenDialogue;
    whichBox = obj_DialogueBox;
    whichFaceSpr = faceSprHoratio;
    hasExpression = true;
    counter = 0;
    if (!DialogueSystemOn){
    messageGiver = self;
    PCtalking = obj_Player_Arete;
    index1 = 1;
    index2 = 0;
    currentExpression = 0;
    isTalking = true;
    scr_dialogue();
    }
    break;

    case 4:
    DialogueSystemOn = false;
    if (counter == 60)
    {
    counter = 0;
    ++currentStep
    }
    else ++counter;
    break;
    break;

    case 5: //create Horatio
    instance_create_layer(416,0,"Objects", obj_Horatio)
    obj_Horatio.sprite_index = spr_HoratioYoung;
    if (counter == 60) {
    counter = 0;
    ++currentStep
    }
    else {++counter}
    break;

    case 6: //horatio moves down
    if (obj_Horatio.y != 64)
    {
    obj_Horatio.y += 1;
    obj_Horatio.image_speed = 1;
    }
    else {
    obj_Horatio.image_speed = 0;
    obj_Horatio.image_index = 0;
    obj_Horatio.y = 64;
    obj_Wayfarer.sprite_index = spr_Wayfarer;
    ++currentStep
    }
    break;


    case 7: //create exclamation mark
    if (!instance_exists(obj_ExclaimationMark))
    {instance_create_layer(386, -13, "Objects", obj_ExclaimationMark)}
    else ++currentStep;
    break;

    case 8:
    counter = 0;
    if (counter == 60)
    {
    counter = 0;
    ++currentStep
    }
    else ++counter;
    break;


    case 9: //destroy exclaimation mark
    if (instance_exists(obj_ExclaimationMark))
    {instance_destroy(obj_ExclaimationMark);}
    else ++currentStep;
    break;


    case 10: //Horatio "found shoes"
    fnt = fntGenDialogue;
    whichBox = obj_DialogueBox;
    whichFaceSpr = faceSprHoratio;
    hasExpression = true;
    counter = 0;
    if (!DialogueSystemOn){
    messageGiver = self;
    PCtalking = obj_Player_Arete;
    index1 = 2;
    index2 = 0;
    currentExpression = 1;
    isTalking = true;
    scr_dialogue();
    }
    break;

    case 12:
    DialogueSystemOn = false;
    ++currentStep;
    break;

    case 11: //horatio moves down
    if (obj_Horatio.y != 320)
    {obj_Horatio.y += 2;
    obj_Horatio.image_speed = 1;}
    else
    {obj_Horatio.image_speed = 0;
    obj_Horatio.image_index = 0;
    obj_Horatio.y = 320;
    ++currentStep;}
    break;

    case 13: //destroy shoes
    instance_destroy(obj_HoratioShoes)
    if (counter == 30)
    {counter = 0;
    ++currentStep;}
    else {++counter}
    break;

    case 14: //Horatio turns around.
    obj_Horatio.sprite_index = spr_HoratioYoungUp;
    if (counter == 30)
    {counter = 0;
    ++currentStep;}
    else {++counter}
    break;

    case 15: //create exclaimation mark
    instance_create_layer(385, 240, "Objects", obj_ExclaimationMark);
    counter = 0;
    if (counter == 60)
    {counter = 0;
    ++currentStep;}
    else {++counter}
    break;

    case 16: //destroy exclaimation mark
    instance_destroy(obj_ExclaimationMark);
    if (counter == 30)
    {counter = 0;
    ++currentStep}
    else {++counter}
    break;

    case 17: //"Oh!"
    fnt = fntGenDialogue;
    whichBox = obj_DialogueBox;
    whichFaceSpr = faceSprHoratio;
    hasExpression = true;
    counter = 0;
    if (!DialogueSystemOn){
    messageGiver = self;
    PCtalking = obj_Player_Arete;
    index1 = 3;
    index2 = 0;
    currentExpression = 4;
    isTalking = true;
    scr_dialogue();
    }
    break;

    case 18:
    DialogueSystemOn = false;
    ++currentStep;
    break;

    case 19: //"I didn't see you there.
    fnt = fntGenDialogue;
    whichBox = obj_DialogueBox;
    whichFaceSpr = faceSprHoratio;
    hasExpression = true;
    counter = 0;
    if (!DialogueSystemOn){
    messageGiver = self;
    PCtalking = obj_Player_Arete;
    index1 = 4;
    index2 = 0;
    currentExpression = 1;
    isTalking = true;
    scr_dialogue();
    }
    break;

    case 20:
    DialogueSystemOn = false;
    ++currentStep;
    break;

    case 21:
    fnt = fntGenDialogue;
    whichBox = obj_DialogueBox;
    whichFaceSpr = faceSprWayfarer;
    hasExpression = true;
    counter = 0;
    if (!DialogueSystemOn){
    messageGiver = self;
    PCtalking = obj_Player_Arete;
    index1 = 5;
    index2 = 0;
    currentExpression = 0;
    isTalking = true;
    scr_dialogue();
    }
    break;

    case 22:
    DialogueSystemOn = false;
    ++currentStep;
    break;

    case 23:
    fnt = fntGenDialogue;
    whichBox = obj_DialogueBox;
    whichFaceSpr = faceSprWayfarer;
    hasExpression = true;
    counter = 0;
    if (!DialogueSystemOn){
    messageGiver = self;
    PCtalking = obj_Player_Arete;
    index1 = 6;
    index2 = 0;
    currentExpression = 2;
    isTalking = true;
    scr_dialogue();
    }
    break;

    case 24:
    DialogueSystemOn = false;
    ++currentStep;
    break;

    case 25:
    fnt = fntGenDialogue;
    whichBox = obj_DialogueBox;
    whichFaceSpr = faceSprWayfarer;
    hasExpression = true;
    counter = 0;
    if (!DialogueSystemOn){
    messageGiver = self;
    PCtalking = obj_Player_Arete;
    index1 = 7;
    index2 = 0;
    currentExpression = 1;
    isTalking = true;
    scr_dialogue();
    }
    break;

    case 26:
    DialogueSystemOn = false;
    ++currentStep;
    break;




    }
    }
     
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,552
    The problem is immediately obvious once you look at case 8. You are continually setting the counter back to 0.
    Code:
    case 8:
    counter = 0;
    if (counter == 60)
    {
    counter = 0;
    ++currentStep
    }
    else ++counter;
    break;
    
    The correct solution is to set it back to 0 just once in case 7 when changing cases, not continually in case 8.
    Code:
    case 7: //create exclamation mark
      if (!instance_exists(obj_ExclaimationMark)) {
        instance_create_layer(386, -13, "Objects", obj_ExclaimationMark);
      } else {
        counter = 0;
        ++currentStep;
      }
      break;
    
    case 8:
      if (counter == 60) {
        counter = 0;
        ++currentStep;
      }
      else {
        ++counter;
      }
      break;
    
     
    Phil Strahl likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice