我坐在这个位置上已经有一段时间了,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");
return语句有效,但我只会从文件夹中返回一张图片。