We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
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!
What do I have to use? Unfortunately it isn't mentioned in the reference that you can't use it..
Traditional ArrayList<Integer>. :P
Like:
? Won't compile.
A Java's Collection, such as an ArrayList, can't store primitive values such as an
int
! :-OWe gotta use the corresponding wrapper class. For example, instead of
int
, we'd use Integer! 3:-OWhoa, Processing is complicated. So I have to write:
ArrayList<integer> story_cubes;
and it should work?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?:
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?
This type of automatic casting is called autoboxing (and here a german link). Autoboxing automatically wraps an Integer object around your primitive int.