Tabs?

edited October 2013 in Using Processing

How do I put this code into different tabs by making it into an object; Separate class and my main body of code into different tabs.

void setup(){
size(200,200);
ellipseMode(CENTER);
rectMode(CENTER);}
void draw(){
background(0);
translate(X,Y);
rotate(r=(r+(t?.1:0))%TWO_PI);
stroke(39,242,29);
fill(255);
triangle(0,0,30,80,-30,80);
fill(39,242,29);
ellipse(0,5,60,60);
stroke(127);
line(10,20,-10,20);
fill(mouseX,0,mouseY);
ellipse(-19,0,16,25);
ellipse(19,0,16,25);
stroke(255);
line(-10,80,-20,100);
line(-20,100,-10,60);
line(10,80,20,100);
line(20,100,10,50);}
void keyPressed(){
  if(key==CODED)
  {if(keyCode==UP)Y--;
  if(keyCode==DOWN)Y++;
if(keyCode==RIGHT)X++;
if(keyCode==LEFT)X--;}if(key=='t'){t=!t;}}
float X=100,Y=100,r;
boolean t;

Answers

  • edited October 2013
        void setup(){ size(200,200); ellipseMode(CENTER); rectMode(CENTER);} 
    void draw(){ background(0); translate(X,Y); rotate(r=(r+(t?.1:0))%TWO_PI); stroke(39,242,29); fill(255); triangle(0,0,30,80,-30,80); fill(39,242,29); 
    ellipse(0,5,60,60); stroke(127); line(10,20,-10,20); fill(mouseX,0,mouseY); ellipse(-19,0,16,25); ellipse(19,0,16,25); stroke(255); line(-10,80,-20,100); line(-20,100,-10,60); line(10,80,20,100); line(20,100,10,50);} void keyPressed(){ if(key==CODED) {if(keyCode==UP)Y--; if(keyCode==DOWN)Y++; if(keyCode==RIGHT)X++; if(keyCode==LEFT)X--;}if(key=='t'){t=!t;}} float X=100,Y=100,r; boolean t;
    

    ControlP5

  • You don't need tabs to make objects (classes), methods or functions. Tabs are for code organization basically. Read about them here But I think you are more interested in make an object than a tab, right? This is a good start: http://processing.org/tutorials/objects/

    And another:

    http://wiki.processing.org/w/From_several_arrays_to_classes

  • _vk_vk
    edited October 2013

    And here a little sample. To properly handle the keys with the objects, you will need some kind of selection, which one to move? Right now they move all together. This is your code fast wrapped in a class, as an illustration. You will learn better from the articles than from this code.

    Alien one;
    Alien[] aliens = new Alien[2];
    
    void setup() { 
      size(600, 200); 
      one = new Alien(100, 100);
      aliens[0] = new Alien(300, 100);
      aliens[1] = new Alien(500, 100);
    } 
    void draw() { 
      background(0);
      one.display();
      for (Alien a:aliens) {
        a.display();
      }
    } 
    void keyPressed() { 
      one.update();
      for (int i = 0; i < aliens.length; i++) {
        aliens[i].update();
      }
    } 
    
    
    
    class Alien {
      float x, y, r;
      boolean t =false;
    
      Alien (float _x, float _y) {
        x = _x;
        y = _y;
      }
    
      void display() {
        ellipseMode(CENTER); 
        rectMode(CENTER);
        pushMatrix();
        translate(x, y); 
        rotate(r=(r+(t?.1:0))%TWO_PI); 
        stroke(39, 242, 29); 
        fill(255); 
        triangle(0, 0, 30, 80, -30, 80); 
        fill(39, 242, 29); 
        ellipse(0, 5, 60, 60); 
        stroke(127); 
        line(10, 20, -10, 20); 
        fill(mouseX, 0, mouseY); 
        ellipse(-19, 0, 16, 25); 
        ellipse(19, 0, 16, 25); 
        stroke(255); 
        line(-10, 80, -20, 100); 
        line(-20, 100, -10, 60); 
        line(10, 80, 20, 100); 
        line(20, 100, 10, 50);
        popMatrix();
      }
    
      void update() {
        if (key==CODED) {
          if (keyCode==UP)
            y--; 
          if (keyCode==DOWN)
            y++; 
          if (keyCode==RIGHT)
            x++; 
          if (keyCode==LEFT)
            x--;
        }
        else {
          if (key == 't') {
            t= !t;
          }
        }
      }
    }
    
Sign In or Register to comment.