error while compiling (cannot convert from float to int)

Person player;
int dist=100;
int[] speeds = {
  5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100
};
float objectTypes=random(1, 5);
int gravity=20;
int[] rectX = {
  500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500
};
int speed=5;
int currentSpeed = speeds[min(frameRate, speeds.length)];

void setup() {
  size(500, 500);
  player = new Person(color(0, 0, 0), 100, 400, speed[0]);
}

void draw() {
  frameRate(60);
  background(105);
  player.run();
  player.display();
}

class Person {
  color c;
  float xpos;
  float ypos;
  float xspeed;

  Person(color tempC, float tempXpos, float tempYpos, float tempXspeed) {
    c=tempC;
    xpos=tempXpos;
    ypos=tempYpos;
    xspeed=tempXspeed;
  }

  void display() {
    noStroke();
    fill(c);
    rectMode(CENTER);
    rect(100, 400, 10, 10);
    if (objectTypes==1) {
      rect(currentSpeed, 400, 20, 20);
    }
    if (objectTypes==2) {
    }
    if (objectTypes==3) {
    }
    if (objectTypes==4) {
    }
    if (objectTypes==5) {
    }
  }

  void run() {
    if ( speed < 100) {
      speed+=5;
    }
  }
}

it says cannot convert from float to int as the error.

Tagged:

Answers

  • edited June 2014 Answer ✓

    Use (cast) convert operator (int) in order to downgrade min()'s float result to an int:
    int currentSpeed = speeds[(int) min(frameRate, speeds.length)];

    Or use (int) on float variable frameRate. This way, min() ends up returning an int instead:
    int currentSpeed = speeds[min((int) frameRate, speeds.length)];

    http://processing.org/reference/min_.html

Sign In or Register to comment.