How to switch to multiple states within a current state using individual keys?

I am currently trying to code some form of interactive fiction using Processing. I am relatively new to coding so please go easy on me. I attempted to use switch case and set up each possibility from each page within a case and then switch between them using void keyPressed(){ but I do not think I understand the code enough to be able to pull it off. It is going to be a tree of cases effectively as each case will need to have the option to switch to three other cases using 'a' 'b' and 'c' within itself and then all three options need to be able to do the same. I'm expecting it'll be a case of get the code correct for the first branch of the tree then be able to just keep duplicating it but I cannot get it right.

Is there a better way to achieve what I'm trying, or does anybody have any example code I can look at?

Tagged:

Answers

  • This is what i've got so far but it does not work.

    int gameState = 0;
    
    
    void setup() {
      size(600, 600);
    }
    
    void draw() {
      background(255);
      fill(0);
            text("title",250, 250);
            if(keyPressed ==true){
            gameState=1;
            }
    }
    void keyPressed() {
      switch(gameState) {  
      case 1:
        background(200);
            fill(0);
          text("STORY TEXT 1 AND OPTIONS",250, 250);
    //answer a
          if (key == 'a') {
          gameState=2;
    //answer b
        } else if (key == 'b') {
          gameState=3;
    //answer c
        } else if (key == 'c') {
          gameState=4;    
        }
        break;
    
      case 2:
        background(255, 0, 0);
            fill(0);
          text("A",250, 250);
         break; 
    
    
      case 3:
        background(0, 255, 0);
        fill(0);
          text("B",250, 250);
         break; 
    
         case 4:
        background(0, 255, 0);
        fill(0);
          text("C",250, 250);
           break; 
    
  • it does not work

    in what way does it not work? what do you expect? what is it doing instead? how do we test this?

  • edited June 2017

    what do you expect?

    Ideally the code would go as follows:

    Displaying the 'Title' screen which is just the void draw function and then hitting any key to begin the story(switch to case 1) and within case 1 there would be a body of text and a keyPressed for a,b and c leading to case 2, 3 and 4. And from this code I could apply the same principals to each case branch(have a keyPressed in case 2 leading to case 5,6 and 7 etc) It will be messy code but as long as it is repeatable and i comment what each is I am fine with it.

    what is it doing instead?

    Currently I cannot switch to the first case from the draw function because I do not know how to stop it drawing repeatedly. However, before I even added anything of substance into Void Draw the code was still not working properly. It would not begin in case 1 and it would switch to case 1 if any key was pressed, not just individual ones. This being something none of my code should do as far as I am aware.

    I hope this makes sense. Thank you for your reply.

  • I realise not having my title screen as a case is probably the issue with that particular aspect. But it was not beginning on any cases so I thought I had to.

  • If you use noLoop() then it'll only change when a key is pressed (via keyPressed method, using redraw()). Maybe that's a better idea?

  • The problem is that you are trying to draw the different states in keyPressed(). Instead, do all your drawing in draw():

    void draw(){
      switch(state){
        case 0:
          // Draw state 0.
          break;
        case 1:
          // Draw state 1.
          break;
        /// Etc.
      }
    }
    

    The switch which state you are in in keyPressed().

  • And the logic would possible be easier if you defined a class, Node, say. It would have the text for that node and 3 references to other nodes, essentially the nodes to visit depending on the key that's pressed.

Sign In or Register to comment.