Yes, I think I am having trouble elsewhere.
Maybe lag is the wrong word for it. I am wanting to keep the relative distance between the number and the black line the same at all times.
If you click and drag the line back and forth very quickly, you can see one object lagging behind the other. It's a minor detail. But for such a simple piece of code, I was hoping that there would be a way to keep the two objects locked to each other in the spacial sence.
Here's the code again with the adjustments you suggested.
Code:int frame = 0;
int val = 0;
void setup(){
size(500,100);
background(204);
smooth();
strokeWeight(4);
line(0, 0, 0, 100);
PFont font;
// The font must be located in the sketch's
// "data" directory to load successfully
font = loadFont("ArialMT-48.vlw");
textFont(font);
fill(0, 102, 153);
text("0", 10, 65);
}
void draw()
{
if (mousePressed == true){
background(204);
frameNumText(mouseX, frame + 10, 65);
line(mouseX, 0, mouseX, 100);
frame = (mouseX);
}
if(keyPressed == true){
background(204);
line(frame + val, 0, frame + val, 100);
frame = frame + val; //update the frame var to the current position
frameNumText(frame, frame + 10, 65);
delay(100); // a slight delay to allow for human reflexes
}
}
void frameNumText(int num, int X, int Y)
{
// PFont font;
// The font must be located in the sketch's
// "data" directory to load successfully
// font = loadFont("ArialMT-48.vlw");
//textFont(font);
//fill(0, 102, 153); // a light grey colour
text(num, X, Y); //text position based on the input arguments
}
void keyPressed() {
if (key == CODED) {
if (keyCode == LEFT) { //if the left key is pressed
val = (-1); // take one of the current value
}
else if (keyCode == RIGHT) { //if the right key is pressed
val = (+1); //add one of the current value
}
else{
val = 0; //set the val to zero so there is no effect.
}
}
}