Can I make a condition for the setup() and draw() to execute?

Hello, Maybe it's a dumb question, but is there a way to make a condition before setup() and draw()? (I want to make "Press space to start the game" -game that is in my sketch- ) Maybe I could do it in the draw() too but I don't know how to do it.

Thank you in advance. :)

Tagged:

Answers

  • How are you going to print the message if you haven't called setup()?

    The traditional way is to set a flag in setup, check it on draw, draw the "press start" until the key is pressed and the flag is changed.

    Keypressed () in the reference.

  • Could you give me an example? I didn't manage to do it :'(

  • Answer ✓
    var flag = false;
    
    function setup() {
      createCanvas(800, 600);
    }
    function draw() {
      background(128);
      if (!flag) {
        text("press a key to start", 100, 100)
      } else {
        text("game on", 100, 100);
      }
    }
    
    function keyPressed() {
      flag = true;
    }
    
  • That's what I tried but I made an error when typing curly brackets. That's what I get for trying this so late. Anyways thanks for your intelligent answer to a dumb question. Have a good day.

Sign In or Register to comment.