You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Processing数组变量修改问题:类型不匹配错误修复

解决Processing中数组类型不匹配与赋值错误问题

问题核心

代码报错“float与int[]类型不匹配”,根源在pickNewTarget()函数的for循环中:直接将float类型的坐标值赋值给int[]类型的targets、targets2数组,而非通过索引更新数组元素。此外初始数组长度与字符数量不匹配,会引发后续越界错误。

修复步骤

  • 修正数组初始化:将positions、positions2、targets、targets2数组初始化为与字符数量(message.length())一致的长度,避免循环访问时越界。
  • 修改数组赋值逻辑:在pickNewTarget()的for循环中,通过索引i定位数组元素,将float坐标值转为int类型后赋值给对应位置。
  • 修正字符绘制坐标:drawChars()中使用positions[i](x轴)和positions2[i](y轴)作为字符的绘制位置,而非重复使用positions[i]。

完整修正代码

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

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

boolean thisHasReached;
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], positions2[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] += (int)changeX;
    positions2[i] += (int)changeY;

    allHaveReached = allHaveReached && thisHasReached;
  }

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

void updateCurrentSpeed() {
  if (hasReachedTarget) {
    if (currentSpeed >= restSpeed) {
      currentSpeed -= (currentSpeed - restSpeed) * 0.09; // 原系数9会导致速度骤降,调整为0.09更合理
    } 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[i] = (int)(tX + i * ts);
      targets2[i] = (int)tY;
    }
  } else {
    runTowards = false;

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

注:额外调整了updateCurrentSpeed()中的速度衰减系数(原9改为0.09),避免速度瞬间降到0导致字符停止移动。

内容的提问来源于stack exchange,提问作者Grace Whitman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 03:11:51