Code from page

13»

Answers

  • yes, understood

    maybe the for-loop gets called more often than 1 time?

    you can say

    allPCsAreDrawn = true; 
    
    If ( ! allPCsAreDrawn ) {
    
    ....
    
    }
    
  • every time when i build room ill use loop, but i dont understand where should i use your code???

  • maybe that why you get too many PCs when the for- loop gets called too often

    so before setup say boolean allPCsAreDrawn = false;

    then comes the if-clause I just said, it is around the for-loop

    inside the if is the for loop and we set the var to true

    it's our marker to assure the for-loop is run only once

    just a small yes/no switch

  • i dont understand you, can you fix my code

    float sum;
        sum = currArea/6;
        println(sum);
        for(int m=1; m <= sum; m ++)
      {  
               if (mouseY > 120)
               {
              strokeWeight(4);
              rect(mouseX-18,mouseY-14,36,28);
              image(img1, mouseX-18,mouseY-14,36,28); 
             }
             }
             fill(0,255,0);
       }
    
  • here...

    // before setup()
    boolean allPCsAreDrawn = false;
    
    // ...................
    
    
    float sum;
    sum = currArea/6;
    println(sum);
    If ( ! allPCsAreDrawn ) {
      allPCsAreDrawn = true; 
      for (int m=1; m <= sum; m ++)
      {  
        if (mouseY > 120)
        {
          strokeWeight(4);
          rect(mouseX-18, mouseY-14, 36, 28);
          image(img1, mouseX-18, mouseY-14, 36, 28);
        }
      }
      fill(0, 255, 0);
    }
    }
    
  • this makes it so that the PCs are drawn only once

    because of allPCsAreDrawn the sketch knows whether they are already drawn or not

  • sorry but you didnt understand me, look : i have a room which i build file-1922 i have area 150 meters squared : 6 and get 25 (25 computers in room) file-3850 in this room i must draw 25 not more ( may be <) with your code i cant draw PC

  • you want to draw the PCs with the mouse and automatically stop after 25 PCs?

    Man, you are building a cathedral and you don't even know how to build a simple wall, right?

  • I mean when you draw the PCs with the mouse one by one, just stop drawing PCs when you have 25

  • you don't need a for-loop then.

  • edited April 2014

    "you want to draw the PCs with the mouse and automatically stop after 25 PCs? " i learn processing a little ( few mounth) Yes i need to do it, with a mouse

  • what did i do wrong? about wall i build walls with a lines

  • edited August 2014

    the cathedral and wall thing was a metaphor

    I think your program is very big (like a cathedral) and you lack some basic knowledge (no offense).

    did you follow my advice to put arrays in?

    for the PCs thing

    you made a small nice bar with the buttons, and one of them is to place the PCs in the room, right?

    when you click it:

    while in this mode you place PCs with the mouse

    your code must come without the for-loop

    float sum; sum = currArea/6; println(sum);

           if (mouseY > 120)
           {
          strokeWeight(4);
          rect(mouseX-18,mouseY-14,36,28);
          image(img1, mouseX-18,mouseY-14,36,28);
    
         }
         fill(0,255,0);
    

    }

    the general architecture of your program

    when I have a bigger program as yours, I take time to write it in a way that is easy to read (and to maintain)

    one point is to use functions (and don't have all in draw())

    one point is to use tabs (and don't have everything in one tab)

    one point is to have states: With a state I exactly know in a game e.g. where I am: do I show the start screen, do I play, do I show the high score?

    do you use states? Because you could be in a state drawPCs and then he counts how many times you clicked... displays this number... when you have 25, he leaves the state...

    ;-)

  • if(button1[i]) 
       {  
       float sum;
       int count=0;
       sum=currArea/6;
       println(sum);
       println(count);
       if(sum==0)
    {   
               if (mouseY > 120)
               {
              strokeWeight(4);
              rect(mouseX-18,mouseY-14,36,28);
              image(img1, mouseX-18,mouseY-14,36,28); 
             }
             fill(0,255,0);
    }
    if ( sum > count)
    {
    count=count+1;
      if (mouseY > 120)
               {
              strokeWeight(4);
              rect(mouseX-18,mouseY-14,36,28);
              image(img1, mouseX-18,mouseY-14,36,28); 
             }
    }
       }
     else 
          {
            noFill();
          }
          strokeWeight(3);
    rect(330,26,56,48);
    

    i still cant fix this part of code, when i draw rect i want to do this " count=count+1;" but it dont work, i want add +1 every time when i draw rect, can you check my code?

  • you really should use ctrl-t in your processing

  • i thought that i had been pressed ctr-t, anyway what do you think about code, why it dont work?When i draw rect i want to add +1 to "count"

  • why

    if ( sum > count) { count=count+1;

    I don't understand

    why not count=count+1;

    after line 14 ?

    I told you how I would do it: With states, see old post...

  • i can do nothing (( i dont understand how to do it

  • edited May 2014

    just for anyone interested:

    this went on via PM and e-mail

  • Other than I wrote in one of my e-mails, you cannot totally put the mouse stuff into mousePressed (since you're drawing with the mouse directly in some modes). You can put them into clear separate functions though.

    ;-)

  • did you ask about a code for a timer?

  • edited May 2014

    this is a simple timer (it's like a stop watch: hit 1 to start it, hit 2 to stop it. It shows the millis passed)

    // stop watch 
    
    Timer timer1 = new Timer(); 
    
    void setup() {
      size(333, 333);
      background(111);
    } // func 
    
    void draw() {
      // 
      background(111);
      text("press 1 to start, 2 to stop", 20, 20);
    } // func 
    
    void keyPressed() {
      switch (key) {
      case '1':
        timer1.start();
        println ("started");
        break;
      case '2':
        timer1.stop();
        println("stop: " + timer1.resultTime);
        break;
      } // switch
    } // func 
    
    // =======================
    
    class Timer {
      int startTime  = 0; 
      int stopTime   = 0;  
      int resultTime = 0;
      boolean isRunning;  
    
      Timer() {
        startTime = 0;   // is not set 
        isRunning = false;
      } // constr
    
      void start() {
        reset();
        isRunning = true;
        startTime = millis();   // set
      }
    
      void stop() {
        isRunning = false;
        stopTime = millis();
        resultTime = stopTime - startTime;
      } 
    
      void reset() {
        isRunning = false;
        startTime  = 0;
        stopTime   = 0;  
        resultTime = 0;
      }
    
      int getCurrentTime() {
        return millis() - startTime;
      }
      //
    } // class 
    
    // ======================
    
  • you need an array of Timer:

    Timer[] timers = new Timer [1000];

    and then use it

    timers[indexTimer].xxxxxxxx

  • here

    // stop watch 
    
    Timer[] timers = new Timer[1000]; 
    int index;
    
    void setup() {
      size(333, 333);
      background(111);
    } // func 
    
    void draw() {
      // 
      background(111);
      text("press 1 to start, 2 to stop", 20, 20);
    } // func 
    
    void keyPressed() {
      switch (key) {
      case '1':
        timers[index]=new Timer();
        timers[index].start();
        println ("started");
        break;
      case '2':
        timers[index].stop();
        println("stop: " + timers[index].resultTime);
        index++;
        print("List of timers: ");
        for (Timer t1 : timers) {
          if (t1!=null)
            print(t1.getResultTime()+", ");
        }
        println();
        break;
      } // switch
    } // func 
    
    // =======================
    
    class Timer {
      int startTime  = 0; 
      int stopTime   = 0;  
      int resultTime = 0;
      boolean isRunning;  
    
      Timer() {
        startTime = 0;   // is not set 
        isRunning = false;
      } // constr
    
      void start() {
        reset();
        isRunning = true;
        startTime = millis();   // set
      }
    
      void stop() {
        isRunning = false;
        stopTime = millis();
        resultTime = stopTime - startTime;
      } 
    
      void reset() {
        isRunning = false;
        startTime  = 0;
        stopTime   = 0;  
        resultTime = 0;
      }
    
      int getResultTime() {
        return resultTime;
      }
    
      int getCurrentTime() {
        return millis() - startTime;
      }
      //
    } // class 
    
    // ====================
    
  • what is index? you mean first will be Timer[0] then will be Timer[1] then Timer[2]??

  • yes, it's a list of timers and index is which one you have currently

  • look what i get a few indexs i want build a room timers[1]=20 timers[2]=50 and rect(); rect(mouseX,mouseY,timers[1],timers[2]); datas from timers its length of wall

  • ??

    My sketch gives the time in millis between you press 1 and 2.

    Nothing to do with what you plan...?

  • i understood that i want write timers[1] (with idex 1) and timers[2]( with index 2 ) in othen value because when i use rect(mouseX,mouseY,timers[1],timers[2]); i got this errorБезымянный

  • Make

    int zet.....

  • i use int in this case error same but "to int" in the end

  • Why 5 ??

    This timer is empty....

  • i can compilate code because of this error

  • my bad...

    // stop watch 
    
    Timer[] timers = new Timer[1000]; 
    int index;
    
    void setup() {
      size(333, 333);
      background(111);
    } // func 
    
    void draw() {
      // 
      background(111);
      text("press 1 to start, 2 to stop", 20, 20);
    } // func 
    
    void keyPressed() {
      switch (key) {
      case '1':
        timers[index]=new Timer();
        timers[index].start();
        println ("started");
        break;
      case '2':
        timers[index].stop();
        println("stop: " + timers[index].resultTime);
        int zet;
        if (index>5) {
          zet = timers[5].resultTime;
          println("zet "+zet);
        }
        index++;
        print("List of timers: ");
        for (Timer t1 : timers) {
          if (t1!=null)
            print(t1.getResultTime()+", ");
        }
        println();
        break;
      } // switch
    } // func 
    
    // =======================
    
    class Timer {
      int startTime  = 0; 
      int stopTime   = 0;  
      int resultTime = 0;
      boolean isRunning;  
    
      Timer() {
        startTime = 0;   // is not set 
        isRunning = false;
      } // constr
    
      void start() {
        reset();
        isRunning = true;
        startTime = millis();   // set
      }
    
      void stop() {
        isRunning = false;
        stopTime = millis();
        resultTime = stopTime - startTime;
      } 
    
      void reset() {
        isRunning = false;
        startTime  = 0;
        stopTime   = 0;  
        resultTime = 0;
      }
    
      int getResultTime() {
        return resultTime;
      }
    
      int getCurrentTime() {
        return millis() - startTime;
      }
      //
    } // class 
    
    // ==================
    
Sign In or Register to comment.