keyReleased help!
in
Programming Questions
•
1 year ago
hey i cant seem to get key released working in my code.
what i need to do is when i press 'P' the loading bar pauses and when i press 'P' again it continues.
Also when i press 'space' the loading bar restarts to the start. This works but when you press 'P' first, the space bar just continues the loading bar instead of restating.
One more thing if you can also help try organise this code in the best possible way that would be great.
Here is the code
// Code Requirements
float stTime;
float pause;
float maping;
float percent;
float number;
float time;
int prog;
boolean started = false;
// Setup and Draw
void setup () {
size(500, 300);
background(255);
fill(0);
textSize(24);
}
void draw () {
if (started) {
background(255);
Bar();
Boarder();
Text();
Percent();
}
}
// Components - Extra Features (Part 3)
//
// Restart
void keyReleased () {
if (key == ' ') {
started = true;
stTime = millis() - pause;
}
// Pause
else if (key == 'p') {
started = false;
pause = millis() - stTime;
}
}
// Starting The Progress Bar (Part 2)
void mouseClicked () {
percent=0;
time = random(2000, 8000);
stTime=millis();
started = true;
}
// Main Components And Function
//
// The Progress Bar
void Bar() {
maping = map(percent, 0, 100, 0, 250);
for (prog=0;prog<maping;prog++) {
stroke(255-prog);
line(width/4+prog, 9*height/20, width/4+prog, 11*height/20);
}
}
// Calculating The Percentage
void Percent() {
number = millis() - stTime;
if (int(percent)<=100) {
percent = (100*(number)/time);
}
if (percent>100) {
percent=100;
}
}
// Boarder Around The Progress Bar
void Boarder() {
noFill();
stroke(0);
rectMode(CENTER);
rect(width/2, height/2, width/2+10, height/10+10);
}
// Drawing The Text
void Text() {
//Percentage Text - Increasing by 5%
fill(0);
if (int(percent)<100) {
int percentForText = round(percent/5)*5;
text(percentForText + "%", width/2-10, height/2+10);
}
// Text When The Progress Bar Reaches 100%
else {
fill(255);
text("100% Done!", width/2-60, height/2 + 10);
}
}
Thanks Guys
Appreciate this
1