Question on why short animation seems to be affected by simple block of code?
in
Programming Questions
•
2 years ago
I am working on a small animation. After having inserted this block in it, I noticed that the first second or two when I run it, I get a blank screen as if something is destabilizing the execution. I was curious to know why this particular block would do that? This is the problem block and I have included the whole code in another section below that.
- // loop to draw the twelve lines on the sand that mark the hours
- int lineNumber =0;
- while (lineNumber<13) {
- int spaceBetween =(lineNumber-1)*10;
- int hourCounter=lineNumber+1;
- float hourLineY = (seaLevelLow+2)+spaceBetween;
- float inverseHourLineY = height-spaceBetween-3;
- stroke(232, 221, 7);
- strokeWeight(1);
- line (40, hourLineY, width-40, hourLineY);
- fill(209,199,6);
- text(hourCounter, 20, inverseHourLineY);
- text(hourCounter+12, width-30, hourLineY-5);
- lineNumber++; }
- // declare global variables for internal clock time functions
- int hr; // will return the hour according to internal clock
- int mnt; // will return the minutes according to internal clock
- int sec; //returns the seconds according to internal clock
- int dy; //returns the day of the month according to internal clock
- int mth; //returns the month according to internal clock
- int yr; //returns the four digit year according to internal clock
- float milliseconds; //returns the number of milliseconds since start of app according to internal clock
- // declare global variables for sky lines and sun and moon animation
- float centerX;
- float centerY;
- float horizonLineY;
- float arcSpaceX;
- float arcSpaceY;
- float arcSpaceAngleX;
- float arcSpaceAngleY;
- float x;
- float y;
- int sunSize=90;
- float angle;
- float sizeArcX;
- float sizeArcY;
- float sunSpeed;
- float seaLevelLow;
- float seaLevelY;
- float hourLineY;
- // declare global variables for Spring fish animation
- float FishYLocation;
- float maleFishXLocation=1500;
- float femaleFishXLocation=1200;
- float fishSpeed=2;
- float maleFishAcceleration=2;
- float fishReproductionSpeed=3;
- float babyX;
- float babyY;
- int time1 = 8000;
- // prepare the size and framerate of the application
- void setup() {
- size(1400, 800);
- frameRate (30);
- // initialize content of the global variables for internal clock time
- int hr= hour(); // hour will range from 0 to 23
- int mnt=minute(); // minutes will range from 0 to 59
- int sec=second(); // seconds will range from 0 to 59
- int dy=day(); // days will range from 1 to 31
- int mth=month(); // mth will range from 1 to 12
- int yr=year(); // yr will range from this year on
- float milliseconds =millis(); // milliseconds will start when app starts
- // initialize content of the global variables for rotation of sun
- // and static purple lines that function as a 5 sec. time reference
- centerX=width/2;
- centerY=height/2;
- horizonLineY=height-height*3/5;
- seaLevelLow=height-120;
- angle=0.0;
- sizeArcX=width/2;
- sizeArcY=height/2.1;
- }
- void draw() {
- background(120, 70, 230);
- // initialize content of the local variables
- sunSpeed=second();
- angle=180+3*sunSpeed;
- // loop to draw purple lines in the sky spaced apart in a regular fashion
- // where the space between each line represents a span of 5 seconds.
- float arcSpaceIteration=0;
- while (arcSpaceIteration<181) {
- arcSpaceAngleX=180+arcSpaceIteration;
- arcSpaceAngleY=180+arcSpaceIteration;
- arcSpaceX=centerX+sizeArcX*cos(radians(arcSpaceAngleX));
- arcSpaceY=centerY+sizeArcY*sin(radians(arcSpaceAngleY));
- stroke(135, 0, 230);
- strokeCap(ROUND);
- strokeWeight(4);
- line (centerX, centerY, arcSpaceX, arcSpaceY);
- arcSpaceIteration+=15;
- }
- // Draw a sand color surface over the purple lines which
- // will be revealed every hour as the tide receeds
- fill(242, 238, 149);
- noStroke();
- rectMode(CORNERS);
- rect (0, horizonLineY, width, height);
- // loop to draw the twelve lines on the sand that mark the hours
- int lineNumber =0;
- while (lineNumber<13) {
- int spaceBetween =(lineNumber-1)*10;
- int hourCounter=lineNumber+1;
- float hourLineY = (seaLevelLow+2)+spaceBetween;
- float inverseHourLineY = height-spaceBetween-3;
- stroke(232, 221, 7);
- strokeWeight(1);
- line (40, hourLineY, width-40, hourLineY);
- fill(209,199,6);
- text(hourCounter, 20, inverseHourLineY);
- text(hourCounter+12, width-30, hourLineY-5);
- lineNumber++;
- }
- // Alternate the color of the ellipse so that it is either the sun or the moon
- float sunMoon=minute()%2;
- if (sunMoon%2 ==0) {
- fill(255, 225, 5);
- }
- else {
- fill (225, 225, 255);
- }
- // draw and move sun incrementally every second over an arc of 180 degrees in the sky
- noStroke();
- float x=centerX+sizeArcX*cos(radians(angle));
- float y=centerY+sizeArcY*sin(radians(angle));
- ellipse(x, y, sunSize, sunSize);
- // create water-based horizon line in a layer in front of the animation of the sun
- // and make this water recede every hour for first 12 hours and then rise back up
- // for second 12 hours of a day
- int hr= hour(); // hour will range from 0 to 23
- if (hr<13) {
- fill (149, 230, 240, 160);
- noStroke();
- seaLevelY=height-hr*10;
- rectMode(CORNERS);
- rect (0, horizonLineY, width, seaLevelY);
- }
- else {
- fill (149, 230, 240, 160);
- seaLevelY=seaLevelLow+(hr-12)*10;
- rect (0, horizonLineY, width, seaLevelY);
- }
- /* Spawning like fish marks the passage of calendrical time with allegories drawn
- from nature: the fish animation represents the spring season */
- // Initialize local variables for fish animation
- int fishMotion =millis ();
- float FishYLocation=height/2;
- stroke(1);
- // Condition for seduction preceding spawning
- if (femaleFishXLocation<maleFishXLocation) {
- //Female fish appearing followed by a male fish
- if (fishMotion < time1) {
- maleFishXLocation-=fishSpeed;
- femaleFishXLocation-=fishSpeed;
- }
- // Male fish showing interest in female fish
- else {
- maleFishAcceleration*=1.007;
- maleFishXLocation-=maleFishAcceleration;
- femaleFishXLocation-=fishSpeed;
- }
- fill(255, 40, 10);
- drawFish (femaleFishXLocation, FishYLocation);
- fill(245, 245, 0);
- drawFish (maleFishXLocation, FishYLocation);
- }
- // Fish spawning babies as they swim off the screen together = Happy ending!
- else {
- femaleFishXLocation-=fishSpeed;
- fill(255, 40, 10);
- drawFish (femaleFishXLocation, FishYLocation);
- fill(245, 245, 0);
- drawFish (femaleFishXLocation-1, FishYLocation);
- // Baby fish following behind
- fill(255, 180, 0);
- scale(0.5);
- translate (300, 100);
- drawFish (2*femaleFishXLocation, 2*FishYLocation);
- for (int f = 0; f < 24; f++)
- // draw 25 other baby fish that follow
- for (int babyX = 0; babyX <7; babyX++) {
- drawFish (2 * femaleFishXLocation + 150 * babyX, 2 * FishYLocation);
- drawFish (2 * femaleFishXLocation + 200 * babyX, 2 * FishYLocation+50*babyX);
- drawFish (2 * femaleFishXLocation + 275 * babyX, 2 * FishYLocation-40*babyX);
- drawFish (2 * femaleFishXLocation + 325 * babyX, 2 * FishYLocation-40*babyX);
- }
- }
- }
- // CREATE THE SPRING FISH FUNCTION
- void drawFish (float fishX, float fishY) {
- strokeWeight (3);
- beginShape();
- vertex(fishX, fishY+50);
- vertex(fishX+45, fishY+20);
- vertex(fishX+100, fishY+40);
- vertex(fishX+150, fishY);
- vertex(fishX+120, fishY+90);
- vertex(fishX+95, fishY+70);
- vertex(fishX+60, fishY+80);
- endShape(CLOSE);
- arc (fishX+34, fishY+42, 10, 15, 0, PI);
- }
- // CREATE THE STARFISH FUNCTION
- void starFish (float starFishX, float starFishY) {
- fill (216, 20, 125);
- strokeJoin(ROUND);
- strokeWeight (4);
- beginShape();
- vertex(starFishX, starFishY+24);
- vertex(starFishX+20, starFishY+32);
- vertex(starFishX+27, starFishY);
- vertex(starFishX+34, starFishY+32);
- vertex(starFishX+54, starFishY+24);
- vertex(starFishX+42, starFishY+54);
- vertex(starFishX+50, starFishY+96);
- vertex(starFishX+27, starFishY+72);
- vertex(starFishX+4, starFishY+96);
- vertex(starFishX+12, starFishY+54);
- endShape(CLOSE);
- arc (starFishX+26, starFishY+58, 10, 15, 0, PI);
- strokeWeight (3);
- ellipse (starFishX+18, starFishY+44, 7, 7);
- arc (starFishX+18, starFishY+41, 10, 10, PI, TWO_PI);
- ellipse (starFishX+32, starFishY+44, 7, 7);
- arc (starFishX+32, starFishY+41, 10, 10, PI, TWO_PI);
- }
1