IntList / variable scope definition

edited April 2014 in JavaScript Mode

I have some problems with IntList (maybe variable scope?) in following code:

    PImage[] all_icons = new PImage[50];
    IntList story_cubes;

    void setup()
    {
      // load the image  from file
      for ( int i = 0; i< all_icons.length; i++ ) 
      {
        all_icons[i] = loadImage( i + ".png" );   // make sure images "0.jpg" to "11.jpg" exist
      }
      size(300, 300);
      background(255);
      dice_cup();
    }

    void draw()
    {
      // display the image
      imageMode(CENTER);
      for ( int i = 0; i< 5; i++ ) 
      {
        image(all_icons[story_cubes.get(i)], 50 + (i * 40), height/2);
      }
    }

    void mousePressed()
    {
      dice_cup();
    }

    void dice_cup() {
      story_cubes = new IntList();
      int x;
      x = random(5);
      story_cubes.add(x);
      return story_cubes;
    }

P.S.: Is there a way to println anything in JavaScript-mode?

Answers

  • edited April 2014

    For starters, your program can't even be compiled in Java Mode.
    And if you wanna use JavaScript Mode, you gotta forget anything from Processing 2's new features, like IntList!

  • edited April 2014

    What do I have to use? Unfortunately it isn't mentioned in the reference that you can't use it..

  • Traditional ArrayList<Integer>. :P

  • edited April 2014

    Like:

    PImage[] all_icons = new PImage[6];
    ArrayList<int> story_cubes;
    

    ? Won't compile.

  • edited April 2014

    A Java's Collection, such as an ArrayList, can't store primitive values such as an int! :-O
    We gotta use the corresponding wrapper class. For example, instead of int, we'd use Integer! 3:-O

  • Whoa, Processing is complicated. So I have to write: ArrayList<integer> story_cubes; and it should work?

  • edited April 2014

    Okay I consideraly moved on. I get an "Unexpected token: int" now. Everything works when I comment out the while loop of void dice_cup(). What's the problem? Again some type conflict? Tipps for simplyfication?:

    PImage[] all_icons = new PImage[6];
    ArrayList <Integer> story_cubes;
    
    void setup()
    {
      // load the image  from file
      for ( int i = 0; i< all_icons.length; i++ ) 
      {
        all_icons[i] = loadImage( i + ".png" );   // make sure images "0.jpg" to "11.jpg" exist
      }
      size(300, 300);
      background(255);
      dice_cup();
    }
    
    void draw()
    {
      background(255);
      imageMode(CENTER); // display the icons
      for ( int i = 0; i< story_cubes.size(); i++ ) 
      {
        image(all_icons[story_cubes.get(i)], 50 + (i * 50), height/2);
      }
    }
    
    void mouseReleased()
    {
      dice_cup();
    }
    
    void dice_cup() {
      story_cubes = new ArrayList <Integer>();
      int x;
      x = zufallszahl();
      story_cubes.add(x);
      while (story_cubes.size() < 5) {
        x = zufallszahl();
        if (story_cubes.contains(x)) {  
          story_cubes.add(x);
        }
      println(story_cubes);
    }
    
    int zufallszahl() {
      return int(random(5));
    }
    
  • edited April 2014

    You forgot to close dice_cup()'s code block. Just add one } after line 42 and the code will compile.

  • Thanks. Why does conversion int and the Java Integer type work in this instance?

  • edited April 2014

    This type of automatic casting is called autoboxing (and here a german link). Autoboxing automatically wraps an Integer object around your primitive int.

Sign In or Register to comment.