Add variable to an array index

edited November 2013 in Questions about Code

Hi all!

First of all, i'm new to java so please forgive me for easy questions and my bad English (I'm Dutch).

I have a code with an array: robotStartX[], the x position of a robot. I would now like to add the value of a variable to one of the indexes of robotStartX[].

Why doesn't the following work?

int robotStartX[] = new int[4];
int value = 12;

robotStartX[1] += value;
println(robotStartX[1]);

Could someone please tell me how to do this?

Thanks!

Joris van Lanen

Answers

  • It prints out 12. I don't get why you're claiming it's not working? (:|

  • also note: array indexes start at 0 so robotStartX[1] is actually the second after robotStartX[0] (but perhaps you know this)

  • edited November 2013

    Oke thanks, I made a new simple one to test it out:

    int robotStartX[] = new int[4];
    int value = 12;
    
    void setup() {
      size(200,200);
    
    }
    
    
    void draw() {
      frameRate(2);
    
    
         for (int i = 0; i < 4; i++) {
           background(0);
    robotStartX[i] += value;
    rect(robotStartX[1],20,20,20);
    
         }
    }
    

    It indeed works fine, but for some reason it doesn't work with my other code. I'll just paste it here. There might me some Dutch words, sorry for that!

    int value;
    
    void setup() {
      size(400, 400);
      background(0);
    }
    
    void draw() {
      frameRate(6);
      background(0);
      tekenRobots();
    }
    
    void tekenRobots() {
      int robots = 4;
      int robotStartY = 80;
      int[] robotStartX = new int[robots]; 
    
      for (int i = 0; i < robots; i++) {
    
    
        dobbel(); 
        value = dobbel();
        robotStartX[i] += value; 
    
    
        println("value: " + value);
        //println("i= " +i);
        println("X: " + robotStartX[i]);
    
        fill(0, 0, 255);  
        stroke(0, 0, 255);
        triangle(robotStartX[i], robotStartY, robotStartX[i]+30, robotStartY, robotStartX[i]+(30/2), robotStartY-30); 
        fill(0);
        text(i+1, robotStartX[i]+30/3, robotStartY); 
        robotStartY += 80; 
      }
    }
    
    int dobbel() {
      int dices = 3;
      if (dices == 1) { 
        int dobbel1 = int(random(1, 7)); 
        int value = dobbel1;
        return value; 
      }
      else if (dices == 2) { 
        int dobbel1 = int(random(1, 7));
        int dobbel2 = int(random(1, 7)); 
        int value = dobbel1+dobbel2;
        return value;
      }
      else { 
        int dobbel1 = int(random(1, 7));
        int dobbel2 = int(random(1, 7));
        int dobbel3 = int(random(1, 7));
        int value = dobbel1+dobbel2+dobbel3;
        return value;
      }
    }
    

    The robots should be going further to the right because the X is being raised, but it doesn't......

    Thanks

  • Answer ✓

    tekenRobots() re-create the array of robots on each frame, the previous one is lost.

    I think you want to define robotStartX as a global variable.

    See also the What are setup() and draw()? article.

  • Thanks very much!!! It works now! I indeed only had to move the create array statement to a place where it's not being done over and over again. Thanks again, problem SOLVED! :)

Sign In or Register to comment.