Hi I've only been learning Processing for around a week now along with Arduino. Sorry for the ignorant question...for the sake of learning I'm trying to use a potentiometer to move a little ellipse up and down on the xaxis to dodge rectangles moving along the x axis. I can move the ball up and down and get random rectangles to move by but I can't get them to crash. I'm using the processing.serial* library and I have a Car class (taken from the Learning example on objects)
How do you make the two classes talk to each other? How can you share the variables outside of the methods? Am I close or would I have to totally rewrite this?
Below is my newbie code.
Thanks for any advice!
Code:
Car myCar1;
Car myCar2; // Two objects!
//Person myPerson1;
import processing.serial.*;
PFont font;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
void setup () {
// set the window size:
size(400, 300);
print("game started");
myCar1 = new Car(color(255,0,0),0,100,2);
myCar2 = new Car(color(0,0,255),0,200,1);
myPort = new Serial(this, Serial.list()[1], 9600);
myPort.bufferUntil('\n');
// set inital background:
background(0);
font = loadFont("Aharoni-Bold-48.vlw");
textFont(font, 32);
text("race game", 15, 50);
}
void draw () {
// everything happens in the serialEvent()
myCar1.drive();
myCar1.display();
myCar2.drive();
myCar2.display();
// myCar1.crash();
// myCar2.crash();
}
class Car {
color c;
float xpos;
float ypos;
float xspeed;
// The Constructor is defined with arguments.
Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) {
c = tempC;
xpos = tempXpos;
//ypos = tempYpos;
xspeed = tempXspeed;
}
void display() {
stroke(0);
fill(c);
rectMode(CENTER);
rect(xpos,ypos,20,10);
}
void drive() {
xpos = xpos + xspeed;
if (xpos > width) {
xpos = 0;
ypos = random(height);
}
}
}
void serialEvent (Serial myPort) {
background(255);
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);
// draw the line:
stroke(0);
fill(200, 100, 29);
ellipse(100, inByte, 20, 20);
}
}
//this won't work because it's outside of everything. how can I include it?
if ((inByte ==ypos)&&(xpos==100)) {
print("game over");
ellipse(100, 100, 100, 100);
background(0);
break;
}