I’m making an application using Processing-2. This means using the draw() method to generate GUI. However something messes with the frameRate of the program making it feel way slower and very laggy/choppy. I know my frameRate works, since at the start and beginning of the program, the frameRate is fine, but once my arrows start spawning, it feels like it drops to a solid 5fps.
This is the code i have to spawn the images I have
public void addArrow() {
int direction = (int)(Math.random()*4);
if (direction == 0) {
Arrow newArrow = new Arrow(40, 600, 25);
leftArrows.add(newArrow);
} else if (direction == 1) {
Arrow newArrow = new Arrow(180, 600, 25);
downArrows.add(newArrow);
} else if (direction == 2) {
Arrow newArrow = new Arrow(320, 600, 25);
upArrows.add(newArrow);
} else if (direction == 3) {
Arrow newArrow = new Arrow(460, 600, 25);
rightArrows.add(newArrow);
}
}
public void drawAll() {
draw(leftArrows, 0);
draw(downArrows, 1);
draw(upArrows, 2);
draw(rightArrows, 3);
drawStationaryArrows();
}
public void draw(ArrayList<Arrow> arrows, int direction) {
for(int i = 0; i < arrows.size(); i++) {
Arrow arrow = arrows.get(i);
if (direction == 0) window.image(left, arrow.x, arrow.y, 100, 100);
if (direction == 1) window.image(down, arrow.x, arrow.y, 100, 100);
if (direction == 2) window.image(up, arrow.x, arrow.y, 100, 100);
if (direction == 3) window.image(right, arrow.x, arrow.y, 100, 100);
arrow.move();
arrow.checkIfDead();
if (!arrow.isAlive()) arrows.remove(arrow);
}
}
This block of code calls the above code from the runnable class:
public void draw() {
if (mp3.player.isComplete() == false) { ////
background(222);
text("Use arrow keys or highlighed buttons", 60, 300);
text("A", 90, 170);
text("S", 220, 170);
text("D", 350, 170);
text("F", 480, 170);
counter++;
System.out.println(counter);
if (counter == 8) {
counter = 0;
spawner.addArrow();
}
spawner.drawAll();
//display score
fill(fontColor);
text("Score: " + score, 450, 550);
}
if(mp3.player.isComplete() == true) { //fix code so that the end of the music is the end of the game
gameOver = true;
mp3.close();
background(222);
text("Game Over!\nYou scored " + score + " points.", 200, 200);
}
}
My setup method is also here, i set the frameRate a bit too fast but just for testing purposes
public void setup() {
size(600, 600);
spawner = new FloatingArrowSpawner(this, 40, 600);
frameRate(217);
score = 0;
counter = 0;
rand = 0;
gameOver = false;
scoreFont = createFont("IMPACT", 32);
textFont(scoreFont);
fontColor = color(240, 128, 128);
filename = "C:\\Users\\PC\\eclipse-workspace\\ics_final2018\\src\\audio.mp3";
mp3 = new MP3(filename);
mp3.play();
}
For some context, I am trying to make Dance Dance Revolution.
Any help is appreciated, I’ve been trying to solve this for hours.