Having issues with this door sequence

Im trying to get this sequence of code to work but im struggling. The door should open at a random time between 180 and 480 but then close, but for some reason the timer isn't restarting itself and the door just stays open. The player should also not enter the door until it has opened, otherwise pressing the right arrow will not make it move further.

Answers

  • edited March 2018

    You confused things a bit :

    doorCounter is your maximum

    You need another variable doorCount++; that you increase at the end of draw().

    if(doorCount>=doorCounter){
    
        // open door 
    
        doorCount=0;
    
        doorCounter=.....
    

    also, you could use millis()

    Names

    Find also better names like doorCounter and doorCounterMaximum or so

    Remarks

    also the door should stay open for some time like 200 and close

    You can also animate the door opening graphically

  • edited March 2018

    so Im a bit confused and that might be due to my limit knowledge.

    I have the random integer between 180 and 480 so the door can randomly open between 3 to 8 seconds, (60 frames for second)

    after the if statement and the code for the door to open should I put a else statement with the code for the closed door

    it doesn't have to be very animated.

    im just having trouble with the counter so the door can open and close not only once

  • edited March 2018
      if (doorCount>=doorCounter) {
        {                                                    
          //Opened door 
          strokeWeight(3);
          line(490, 210, 507, 200);
          line(507, 200, 507, 300);
          line(490, 290, 507, 300);
    
         else if (doorCount <= doorCounter) {
    
      strokeWeight(3);
      //Closed door 
      line(450, 200, 450, 300);
      line(450, 200, 507, 200);
      line(507, 200, 507, 300);
      line(450, 300, 507, 300);
      noStroke();
      fill(0);
      rect(465, 220, 25, 25);
      ellipse(460, 270, 10, 15);
    
      doorCount ++;
    }
    

    this what I've got so far

  • doorCount ++;

    Outside if or else!

  • edited March 2018

    added to the last code of draw, door opens within the limit but still doesn't close after it opens, UGHHHHHH

  • edited March 2018

    So, good progress here!

    When the door is open you would reset doorCount to 0 as I wrote above.

    But then the door would close immediately. Bad.

    you could set a new boolean variable to false before setup() :

    boolean doorIsOpen = false;
    

    Set it true when door opens.

    Also before setup():

    int doorCountForOpenDoor=0;
    

    Outside the big if and else section above make a new if clause:

    if(doorIsOpen) { 
    
        doorCountForOpenDoor++;
    
        if (doorCountForOpenDoor>300) {
    
        doorIsOpen=false; 
    
        doorCount=0;
    
        doorCounter=int(random(180,480));
    
        doorCountForOpenDoor=0;
    
        }
    
    }
    

    not tested....

  • okay ive added all you've said, added the statement after the if and else, still keeping the doorCount ++; in the last line in draw.

    when I set boolean doorIsOpen = false; same thing again, door opens, never closes

    when I set boolean doorIsOpen = true; the door opening flashes for one second, then finally opens again, stay open but again never closes

  • Then debug it...

    you need to reset the variables before using them again. All of them.

    Think it through and find the error

    I am not at home and can’t do it here

    Post your entire code

  • edited March 2018

    Do you have this:

    doorIsOpen = true;

  • edited March 2018

    sorry man

    maybe ive been looking at it too much without a break I can't get around the issue

    I do have that, no change with it

  • edited March 2018 Answer ✓

    Set doorIsOpen to true after line 35.

  • THANK YOU SO MUCH, it worked lol, so annoying

Sign In or Register to comment.