How to draw different models of a class as the object of that class moves in different directions.

edited November 2013 in How To...

I'm learning how to make classes/objects and I am trying to recreate kirby in a very basic form. I used the void display to set up how the object would look normally. I added the arrow keys and movement for all four directions, and now I want to change how the model looks when kirby moves in different directions. The only problem now is that when I try to draw the object under each arrow key, processing won't recognize variables that I used in the class setup. Now I'm stuck because I don't know how to re-draw the object differently depending on which key I press.

Here is the code.

NOTE - I am very much an amateur, and there will be a bunch of holes in my code, so if any help could be stated as simply as possible thanks! There is a bunch of code there that isn't being used yet either.

CLASS SETUP

// writing a class 

class Kirby {

  //floats for rectangle/ellipse size
  float ellipse_size;

  Kirby() { //the constructor, best place to initialize a field 

    ellipse_size = 100;
  }

  void display(float xcen, float ycen) { //establish variables for the points where the shapes are drawn//

    fill(#F58FC2);
    ellipse(xcen-40,ycen,40,30);
    ellipse(xcen+40,ycen,40,30);//kirbys arms

    fill(#9B1E1E);
    arc(xcen-20, ycen+40, 40, 30, 0, PI/.5, CHORD);//kirbys feet  
    arc(xcen+20, ycen+40, 40, 30, 0, PI/.5, CHORD);

    stroke(0);
    fill(#F58FC2);
    ellipse(xcen, ycen, ellipse_size, ellipse_size);//kirbys body

    fill(0);
    ellipse(xcen-10, ycen-15, 10, 30);//kirbys eye sockets
    ellipse(xcen+10, ycen-15, 10, 30);

    fill(255);
    ellipse(xcen-10, ycen-22, 10, 15);//kirbys pupils
    ellipse(xcen+10, ycen-22, 10, 15);

    fill(#483989);
    ellipse(xcen-10, ycen-6, 9, 12);//eye shadow
    ellipse(xcen+10, ycen-6, 9, 12);

    fill(0);
    ellipse(xcen-11, ycen-10, 6, 6);//eye shadow hard to draw cleanly..
    ellipse(xcen+9, ycen-10, 6, 6);

    noStroke();
    fill(#FF4B4B);
    ellipse(xcen-22, ycen, 15, 8);  // blush cheeks
    ellipse(xcen+22, ycen, 15, 8);

    stroke(0);
    fill(#C42222);
    arc(xcen, ycen+8, 20, 20, 0, PI, CHORD); //kirbys mouth


}
  void setSize(float size1, float size2)//adjust size in object
  {

    ellipse_size = size1;
  }
}

AND NOW FOR THE OBJECT SETUP 

Kirby sq1 = new Kirby();
Kirby sq2 = new Kirby();
Kirby sq3 = new Kirby();

PImage img;

void setup() {
  size(600, 600);
 img = loadImage("kirby level background.jpg");
}

float x = width;
float y = height;
void draw() {
  image(img, 0, 0, width, height);
  //sq1.setSize(width/4.3,height/4); setSize has not been established in the class_setup (doesn't exist yet)
  sq1.display(x+50, y+50); 
  {
    if (keyPressed && (key == CODED)) { //use left arrow to move 
      if(keyCode == LEFT) {
      x-= 2;
      }


}


     if (keyPressed && (key == CODED)) { //use right to arrow to move
      if(keyCode == RIGHT) {
       x+=2;
      y+=0;
    }
     stroke(0);   //need to redraw kirby as moving right instead of stand still
    fill(#F58FC2);
    // WONT WORK - ellipse(xcen, ycen, ellipse_size, ellipse_size);//kirbys body


 }

     if (keyPressed && (key == CODED)) { //
      if (keyCode == DOWN) { 
        y+= 2;
      }
    }
     if (keyPressed && (key == CODED)) {
      if (keyCode == UP) { 
        y-= 2;
      }
    }
  }
  //sq2.setSize(200, 150); //using setSize to make sq2 bigger/smaller
  //sq2.display(100, 100);
}

Answers

  • edited November 2013

    float x = width;
    float y = height;

    These are global variables, initialized before setup() is run, so width and height are zero at this point! You have to separate declaration and initialization and move the latter to setup().

    Beside, actually, you should move these variables to the Kirby class, so that each Kirby keeps its own position. This way, no need to pass them to display().

    "processing won't recognize variables that I used in the class setup'
    I see only ellipse_size variable in the class, so I am not sure of why you have a problem.

Sign In or Register to comment.