We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › using "this" as an argument in Java mode
Page Index Toggle Pages: 1
using "this" as an argument in Java mode (Read 503 times)
using "this" as an argument in Java mode
Dec 8th, 2009, 4:05pm
 
Hi,

I'm on chapter 14 of the Ira Greenberg book and I'm beginning to understand this PApplet stuff, but there's one part I've gotten stuck on.

Here's the code in the main tab:

Quote:
Bird b;


void setup(){
  size(400, 400, P3D);
  noStroke();
  b = new Bird();
}


void draw(){
  background(152, 223, 255);
  lights();
  b.fly();
}



and here's the code for the Java class:

Quote:
import processing.core.PApplet;

public class Bird{

  private PApplet p;
  private float offsetX, offsetY, offsetZ;
  private float w, h;
  private int bodyFill;
  private int wingFill;
  private float ang = 0, ang2 = 0, ang3 =0, ang4 = 0;
  private float radiusX = 120, radiusY = 200, radiusZ = 700;
  private float rotX = 15, rotY = 10, rotZ = 5;
  private float flapSpeed = .4f;
  private float rotSpeed = .1f;

  public Bird(PApplet p){
    this(p, 0, 0, 0, 60, 80);
  }

  public Bird(PApplet p, float offsetX, float offsetY,
  float offsetZ, float w, float h){
    this.p = p;
    this.offsetX = offsetX;
    this.offsetY = offsetY;
    this.offsetZ = offsetZ;
    this.h = h;
    this.w = w;
    bodyFill = p.color(200, 100, 10);
    wingFill = p.color(200, 200, 20);
  }

  public void setOffsetX(float offsetX){
    this.offsetX = offsetX;
  }

  public float getOffsetX(float offsetX){
    return offsetX;
  }

  public void setColor(int bodyFill, int wingFill){
    this.bodyFill = bodyFill;
    this.wingFill = wingFill;
  }

  public void setFlight(float radiusX, float radiusY, float radiusZ,
  float rotX, float rotY, float rotZ){
    this.radiusX = radiusX;
    this.radiusY = radiusY;
    this.radiusZ = radiusZ;

    this.rotX = rotX;
    this.rotY = rotY;
    this.rotZ = rotZ;
  }

  public void setWingSpeed(float flapSpeed){
    this.flapSpeed = flapSpeed;
  }

  public void setRotSpeed(float rotSpeed){
    this.rotSpeed = rotSpeed;
  }

  public void fly(){
    p.pushMatrix();
    float px, py, pz;
    p.fill(bodyFill);

    //flight
    px = p.sin(p.radians(ang3))*radiusX;
    py = p.sin(p.radians(ang3))*radiusY;
    pz = p.sin(p.radians(ang4))*radiusZ;
    //

    p.translate(p.width/2+offsetX+px, p.height/2+offsetY+py,
    -500+offsetZ+pz);

    p.rotateX(p.sin(p.radians(ang2))*rotX);
    p.rotateY(p.sin(p.radians(ang2))*rotY);
    p.rotateZ(p.sin(p.radians(ang2))*rotZ);

    //body
    p.box(w/5, h, w/5);

    p.fill(wingFill);
    //left wing
    p.pushMatrix();
    p.rotateY(p.sin(p.radians(ang))*20);
    p.rect(0, -h/2, w, h);
    p.popMatrix();

    //right wing
    p.pushMatrix();
    p.rotateY(p.sin(p.radians(ang))*-20);
    p.rect(-w, -h/2, w, h);
    p.popMatrix();

    //wing flap
    ang += flapSpeed;
    if (ang > 3){
      flapSpeed *= -1;
    }
    if (ang < -3) {
      flapSpeed *= -1;
    }

    // ang's run trig functions
    ang2 += rotSpeed;
    ang3 += .7;
    ang4 += .55;
    p.popMatrix();
  }
}


Re: using "this" as an argument in Java mode
Reply #1 - Dec 8th, 2009, 4:08pm
 
The book says to add "this" as an argument to  b = new Bird(); in order to get the code to work. However, the code works fine as is, but as soon as I add the "this" it stops working.

Is the "this" not actually necessary?
Re: using "this" as an argument in Java mode
Reply #2 - Dec 8th, 2009, 4:57pm
 
Did you name the class Bird.java
Re: using "this" as an argument in Java mode
Reply #3 - Dec 8th, 2009, 5:19pm
 
Ok it's working, but I could've sworn I named the tab Bird.java the first time around. I think my brain's just fried for today.
Page Index Toggle Pages: 1