We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to add elements to an ArrayList (in Processing.js, basically a javascript array) in a for loop. BEcause of the many objects I need to add, the for loop is not incrementing all the way through. I basically have an ArrayList of levels (like in a game) and each level has it's own ArrayList of GameObjects. I'm first adding all the level objects to their own array, them I'm looping through that ArrayList to "make" each level, a.k.a. add a bunch of gameObjects to that level based on a String array. Here's my code:
void setupAllLevels() {
for(int i = 0; i < map.length(); i++) {
levels.add(new Level(i));
}
for(int i = 0; i < levels.size(); i++) {
levels.get(i).makeLevel();
}
}
And here's the makeLevel function within each level object:
void makeLevel() {
for (int y = 0; y < map[levelNum].length (); y++) {
for (int x = 0; x < map[levelNum][y].length (); x++) {
if (mapKey[map[levelNum][y][x]]) {
gameObjects.add(new (mapKey[map[levelNum][y][x]])(x * tileSize, y * tileSize, levelNum));
}
}
}
}
it works when I only try to make one level, but looping through all of them and making them results in either half of a level being made, or none of a level (it's kind of random how much it works). I don't care how long this function takes, because I'm doing it in setup(). Does anyone have any ideas of how to do this process so that every level gets made? Possibly by slowing down the for loop? Ive tried setTimeout(loop here, 0), but that hasn't worked. Any ideas?
Answers
Please, edit your question and change its category to JavaScript Mode section!
And most importantly, highlight your code and hit CTRL+K to format it for this forum! #-o
I had to edit your code myself. It's highlight and CTRL+K. Seems like you did CTRL+Q instead! :-@
Also, it's a good idea to use Processing IDE's CTRL+T for auto-indent! *-:)
Since JavaScript Mode is merely Java Mode transpiled into JS, you should also make sure your sketch works in both modes. There are many potential bugs too: :-SS
Thank you for your answer, and for highlighting my code (I'll do better next time). Map is the only thing I'm directly accessing from the level class, which is a 3d String array. the classof Level has its own gameObjects, so that is defined there as well. Although java modesign is important, my primary goal is just to get it to work as javascript. And I'll fix those things like length, map, etc. What I would really like to know in the meantime is how to maybe slow down the loop, or get it to complete all the way (these methods work fine when making only one level, or sometimes two). I would like to load in all the levels in the beginning of the game, so they're all ready to go. Do you know how I can ensure that this (process intensive) for loop increments all the way through?
"You shouldn't name your variables w/ same names as existing Processing functions, like map!"
Why? Variable names and method names are in different namespaces, so they don't conflict. Having a function named
map
, or a variable namedwidth
might be more confusing."Is map a String type?"
Maybe it is a rhetorical question, but given the usage with [], it is pretty obvious. The remark about
length
vs.length()
is pertinent, though.This is "JavaScript Mode" section. And I'm sure you know function references are stored in regular variables in JS!
Besides, even though Java allows variables & functions share labels, it's a source of confusion.
Like for example, Processing's mousePressed field & mousePressed() method:
How many times we had to meticulously explain such differences in this very forum so far!? :-@
And much probably, he re-assigned map globally, atop sketch. 8-X
Therefore, original map()'s reference got obliterated for the whole sketch! [..]
We already know what would happen next if he happens to invoke map()... X_X
ok I changed the map variable name to levelMap, but the for loop still isn't executing all the way through. I think the real problem is that it's trying to rush to the end of the for loop, without completing the whole thing. How do people do pre-loaders for games? I think this will get to the heart of the answer. Is there a way to use perusing a @pj's preload feature to run a function?
"This is "JavaScript Mode" section."
OK, one point for you, that's me not paying attention to context, for once... :-D
That said, if map() is a method in a class, the problem might not arise.
But problems of scope in JS are subtle, and indeed beginners should just avoid tempting the devil!
And indeed, we have to speculate on code we don't see, so this discussion is a rhetorical and pointless.
"new (mapKey[map[levelNum][y][x]])(x * tileSize, y * tileSize, levelNum)"
This is strange as well, and probably wouldn't work in Java mode.
Is mapKey an array of class names?
Again, we cannot debug code we don't see.
It means the class itself can't use Processing's original map()! That's true even in Java Mode! #-o
Unless by calling Processing.map() instead, since it's
static
. Not so sure that'll work in JS too! :-?That was my 1st advice: "You should also make sure your sketch works in both modes.".
Old Processing 1.5.1 would be much better, since it's closer to Processing.JS 1.4.1 from JS Mode! *-:)
If RAM allows it and it's not exaggeratedly slow, all resources are load & dealt w/ before draw() kicks in! $-)
For example, this online sketch calls terrainGenerator() from reset(). Which initializes a huge 2560x2560 byte[][]. Take a look: (*)