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 & HelpPrograms › Beginners problems with OOP
Page Index Toggle Pages: 1
Beginners problems with OOP (Read 632 times)
Beginners problems with OOP
Jun 8th, 2009, 11:29am
 
I'm working with the lesson for object oriented programming, and I'm trying to add interactivity, but I've run up against a problem. I don't know how to check for outside events, like a key being pressed, and assign that to a variable within a class.

Here's my code so far:
Code:

Car myCar;

void setup() {
size(200,200);
// Parameters go inside the parentheses when the object is constructed.
myCar = new Car(color(255,0,0),0,100,0,0);
}

void draw() {
background(255);
myCar.move();
myCar.display();
}

// Even though there are multiple objects, we still only need one class.
// No matter how many cookies we make, only one cookie cutter is needed.
class Car {
color c;
float xpos;
float ypos;
float xspeed;
float yspeed;

// The Constructor is defined with arguments.
Car(color tempC, float tempXpos, float tempYpos, float tempXspeed, float tempYspeed) {
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
xspeed = tempXspeed;
yspeed = tempYspeed;
}

void display() {
stroke(0);
fill(c);
rectMode(CENTER);
rect(xpos,ypos,20,10);
}

void move() {
xpos = xpos + xspeed;
if (xpos > width) {
xpos = 0;
}
if (xpos < 0) {
xpos = width;
}
ypos = ypos + yspeed;
if (ypos > height) {
ypos = 0;
}
if (ypos < 0) {
ypos = height;
}
}

void direction() {
if (keyPressed) {
if (key == 'A') {
xspeed = -1;
}
if (key == 'D') {
xspeed = 1;
}
if (key == 'W') {
yspeed = 1;
}
if (key == 'S') {
yspeed = -1;
}
}
}
}
Re: Beginners problems with OOP
Reply #1 - Jun 8th, 2009, 1:45pm
 
There was a similar post not long ago that used booleans to catch the key presses:
http://processing.org/discourse/yabb2/num_1243835922.html

Aside from that, you could make your classes listen for mouse events...
http://java.sun.com/docs/books/tutorial/uiswing/events/mouselistener.html
Re: Beginners problems with OOP
Reply #2 - Jun 8th, 2009, 4:27pm
 
Embarrassed
Having looked at it again my assumptions were clearly incorrect - I should have paid more attention to the problems with the originally posted code.
I'd noticed that the OP hadn't invoked the 'direction' method in the draw loop, but hadn't noticed that the keyPressed condition checked against CAPITALS... [slaps forehead]

The boolean approach is only really relevant if you want motion only for as long as the key is pressed.

Code:
Car myCar;

void setup() {
 size(200,200);
 // Parameters go inside the parentheses when the object is constructed.
 myCar = new Car(color(255,0,0),0,100,0,0);
}

void draw() {
 background(255);
 myCar.move();
 myCar.display();
 myCar.direction();
}

// Even though there are multiple objects, we still only need one class.
// No matter how many cookies we make, only one cookie cutter is needed.
class Car {
 color c;
 float xpos;
 float ypos;
 float xspeed;
 float yspeed;

 // The Constructor is defined with arguments.
 Car(color tempC, float tempXpos, float tempYpos, float tempXspeed, float tempYspeed) {
   c = tempC;
   xpos = tempXpos;
   ypos = tempYpos;
   xspeed = tempXspeed;
   yspeed = tempYspeed;
 }

 void display() {
   stroke(0);
   fill(c);
   rectMode(CENTER);
   rect(xpos,ypos,20,10);
 }

 void move() {
   xpos = xpos + xspeed;
   if (xpos > width) {
xpos = 0;
   }
   if (xpos < 0) {
xpos = width;
   }
   ypos = ypos + yspeed;
   if (ypos > height) {
ypos = 0;
   }
   if (ypos < 0) {
ypos = height;
   }
 }
 
 void direction() {
   if (keyPressed) {
if (key == 'a') {
 xspeed = -1;
}
if (key == 'd') {
 xspeed = 1;
}
if (key == 'w') {
 yspeed = 1;
}
if (key == 's') {
 yspeed = -1;
}
   }
 }
}
Page Index Toggle Pages: 1