I have 55 images named alien1 - alien55 (I have a reason not to use a PImage array), instead of typing the draw() command for each one, I used a for loop:
void draw() {
for (int i; i=0; i<54 i=i+1){
if (aliendraw[i]==true){
"alien"+i+".draw()";
}
}
obviously, this won’t work outside of a string, which brings us to my question, is there a way to make this work? in other coding languages there’s eval() but it’s not in processing.
help will be appreciated, thanks in advance.
I assume you are working in ‘Java’ mode and it is true that Java does not have the equivalent of the eval() so you will not get Java to execute a String variable.
The basic syntax errors in the for statement you posted indicate that you are very new to programming so I would need to see more code especially the part where you load the images.
I would be interested to know why you don’t want to use an array to store the images when you use an array to decide on their visibility.
use an index value (in your example 1 - 55) to access an image.
avoid using a PImage array (for some unknown reason)
As pointed out you don’t want to have 55 variables e.g. PImage image1, image2, image3,... image55;
If you have done this then think again as it would be an absolute nightmare to work with.
What is needed is a named container to enable access to the images individually using their ID number (1 - 55).
Given the minimal information you have provided I can think of two possibilities the humble array or a HashMap. For the problem you have described I cannot think of a realistic way of solving the problem without using a container.
I suspect that you are using these images as sprites in which case I would encapsulate the image inside a user defined sprite class. If you do this then the sprite is aware of its own visibility and could use a HashMap which will give you freedom to use different keys.
I think you misunderstood me, I already have 55 variables, what I need help with is identifying them within the for loop, because they don’t have brackets (these things ] [ ) I can’t use i to identify them normally, then I remembered how you can “merge” variables and words inside strings, what I want to do is merge “alien” and i so that the for loop will execute the command “alien0.draw()” than “alien1.draw()” all the way to 54.
Sorry I missed this post but it explains what you are trying to do much better than the original one.
So the short answer is no, there is no simple Java language feature that would take a String literal such as “alien42” and use that to manipulate a variable called alien42
It maybe possible to use Java Reflection to do something like this but that is an advanced language feature well beyond arrays and other Java collections which would be the preferred way of handling data sets.
I think you need to go back to the teacher for guidance on what is expected of you.
The course I’m taking provides custom classes that are easier to use, one of those is a custom class for images, we learned arrays for all the classes that weren’t replaced but since images are replaced we weren’t taught PImage arrays, I just googled “an array of images processing” and implemented a PImage array, but as I said in a previous post it was extremely laggy and my teacher didn’t want me to use that.