how do you make a class variable global?

edited December 2016 in Programming Questions

Trying to use a class variable to specify the new objects parameters but i don't know how. Thanks for the help!

Answers

  • Check this example. I have a button class with multiple variables. I have instantiated an button b1 from this class. The b1 object's parameters are described by the class Button. Can you think of any extra object parameters that could/should be included in the Button class?

    This next guide could provide you more details of the main concepts you want to master: https://processing.org/tutorials/objects/

    Kf

    class Button {
      String label;
      float x;      
      float y;    
      float w;    
      float h;    
    
      // constructor
      Button(String labelB, float xpos, float ypos, float widthB, float heightB) {
        label = labelB;
        x = xpos;
        y = ypos;
        w = widthB;
        h = heightB;
      }
    
      void Draw() {
        fill(218);
        stroke(141);
        rect(x, y, w, h, 10);
        textAlign(CENTER, CENTER);
        fill(0);
        text(label, x + (w / 2), y + (h / 2));
      } 
    
    }
    
    
    Button b1;
    void setup() {
      size(600, 400);
      rectMode(CORNER);
      textAlign(CENTER, CENTER);
      b1=new Button("One button", width*0.2, 200, width*0.6, 50);
    }
    
    void draw() {
      background(92);
      b1.Draw();
    }
    
  • edited December 2016

    Yea I didn't get it I still need help, can someone give a straight answer.

  • Not based on the information you've given us, no.

    Post some code.

  • edited December 2016

    I'M TRYING TO PUT SIZE(THE CLASS VARIABLE) WHERE IT SAYS /HERE/ IN THE SETUP FUNCTION BUT I DON'T KNOW IF IT'S POSSIBLE.

    spaceship spaceship1;
    spaceship spaceship2;
    spaceship spaceship3;
    spaceship spaceship4;
    
    void setup() {
      size(500, 400);
      background(0);
      smooth();
      frameRate(30);
      strokeWeight(1);
      ellipseMode(CENTER);
      rectMode(CENTER);
      spaceship1 = new spaceship(mouseX, 125, 1.75, 1.75, random(/*HERE*/, width-/*HERE*/), random(/*HERE*/, height-/*HERE*/), 50);
      spaceship2 = new spaceship(mouseX, 125, -1.75, -1.75, random(/*HERE*/,width-/*HERE*/), random(/*HERE*/, height-/*HERE*/), 75);
      spaceship3 = new spaceship(mouseX, 125, 1.75, 1.75, random(/*HERE*/, width-/*HERE*/), random(/*HERE*/, height-71), 100);
      spaceship4 = new spaceship(mouseX, 125, -1.75, -1.75, random(/*HERE*/, width-/*HERE*/), random(/*HERE*/, height-/*HERE*/), 125);
                              /*(alienX,alienY,velX,  velY,           shipX,                     shipY,                 size)*/
    }
    
    void draw() {
      background(0);
      spaceship1.drawSpaceship();
      spaceship2.drawSpaceship();
      spaceship3.drawSpaceship();
      spaceship4.drawSpaceship();
      spaceship1.xBounce();
      spaceship2.xBounce();
      spaceship3.xBounce();
      spaceship4.xBounce();
      spaceship1.yBounce();
      spaceship2.yBounce();
      spaceship3.yBounce();
      spaceship4.yBounce();
      spaceship1.drawFace();
      spaceship2.drawFace();
      spaceship3.drawFace();
      spaceship4.drawFace();
    }
    
    class spaceship {
      float centerX;
      float centerY;
      float xVelocity;
      float yVelocity;
      float shipX;
      float shipY;
      float size;
    
      spaceship(float tempCX, float tempCY, float tempVX, float tempVY, float tempSX, float tempSY, float sSize) {
        centerX = tempCX;
        centerY = tempCY;
        xVelocity = tempVX;
        yVelocity = tempVY;
        shipX = tempSX;
        shipY = tempSY;
        size = sSize;
    
      }
    
      void drawSpaceship() { 
      //making the spacship
      stroke(0);//black line color
      fill(255);//inside of shape white 
      ellipse(shipX-0,shipY-(size*.36),(size*.4),(size*.4));//the top dome of the spaceship
      fill(180);//grey color of the body
      ellipse(shipX-0,shipY-(size*.16),size,(size*.4));//the main body of the spaceship
      stroke(0);//black line color
      fill(12, 246,255);
      ellipse(shipX,shipY,(size*.12),(size*.04));//the bottom hole on the spaceship
      noStroke();//no lines/outline of the next shapes
      fill(random(255), random(255), random(255));//random color
      ellipse(shipX-0,shipY-(size*.24), (size*.06), (size*.06));//middle light
      ellipse(shipX+(size*.2),shipY-(size*.24), (size*.06), (size*.06));//middle right
      ellipse(shipX-(size*.2),shipY-(size*.24), (size*.06), (size*.06));//middle left
      ellipse(shipX-(size*.44),shipY-(size*.2), (size*.06), (size*.06));//far left 
      ellipse(shipX+(size*.44),shipY-(size*.2), (size*.06), (size*.06));//far right
      shipX = shipX + xVelocity;//keep moving the x intercept by a value
      shipY = shipY + yVelocity;//keep moving the y intercept by a value
    }
    
      void xBounce() {
        if(shipX > width-(size/2)){
          xVelocity*=-1;
        }
        if(shipX<0+(size/2)){
          xVelocity*=-1; 
        }
      }
    
      void yBounce() {
        if(shipY<0+(size/2)){
          yVelocity*=-1;
        }
        if(shipY>height){
          yVelocity*=-1;
        }
      }
    
      void drawFace() {
        centerX = mouseX;
        centerY = mouseY - 40;
        stroke(0);
        fill(28, 221, 50);
        ellipse(centerX, centerY, 40, 80);
        fill(0);
        ellipse(centerX-10 , centerY-10, 10, 30);
        ellipse(centerX+10 , centerY-10, 10, 30);
        line(centerX-5 , centerY+25 ,centerX+5, centerY+25);
    }
    
    
    }
    
  • edited December 2016

    Sorry if i wasn't being clear because i'm a beginner and i don't even know if what i'm even saying even making any sense i'm taking an intro to computer science high school class so i'm very new to all of the concepts.

  • edited December 2016

    It is not clear at all what you are asking. The member variable shipX belongs to your class spaceship. When you do this next:

    spaceship1 = new spaceship(mouseX, 125, 1.75, 1.75, random(/HERE*/71, width-71), random(/HERE*/71, height-71), 50);

    You are assigning the value random(71, width-71) to shipX. The variable shipX cannot be freely access unless you do something like:

    println("The value of shipX in spaceship1 is " + spaceship1 .getX);

    You need to look into exactly what you want to do.

    Kf

  • edited December 2016

    I just realized that my question didn't make sense because I was trying to specify some values of variables for my new objects with a variable that I needed to state in the same line. If there is a way around this please let me know, thanks.

  • I was trying to use the size variable to specify the ships max and minimum x positions when really the starting and ending x was being stated in the same line as size.

  • edited December 2016

    Here's the failure piece of code.

    I tried making the starting x and y intercepts random and with a minimum if more than half of the ship size and the max the width divided by half of the size also so they wouldn't get stuck on spawning but it didn't work.

      spaceship1 = new spaceship(mouseX, mouseY, 1.75, 1.75, random((spaceship1.size/2), width-(spaceship1.size/2)), random((spaceship1.size/2), height-(spaceship1.size/2)), 50);
      spaceship2 = new spaceship(mouseX, mouseY, -1.75, -1.75, random((spaceship2.size/2), width-(spaceship2.size/2)), random((spaceship2.size/2), height-(spaceship2.size/2)), 75);
      spaceship3 = new spaceship(mouseX, mouseY, 1.75, 1.75, random((spaceship3.size/2), width-(spaceship3.size/2)), random((spaceship3.size/2), height-(spaceship3.size/2)), 100);
      spaceship4 = new spaceship(mouseX, mouseY, -1.75, -1.75, random((spaceship4.size/2), width-(spaceship4.size/2)), random((spaceship4.size/2), height-(spaceship4.size/2)), 125);
                            /*((alienX),(alienY),(velX),(velY),(                       shipX                         ),(                         shipY                       ),size)*/
    
  • edited December 2016

    shipX and shipY = spaceship starting point.(random)

    alienX and alienY = alien head starting point.(random)

    size = size of spaceship(not random)

    velX and velY = steps the ship takes.

  • is this solved or do you still have a question?

  • still a question

  • edited December 2016

    wanted to know if their was a way of using size to specify shipX one of the parameters even though size was being given a value on the same line.

  • you can make one var "size" in setup, say its value in setup and pass it to all 4 spaceships

    when size is the same for all ships, you can just state it in the class, in line 48, then you don't pass it

  • so how would I still be able to change the size variable independently throughout the new spaceships?

  • i still need to be able to change size too

  • Answer ✓

    so how would I still be able to change the size variable independently throughout the new spaceships?

    when you want different sizes for the ships, you should pass them separately

    ...a way of using size to specify one of the parameters even though size was being given a value at the same line I was trying to use it.

    what you could do is that you calculate size1 in setup separately and then use it in multiple places in the line with "new"

      float size1; 
    
      size1=50; 
      spaceship1 = new spaceship(mouseX, 125, 1.75, 1.75, random(size1, width-size1), random(size1, height-size1), size1);
    

    (you can use random for size as well)

  • Thanks, that's a good solution i'll take it. ^:)^

  • Just a little repetitive but i bet there's no other way.

  • Answer ✓

    i still need to be able to change size too

    you can change size later with

    spaceship1.size--; 
    

    or so

    by the way:

    spaceship1.drawSpaceship();
      spaceship2.drawSpaceship();
      spaceship3.drawSpaceship();
      spaceship4.drawSpaceship();
      spaceship1.xBounce();
      spaceship2.xBounce();
      spaceship3.xBounce();
      spaceship4.xBounce();
      spaceship1.yBounce();
      spaceship2.yBounce();
      spaceship3.yBounce();
      spaceship4.yBounce();
      spaceship1.drawFace();
      spaceship2.drawFace();
      spaceship3.drawFace();
      spaceship4.drawFace();
    }
    

    you call drawSpaceship, xBounce, yBounce, drawFace for all

    when you make a function script in the class, this can be shortened:

    void script() {
        drawSpaceship(); 
        xBounce(); 
        yBounce(); 
        drawFace();
    }
    

    so in draw for each spaceship you put: spaceship1.script();

  • so script basically means that you want to run every function in the class?

  • yeah, making your draw() much shorter

    the name script can be changed of course

  • edited December 2016

    Oh ok i get it now! Thanks again for making my code so much better.Have you seen my actual sketch lol. You could probably duplicate it in a minute. ^:)^

  • I still don't even know how to use arrays :))

  • Answer ✓

    yeah, arrays would be cool

    another remark: you can hit ctrl-t to auto-format your code (in processing)

  • edited December 2016

    Yea it looks a lot better now thanks!

    spaceship spaceship1;
    spaceship spaceship2;
    spaceship spaceship3;
    spaceship spaceship4;
    
    void setup() {
      size(500, 400);
      background(0);
      smooth();
      frameRate(30);
      strokeWeight(1);
      ellipseMode(CENTER);
      rectMode(CENTER);
      float size1 = 50;
      float size2 = 75;
      float size3 = 100;
      float size4 = 125;
      spaceship1 = new spaceship(mouseX, mouseY, 1.75, 1.75, random((size1/2), width-(size1/2)), random((size1/2), height-(size1/2)), size1);
      spaceship2 = new spaceship(mouseX, mouseY, -1.75, -1.75, random((size2/2), width-(size2/2)), random((size2/2), height-(size2/2)), size2);
      spaceship3 = new spaceship(mouseX, mouseY, 1.75, 1.75, random((size3/2), width-(size3/2)), random((size3/2), height-(size3/2)), size3);
      spaceship4 = new spaceship(mouseX, mouseY, -1.75, -1.75, random((size4/2), width-(size4/2)), random((size4/2), height-(size4/2)), size4);
                            /*((alienX),(alienY),(velX),(velY),(             shipX                ),(             shipY              ),size)*/     
    }
    
    void draw() {
      background(0);
      spaceship1.fullScript();
      spaceship2.fullScript();
      spaceship3.fullScript();
      spaceship4.fullScript();
    }
    
    class spaceship {
      float centerX;
      float centerY;
      float xVelocity;
      float yVelocity;
      float shipX;
      float shipY;
      float size;
    
      spaceship(float tempCX, float tempCY, float tempVX, float tempVY, float tempSX, float tempSY, float sSize) {
        centerX = tempCX;
        centerY = tempCY;
        xVelocity = tempVX;
        yVelocity = tempVY;
        shipX = tempSX;
        shipY = tempSY;
        size = sSize;
    
      }
    
      void drawSpaceship() { 
      //making the spacship
      stroke(0);//black line color
      fill(255);//inside of shape white 
      ellipse(shipX-0,shipY-(size*.36),(size*.4),(size*.4));//the top dome of the spaceship
      fill(180);//grey color of the body
      ellipse(shipX-0,shipY-(size*.16),size,(size*.4));//the main body of the spaceship
      stroke(0);//black line color
      fill(12, 246,255);
      ellipse(shipX,shipY,(size*.12),(size*.04));//the bottom hole on the spaceship
      noStroke();//no lines/outline of the next shapes
      fill(random(255), random(255), random(255));//random color
      ellipse(shipX-0,shipY-(size*.24), (size*.06), (size*.06));//middle light
      ellipse(shipX+(size*.2),shipY-(size*.24), (size*.06), (size*.06));//middle right
      ellipse(shipX-(size*.2),shipY-(size*.24), (size*.06), (size*.06));//middle left
      ellipse(shipX-(size*.44),shipY-(size*.2), (size*.06), (size*.06));//far left 
      ellipse(shipX+(size*.44),shipY-(size*.2), (size*.06), (size*.06));//far right
      shipX = shipX + xVelocity;//keep moving the x intercept by a value
      shipY = shipY + yVelocity;//keep moving the y intercept by a value
    }
    
      void xBounce() {
        if(shipX > width-(size/2)){
          xVelocity*=-1;
        }
        if(shipX<0+(size/2)){
          xVelocity*=-1; 
        }
      }
    
      void yBounce() {
        if(shipY<0+(size/2)){
          yVelocity*=-1;
        }
        if(shipY>height){
          yVelocity*=-1;
        }
      }
    
      void drawFace() {
        centerX = mouseX;
        centerY = mouseY - 40;
        stroke(0);
        fill(28, 221, 50);
        ellipse(centerX, centerY, 40, 80);
        fill(0);
        ellipse(centerX-10 , centerY-10, 10, 30);
        ellipse(centerX+10 , centerY-10, 10, 30);
        line(centerX-5 , centerY+25 ,centerX+5, centerY+25);
    }
    
    void fullScript() {
    
      drawSpaceship();
      xBounce();
      yBounce();
      drawFace();
    
    }
    
    
    
    }
    
  • no use to pass mouseX, mouseY in setup(), they are just 0 there anyway

    say centerX = 0; in the constructor

  • edited December 2016

    ok got it thanks again =D>

  • sure!

    well done!

Sign In or Register to comment.