Array [i] declaration

edited December 2015 in Questions about Code

Hi there,

I don't have a lot of experience with Arrays, so I know I am definitely missing a core concept of array structure. Why does processing tell me it can't find anything named "i" even though I initialized [i] in setup and in draw?

/declare movingMolecule variable to hold the Molecule array instance

Molecule[] movingMolecule;
//declare myHero variable to hold the Hero instance
Hero myHero;
//declare a variable to hold the molecule array instance


//boolean to test molecule to hero collide is initialized to false
boolean isColliding =false;

void setup() {
  //set the canvas size to 500 by 500
  size (500, 500);


  //instanciate the Hero object
  myHero = new Hero (100, 100, 25);
  //instanciate the Molecule object with an array of 4 of the same molecule
  movingMolecule = new Molecule[4];

  //set up the new molecule objects
  for (int i =0; i< movingMolecule.length; i++) {
    movingMolecule[i]= new Molecule(200, 200);
  }
}

void draw() {
  background(100);
  //display Hero object
  myHero.displayHero(250, 250, 25);

  //display all elements of Molecule array
  for (int i =0; i< movingMolecule.length; i ++) {

    movingMolecule[i].displayMolecule();
  }
  //update position of molecule object 
  //if the objects don't collide.. 
  if (isColliding ==false)
  {
    for (int i =0; i< movingMolecule.length; i ++) {
      //..continue updtating the molecule 
      movingMolecule[i].updtateMolPosition();
    }
  }

  // check for collision with hero points
  isColliding = movingMolecule[i].collidesWith( myHero.xPosHero + 18, myHero.yPosHero - 45);
  //isColliding = movingMolecule.collidesWith( myHero.xPosHero + 20, myHero.yPosHero - 26);
  //(myHero.xPosHero + 18, myHero.yPosHero - 45, myHero.xPosHero +20,myHero.yPosHero + 26) ? fpr two points? 

  // if the colliding boolean is true between the triangle point(s) and the molecule.. 
  if (isColliding ==true)
  {
    //Top right triangle.// x and y of molecule will now be equal to the triangle point x and y  of myHero 
    movingMolecule.x = myHero.xPosHero + 18; 
    movingMolecule.y = myHero.yPosHero -45; 

    //Bottom right triangle // x of molecule will now be equal to the triangle point x and y  of myHero 
    movingMolecule.x = myHero.xPosHero + 20; 
    movingMolecule.y = myHero.xPosHero + 26; 


    //Bottom left triangle // x and y of molecule will now be equal to the triangle point x and y  of myHero 
    movingMolecule.x =myHero.xPosHero - 20; 
    movingMolecule.y = myHero.xPosHero + 23; 

    //Top left triangle // x and y of molecule will now be equal to the triangle point x and y  of myHero 
    movingMolecule.x =myHero.xPosHero - 18; 
    movingMolecule.y = myHero.xPosHero -45;
  }
}
// keyPressed function for HERO 
void keyPressed() {

  myHero.keyPressed();
}
//keyReleased function for HERO
void keyReleased() {
  myHero.keyReleased();
}
Tagged:

Answers

  • Answer ✓

    isColliding = movingMolecule[i].collidesWith( myHero.xPosHero + 18, myHero.yPosHero - 45);

    Local variable i doesn't exist at that point b/c it was declared inside the loops.

  • edited December 2015 Answer ✓

    you wrote

    Why does processing tell me it can't find anything named "i"

    at which line? 49 ?

    so take bracket } from line 45 and 46 after line 72

    also, I don't know why you want 2 for loops in 34 and 42 ?

  • So it probably make more sense to leave the for loop in draw (since I want to redraw the molecules) ? I used another example that had two loops, but I didn't realize I only need it once

    Stupid question, but how else would I declare [i] other than a with a global variable or a loop?

  • edited December 2015 Answer ✓

    [] is the array access operator. While i is an int variable representing an index value for it.
    Variables can be declared. Operators can't.

    https://Processing.org/reference/arrayaccess.html

  • _vk_vk
    Answer ✓

    initialized [i] in setup and in draw?

    Besides what have being said, perhaps you are not familiar with scope?

    Variables have block scope in java. Whenever a variable is declared inside a block it only "lives" inside that block. Variables declared outside any block are "global" and lives for ever (technically this is not really this, but thas is out of scope here:)

    int a  = 1; //global
    
    
    void setup() {
    
      println(a);  // 1
    
      // a block
      { 
        int a = 2; 
    
        println(a); //2
      }//end of block
    
      println(a); //1
    
      //another block 
      for (int i = 0; i<1; i++) {
    
        int a = 3;
    
        println(a);//3
    
        int c = 10; //scope of this for
      }
      println(a); //1
      //println(c); //ERROR no c available
      //println(i); //ERROR no i available
    }
    
  • In draw you want loop over all mols

    You just placed the brackets wrong

    Correct it, post again, ask again

  • Answer ✓

    i is declared correctly, only the brackets are terribly wrong

    See above

  • I have moved the bracket to line 73, and added "[i]" to be part of moving molecule

          if (movingMolecule[i].attatched==4) {
                //.. attach x and y of molecule to 4th Spike 
                movingMolecule[i].x = mySpike.xPosSpike - 20;
                movingMolecule[i].y = mySpike.yPosSpike +23;
    

    and I think I somewhat understand the logic of the loop, but I definitely need more practice. Thanks for all the help and replies!

  • you're welcome!

Sign In or Register to comment.