variables & storing them

I have a 2 part question:

Part 1 is there a way to store the variables other than using a class or putting in the setup and still have the arguments not have numbers.

Part 2 What if I want to make the bird more complex buy adding more shapes and I did not want to add more variables to setup.

float x, y, w, h,dx;
int n;

void setup (){
 //setup//
 size (600,600);
  x=  600;
  y=  500;
  w=  20;
  h=  10;
  dx=1;
}



void draw() {
  background (255,255,255);
  bird( x,y,w,h);
  moveBird(dx);
}





void bird( float x, float y, float w, float h){
  //// draw a bird
  fill(0);
  rect( x,y, w,h );
}

void moveBird(float dx){
  if (x>width)
  {
    x = x-dx;//take dx and -1 untill 0 then run the next if
  }
  if (x <= -100) {
    x=600;// sets this back to 600 so witch can fly from the right
  }
}

Answers

  • beware that the x = 0 on the end of line 33 means that block on lines 34-36 ISN'T conditional.

    a class is the obvious way to do what you're doing - all those variables relate to the bird so store them as part of a bird object. what would happen if you had 2 birds? 200 birds? 2000 birds?

  • Ty for the info.

  • edited October 2014

    you could use PVector instead of x,y and w,h

    see reference for PVector

    you could pass pos and size as PVectors to the function bird then

    also you could use arrays of PVector

    you could even pass an entire array to a function

    In the long run

    but koogsis right, a class is the way to go in the long run

    you wouldn't have a single var in setup()

    class / OOP is very simple and we can lead you through

    do the 6th tutorial, it's about OOP

    Best, Chrisir ;-)

Sign In or Register to comment.