1

Help! I'm drawing a sketch with randomized letters that fill the screen. The code issue is with my

for (int i = 0; i < I1; i++) {
  targets = tX + i * ts;
  targets2 = tY;
} else {
runTowards = false;

for (int i = 0; i < I1; i++) {
  targets = random(ts, width - ts);
  targets2 = random(ts, height - ts);`

** I need to update i within the for loop to denote the array letters' locations. The error returns a type mismatch, "float" does not match with int[]

How do I update the i within the for loop?/ (the targets= and targets2=) Here is the code**

    String message = "a,b,c,d,e,f";
int []positions = new int [0];
int[]positions2= new int[1];
int []targets = new int [0] ;
int [] targets2= new int[1];
int I1= message.length();


boolean runTowards = true;
float runSpeed = 100;
float restSpeed = 1;
float currentSpeed = restSpeed;
float ts = 64;

boolean thisHasReached;
float bk;
float elapsedTime;
boolean allHaveReached;
float pauseTime = 1000;
float distX;
float distY;
float changeX;
float changeY;
float tX;
float tY;
float reachedTargetAt = 0;
boolean hasReachedTarget = true;



void setup() {
  size (600,600);
  background(255);
   if ((I1 > targets.length) && (I1 > positions.length)){
    pickNewTarget();
}
}

void draw() {
   elapsedTime = millis() - reachedTargetAt;
  if (elapsedTime > pauseTime) {
    pickNewTarget();
  }
  drawChars();
  updatePositions();
  updateCurrentSpeed();
}

void drawChars() {
  for (int i = 0; i < I1; i++) {
    text(message.charAt(i),positions[i], positions[i]);
  }
}

void updatePositions() {
   allHaveReached = true;
   thisHasReached = false;

  for (int i = 0; i < I1; i++) {
     distX = abs(positions[i] - targets[i]);
     distY = abs(positions2[i] - targets2[i]);

     changeX = random(currentSpeed);
     changeY = random(currentSpeed);

    thisHasReached = changeX > distX && changeY > distY;

    if (positions[i] > targets[i]) {
      changeX = -changeX;
    }

    if (positions2[i] > targets2[i]) {
      changeY = -changeY;
    }

    positions[i] += changeX;
    positions2[i] += changeY;

    allHaveReached = allHaveReached && thisHasReached;
  }

  if ((!hasReachedTarget) && (allHaveReached)) {
    hasReachedTarget = true;
    reachedTargetAt = millis();
  }
}

void updateCurrentSpeed() {
  if (hasReachedTarget) {
    if (currentSpeed >= restSpeed) {
      currentSpeed -= (currentSpeed - restSpeed) * 9;
    } else {
      currentSpeed += 1;
    }
  } else {
    if (currentSpeed <= runSpeed) {
      currentSpeed += (runSpeed - currentSpeed) * 0.25;
    } else {
      currentSpeed -= 1;
    }
  }
}

void pickNewTarget() {
  if (!runTowards && random(1) > 0.75) {
    runTowards = true;

     tX = random(ts, width - 3 * ts);
     tY = random(ts, height - ts);

    for (int i = 0; i < I1; i++) {
      targets = tX + i * ts;
      targets2 = tY;
    }
  } else {
    runTowards = false;

    for (int i = 0; i < I1; i++) {
      targets = random(ts, width - ts);
      targets2 = random(ts, height - ts);
}
   
  }
  hasReachedTarget = false;
}

1 Answers1

1

Please look at this reference for random(): https://processing.org/reference/random_.html. Note that it states "If two parameters are specified, the function will return a float with a value between the two values." You got the error message because you tried to mix ints and floats. You can correct it by changing the array types from int to float, eg.

float[] targets = new float[80] ;
float[] targets2= new float[80];

You'll also need to make those arrays large enough to handle the size of your data or you'll wind up with another error that the array length has been overrun. Then change the loop to reflect that you are adding data to the arrays ([i]):

void pickNewTarget() {
  if (!runTowards && random(1) > 0.75) {
    runTowards = true;

    tX = random(ts, width - 3 * ts);
    tY = random(ts, height - ts);

    for (int i = 0; i < I1; i++) {
      targets[i] = tX + i * ts;
      targets2[i] = tY;
    }
  } else {
    runTowards = false;

    for (int i = 0; i < I1; i++) {
      targets[i] = random(ts, width - ts);
      targets2[i] = random(ts, height - ts);
    }
  }
  hasReachedTarget = false;
}

Also need to add a fill() for the text characters or you will see a blank screen.

apodidae
  • 1,988
  • 2
  • 5
  • 9