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 › Help with classes, program isn't running
Pages: 1 2 
Help with classes, program isn't running (Read 2835 times)
Help with classes, program isn't running
Mar 2nd, 2010, 6:14pm
 
I'm a pretty new programmer working on a project involving classes. I want to create a Tigger-like creature that bounces (using gravity). I haven't gotten that far yet unfortunately. When I run the following code, I get a "expecting IDENT, found ','" error.

Code:
int width=800;
int height=600;
float xpos=400;
float ypos=200;
float headwidth=60;
float headheight=100;
float nosewidth=40;
float noseheight=30;
float noseoffset=30;
float bodywidth=60;
float bodyheight=200;
float bodyoffset=150;
float direction=1;
float speed=4;
float levelofshake=5;
float bounciness=5;
float white=255;
float redness=255;
float greenness=140;
float blueness=0;

Creature tigger;

void setup() {
 size(width,height);
 smooth();
 tigger=new Creature(xpos,ypos,direction,speed);
}

void draw() {
 background(white);
 tigger.display();
 //tigger.bounce(bounciness);
 //tigger.shake(levelofshake);
 //tigger.talk();
}

class Creature {
 Creature(tempXpos,tempYpos,tempDirection,tempSpeed) {
   xpos=tempXpos;
   ypos=tempYpos;
   direction=tempDirection;
   speed=tempSpeed;
 }
}

 void display() {
   fill(redness,greenness,blueness);
   ellipse(xpos-noseoffset,ypos,nosewidth,noseheight); //ellipse for the nose
   ellipse(xpos,ypos,headwidth,headheight); //ellipse for the head
   ellipse(xpos,ypos+bodyoffset,bodywidth,bodyheight); //ellipse for the body
 }
   


I have some questions about this code that I can hopefully get answered here.

I'm wanting to have four customizable attributes of my creature, hence the xpos, ypos, direction and speed found when I initialized my object. I also don't want to have any hard-coded variables, so I put them all at the top of my code. I'll set them as global variables instead of variables within my classes, as I may use some in each of my classes. With this being the case, wouldn't all my attributes be easily customizable, not only the four I mentioned?

I have put my other three user-defined functions (bounce, shake and talk) into comments as I'll worry about those when the time comes. As for now, I just want my creature (so far just a head, nose and body [I'll improve them when the time comes]) to be displayed properly, yet my program isn't even running and I don't know why. No where in the appendix of Daniel Shiffman's book does it mention this area.

May I also know the basic things that are wrong with my code please? I'm only up to ch. 8 (on objects) in Shiffman's book, so I won't be able to fully understand coding from beyond that point. (When I try to make my creature talk, by importing an audio file from somewhere, I'll look ahead and read how to do it).

Thanks very much!  Smiley

 
Re: Help with classes, program isn't running
Reply #1 - Mar 2nd, 2010, 6:56pm
 
The display method needs to be inside your Creature class if you want to call it as tigger.display();

All of the attributes of the creature should be inside the class, too... that's the whole point of having a class - it contains all the attributes and methods of the objects so that the main class doesn't have to worry about it. It just tells the object to draw itself and it runs the right method and looks up the attributes and does it. This is called "encapsulation" - which sort of means you don't have to worry about how a class does something when you ask it to.....
Re: Help with classes, program isn't running
Reply #2 - Mar 2nd, 2010, 7:07pm
 
Thanks Giles. Unfortunately when I put my display(); function into my Creature class, it still gives me the same error. Below is the code for my class with the display(); function in it (ignoring the code outside the class).

Code:

class Creature {
Creature(tempXpos,tempYpos,tempDirection,tempSpeed) {
xpos=tempXpos;
ypos=tempYpos;
direction=tempDirection;
speed=tempSpeed;
}

void display() {
fill(redness,greenness,blueness);
ellipse(xpos-noseoffset,ypos,nosewidth,noseheight); //ellipse for the nose
ellipse(xpos,ypos,headwidth,headheight); //ellipse for the head
ellipse(xpos,ypos+bodyoffset,bodywidth,bodyheight); //ellipse for the body
}
}


I also tried putting the display(); function in with where I defined my variables to each other (xpos=tempXpos, ect), but that gave me the same error.
Re: Help with classes, program isn't running
Reply #3 - Mar 2nd, 2010, 7:45pm
 
Yes, I see now... you need to put the identifiers for the variables in your constructor  - float tempXpos, float tempYpos, etc.
Re: Help with classes, program isn't running
Reply #4 - Mar 2nd, 2010, 9:31pm
 
Thanks again  Smiley The answer seems to obvious now that you said it. But I guess that's how it is in programming.

As I said, I'd like to make my Tigger bounce with the gravity effect mentioned in Shiffman's book. I like to think I'm pretty good with making simple objects bounce, yet my Tigger won't be simple (hopefully). Will I have to make every individual part of Tigger bounce, or is there some way around that, where I can make the entire Tigger object bounce? I've read through his chapter on objects several times now and cannot find anything regarding it.

Is it normal to have hard-coded numbers as our function arguments? All the examples in his book give hard-coded numbers, yet my computer professor would like no hard-coded numbers at all. Would I only define the parameters in the class itself? I tried defining all my variables within class and it wouldn't run.
Re: Help with classes, program isn't running
Reply #5 - Mar 3rd, 2010, 9:04am
 
Hi again....

Yes, you can make the whole object bounce by carefully building up the methods inside the class so that it "knows" how to draw the object from the separate parts, and then knows how to move the whole object. If your world has gravity then a "bounce" could just be a sharp acceleration upwards, and then allowing the gravity to pull the object back down again.

You still need to put all those object parameters in the class itself....then put them in the constructor too so you can set them when the object is instantiated...
Re: Help with classes, program isn't running
Reply #6 - Mar 3rd, 2010, 12:18pm
 
I'm still not certain what variables to put into my class and what to put outside my class. Will the number of variables within my class be greater than the number of parameters the object takes?

And how do I go about have my project make noise? I don't see anything on the reference here regarding importing sound/audio.
Re: Help with classes, program isn't running
Reply #7 - Mar 3rd, 2010, 1:12pm
 
float white should be a global variable. All others can be put in your class.

You may use PVector to store 2D coordinates, instead of 2 (x, y) variables.

For sound output, see the Minim library, either in reference/ librairies / Minim or learning / librairies Minim.
Re: Help with classes, program isn't running
Reply #8 - Mar 3rd, 2010, 1:30pm
 
When I put all variables besides float white into my class, I get an error saying "Cannot find anything named xpos" on the line where I initialized my object. I assume this is because my argument values aren't hard-coded. Do I need hard-coded values for my arguments?

Code:
tigger=new Creature(xpos,ypos,direction,speed); 



I just downloaded the Mini 2.0.2 full distribution and have to say I am thoroughly confused by it. I found a "groove.mp3" file within the Minim folder and it sounded groovy enough that I'd like it in my project.

Judging from the help given on that site, I would say that something like this:
Code:
import ddf.minim.*;

Minim minim;
AudioPlayer song;

void setup()
{
size(100, 100);

minim = new Minim(this);

// this loads mysong.wav from the data folder
song = minim.loadFile("groove.mp3");
song.play();
}

void draw()
{
background(0);
}

void stop()
{
song.close();
minim.stop();

super.stop();
}


would play that file. Yet, it doesn't, saying that Minim is not defined.

I'm not looking for anything fancy. Just a simple example, that works, that I can run in my own Processing screen, so I can see how to use it. Thanks.
Re: Help with classes, program isn't running
Reply #9 - Mar 3rd, 2010, 2:07pm
 
I suggest you use hard-coded values indeed. Go simple.

Code:
tiger = new Creature(400, 300, 1, 10); 




You could also set those values in the constructor.

Code:
tiger = new Creature();

class Creature {
 
 float xpos, ypos, direction, speed;
 
 Creature() {
   this.xpos = 400;
   this.ypos = 300;
   this.direction = 1;
   this.speed = 10;
 }
 
}



You could also have 2 constructors :

Code:
// this one will get default values
tiger = new Creature();

// this one has custom values
tiger2 = new Creature(10, 10, 0, 4);

class Creature {
 
 float xpos, ypos, direction, speed;
 
 // if no arguments specified, set default values
 Creature() {
   Creature(400, 300, 1, 10);
 }
 
 // set the parameters from arguments
 Creature(float x, float y, float d, float s) {
   this.xpos = x;
   this.ypos = y;
   this.direction = d;
   this.speed = s;
 }
 
}
Re: Help with classes, program isn't running
Reply #10 - Mar 3rd, 2010, 2:48pm
 
Thank you Plastik, that was helpful for me.

In trying to "start from scratch", I want to apply the gravity effect to just a ball, which will be called Tigger.

I used hard-coded numbers as you suggested. In trying to run the following code, I received an error message I've never seen before:"Syntax error, insert 'AssignmentOperator Expression' to complete Expression." I don't know what I could have done wrong, unless my xpos and ypos only work within the display() function. In which case I would expect a different error message.
Code:
float gravity=.1;

Creature tigger;

void setup() {
size(800,600);
smooth();
tigger=new Creature(10);
}

void draw() {
background(255);
tigger.display(400,300);
tigger.move;
}

class Creature {
float speed;
Creature(float tempSpeed) {
speed=tempSpeed;
}

void display(float xpos,float ypos) {
fill(255,140,0);
ellipse(xpos,ypos,100,100);
}

void move() {
ypos=ypos+speed;
speed=speed+gravity;
}
}

Re: Help with classes, program isn't running
Reply #11 - Mar 3rd, 2010, 3:06pm
 
The problem is in tigger.move;
Because it is a function you are calling, it needs the empty brackets:
 tigger.move();
When functions are called they always have to have the brackets - with parameters, if the function takes parameters, or empty if it doesn't. The brackets are the 'AssignmentOperator Expression'.
Re: Help with classes, program isn't running
Reply #12 - Mar 3rd, 2010, 5:43pm
 
Thanks. I should have seen that. I've read in Shiffman's book that when you're frustrated with programming, you should take a break. I didn't do that, and as a consequence failed to see my mistake.

I edited a post further up here regarding the Mimim audio libraries. I'm trying to get a *boing* sound whenever Tigger hits the ground and bounces back up. I can't imagine this would be too hard to do (I say that now).

I've managed to get a picture to be my background image. It looks nice and all but my bouncing object is now very slow. I'm guessing this is due to the background image having to re-draw itself 30 times every second (I put it in my draw(); function). Of course I can't put it in my setup() function or it wouldn't remain. What do I do?  Undecided
Re: Help with classes, program isn't running
Reply #13 - Mar 3rd, 2010, 10:00pm
 
You're not doing something like loading the image from disk every frame are you? You need to have the image as a global variable and just load the image once in setup, and then do background(image) every frame. It should be really fast. Also, make sure the image dimensions are the same as the size of the sketch.
Re: Help with classes, program isn't running
Reply #14 - Mar 9th, 2010, 4:52pm
 
Thank you! My program has really evolved I feel. I have a background picture imported as well as some text, but I'll leave those out of the following code. I have two questions:

1. How do I make them not jump as high? I tried lowering my speed variable to .0001 yet that barely seems to make a difference.

2. How do I avoid having my creatures get "stuck" on the ground level? Without fail, one of my smilies gets stuck on the second or third bounce, then more get stuck after it. I think it has something to do with my random() functions, yet I can't pinpoint where.
Code:
//I set my global variables
int width=800;
int height=600;

float wbody=200;
float hbody=200;
float woutereye=120;
float houtereye=90;
float winnereye=60;
float hinnereye=45;
float wsmile=120;
float hsmile=80;
float wblink=90;
float hblink=75;
float groundlevel=500;
float mouththickness=12;
float outereyeoffset=50;
float innereyeoffset=50;
float smileoffset=20;
float eyefollowmouse=250;
float slowdowneyemovement=20;
float gravity=.05;
float white=255;
float black=0;
float redness=100;
float greenness=150;
float fontsize=32;
float xposfont=15;
float yposfont=30;

Robots[] robots=new Robots[20];

void setup() {
size(width,height);
smooth();
println("Press 'b' to see the creatures blink.");

for (int i=0; i<robots.length; i++) {
robots[i] = new Robots(color(i*2,random(255),random(255)),random(width),random(-1000,-100),.0001);
}
}

void draw() {
background(255);

for (int i=0; i<robots.length; i++) {
robots[i].display();
robots[i].move();
robots[i].sway(PI/4);
robots[i].blink();
}
}


Class:
Code:
//I begin my class
class Robots {
color c;
float xpos;
float ypos;
float speed;

//My class parameters are paired with the Robots class arguments from setup()
Robots(color tempC, float tempXpos, float tempYpos, float tempSpeed) {
c=tempC;
xpos=tempXpos;
ypos=tempYpos;
speed=tempSpeed;
}

void display() {
noStroke();
fill(c);
ellipse(xpos,ypos,wbody,hbody); //body
strokeWeight(mouththickness);
fill(white);
ellipse(xpos,ypos-outereyeoffset,woutereye,houtereye); //outer eye
fill(redness,greenness,mouseY);
ellipse(xpos-(eyefollowmouse-mouseX)/slowdowneyemovement,ypos-innereyeoffset,winnereye,hinnereye); //inner eye
stroke(white);
noFill();
arc(xpos,ypos+smileoffset,wsmile,hsmile,0,PI); //smile
}

void move() {
ypos=ypos+speed;
speed=speed+gravity;
if (ypos>groundlevel) {
speed=speed*-.95; //This achieves the gravity effect. Mulitplying by -.95 decreases the bounce level each bounce.
}
}

/*This allows my robots to have some sort of random movement. I did not include random y-movement as I found the robots often got "stuck"
on the bottom fo the screen*/
void sway(float swaylevel) {
xpos=xpos+random(-1,1)*swaylevel;
}

//If the 'b' button is pressed, the creatures will blink
void blink() {
if (keyPressed) {
if (key=='b'||key=='B') {
fill(white);
ellipse(xpos,ypos-innereyeoffset,wblink,hblink);
}
}
}
}

Pages: 1 2