Я сижу на этом в течение довольно долгого времени, кто-то из Reddit помог мне, и я хотел бы отдать ему должное.
Я хочу вернуть несколько изображений из папки ресурсов и вернуть их, чтобы я мог загрузить содержащиеся там изображения в игру для Android.
private Target chooseTarget(int x, int y, int targetNumber) throws IOException {
int targetScores[] = {1, 2, 4, 8}; // TODO
double targetProps[] = {0.6, 0.8, 0.5}; // TODO Wahrscheinlichkeiten
int targetIndex;
// zufällige Auswahl des Targets nach Wahrscheinlichkeiten in targetProps
if (targetNumber == 0) { // Incase targetNumber == 0
double dice = random.nextDouble(); // give dice a random number
targetIndex = targetProps.length; // targetIndex has length 3 now !
while (targetIndex > 0 && dice < targetProps[targetIndex - 1]) // while targetIndex > 0 && dice kleiner als 0,8
targetIndex--; // Postfix-- targetIndex
targetNumber = targetIndex + 1; // targetNumber =
} else // explizite Wahl der Nummer des Targets
{
if (targetNumber < 1 || targetNumber > targetScores.length) // explizit ausgewähltes Target
targetNumber = 1;
targetIndex = targetNumber - 1;
}
String [] imagePaths = assetManager.list("levels/default/cans"); /// create a list of files as a string array from the directory
List<Image> images = new ArrayList<>(imagePaths.length); /// Arraylist mit der lägne von imagePaths
for (String imagePath : imagePaths) { /// advanced loop
String path = "levels/default/cans/" + imagePath; /// + Array Elemente [i] so gesehen
byte[] content; /// byte array mit den Namen content erstellen
try(InputStream input = assetManager.open(path)){ /// versuche input = öffne path d.h jedes mal öffne path mit Array Element [i]
content = toByteArray(input);
}
images.add(new Image(path, content));
}
return new Target(x, y, (InputStream) images, targetScores[targetIndex]);// TODO
}
Это то, что я сделал до сих пор, экран просто становится белым и, например, для
InputStream is = assetManager.open("levels/default/cans/can1.png");
Оператор возврата работает, но я будуверните только одно изображение из папки.