hi, i'm continuing this game, and i'm adding in some stages to progress through. Right now the error that I keep running into but don't really see is a stack overflow, or pushMatrix comes up more than 32 times? i'm trying to make it so that after you get to stage two.. which works fine for me.. it draws out the game playing as though you just ran the program again. I have the top cord and bottom cords as voids, just for ease, and the target at the bottom is as well. When I add in the target once I go to the second stage, it is drawn just fine, yet adding in the oscillating cords, is what is causing the error. Here is the code so far:
Code:
int yval = 200;
int sx = 50; //end of second 'arm' with circle swinging on it
int sy = 0; //incrementing variable for length of second 'arm' (extends y length)
int tx = 50; //coordinate for line @ bottom of screen
int ty = 435; // coordinate for line @ bottom of screen
int erads = 15; //radii of circles
int timer = 255;
int stagetime = 0;
PFont font;
boolean isStuck = false;
float cord = (yval * .5); //line with fixed point @ top of screen
float theta = 0.0; //angular motion
float psi = 0.0; //angular motion
float scalar = 1.0;
void setup() {
size(320, 480);
smooth();
font = loadFont("Helvetica-24.vlw");
ellipseMode(RADIUS);
}
void draw() {
textFont(font);
background(225);
//println(frameRate);
//println(sy);
translate(width/2, 0);
float angle = (2 + sin(theta)) * QUARTER_PI;
// float x = cord * cos(angle);
// float y = cord * sin(angle);
float sangle = (2 + scalar * cos(psi)) * HALF_PI;
float x2 = (cord) * cos(sangle);
float y2 = (cord) * sin(sangle);
theta += 0.06;
psi += 0.02;
//top line
topCord();
//bottom arm
bottomCord();
float theX = screenX(x2, y2); //grab coords for 'wrecking-ball'
float theY = screenY(x2, y2);
//println(theX + " " + theY);
popMatrix();
popMatrix();
//bottom target
target();
//balls after collision
if (isStuck == true){
noStroke();
fill(39, 148, 201);
ellipse(theX-width/2, theY, erads, erads);
ellipse(theX-width/2 + 12, theY + 12, erads, erads);
scalar -= 0.005; //change decrement
if (scalar <= 0){
scalar = 0;
}
}else{
ellipse(theX-width/2, theY, erads, erads);
ellipse(tx, ty, erads, erads);
}
if(scalar == 0){
//delay(500);
timer -= 10;
background(timer);
if(timer <= 15){
timer = 20;
}
}
if((scalar == 0)&&(timer == 20)){
stage2();
}
if (circleCircleIntersect(tx, ty, //checking if the swinging circle touches the target @ bottom
erads, theX-width/2, theY, erads) == true){
isStuck = true;
//stage2();
}
else {
fill(0);
}
}
boolean circleCircleIntersect(float cx1, float cy1, float cr1, float cx2, float cy2, float cr2) {
if (dist(cx1, cy1, cx2, cy2) < (cr1 + cr2)) {
return true;
}
else {
return false;
}
}
void bottomCord(){
float sangle = (2 + scalar * cos(psi)) * HALF_PI;
float x2 = (cord) * cos(sangle);
float y2 = (cord) * sin(sangle);
pushMatrix();
translate(100, 0);
strokeWeight(1);
rotate(sangle*scalar + (1-scalar) * 0.65 * PI); //change angle to make it move differently(independently)
if ((mousePressed == true)&&(mouseButton == LEFT)){
cord += 2;
}
else if (!(mousePressed == true)&&(cord >= 100)){
cord -= 2;
}
line(0, 0, x2, y2); //second/smaller arm
}
void topCord(){
float angle = (2 + sin(theta)) * QUARTER_PI;
fill(0);
stroke(0);
pushMatrix();
rotate(angle*scalar + (1-scalar) * 0.5 * PI);
strokeWeight(2);
line(0, 0, 100, 0); // top cord
}
void target(){
//bottom target
strokeWeight(2);
stroke(0);
line(tx, ty, tx, height); //line(x, y, a, b); y = length
//x&a = end of line
//if y = 480; it's off screen
println(timer);
}
void stage2(){
stagetime++;
fill(255);
textSize(24);
text("Stage 2!", -50, height/2);
if (stagetime >= 100){
background(225);
erads = 10;
//change other aspects of gameplay for second stage here
target();
//topCord();
//bottomCord();
}
}
That is what is being run so far, and i see that the two pop matrices are outside of the void, but is there something I can do to leave it like that and not have an error when I just want to redraw everything as it was in the first stage? Thanks for any help!