我必须为我的学校项目进行处理。因此,我将图片加载到一个数组中,然后在一个新类中添加另一个数组,每次创建该类时,我都会将图片放入该数组中。每次创建该类时,我都会将图像放置在不同的位置,并且效果很好,但一旦尝试调整这些图像的比例,它也会同时调整其他类中图像的比例。我认为它会更改所有这些图像的比例,因为它会更改我的第一个数组中图像的比例,并且我不会创建新的图像。有办法解决这个问题吗?这是我的代码。这是我加载图像的地方:
class ItemLoader
{
PImage[] itemArray;
ArrayList itemCollection;
String itemType;
int amountOfFrames;
int amountOfItems = 1;
ItemLoader()
{
itemCollection = new ArrayList();
LoadImage();
}
void LoadImage()
{
for(int ii = 0; ii < amountOfItems; ii++)
{
AssignItemType(ii);
itemArray = new PImage[amountOfFrames];
for(int i = 0; i < amountOfFrames; i ++)
{
String filenaam = itemType + nf(i, 5) + ".png";
itemArray[i] = loadImage(filenaam);
}
itemCollection.add(itemArray);
}
}
void AssignItemType(int itemNumber)
{
switch(itemNumber)
{
case 0: itemType = "Leaves"; amountOfFrames = 21;
break;
case 1: itemType = "Apple";
break;
case 2: itemType = "Bannana";
break;
case 3: itemType = "Pear";
break;
case 4: itemType = "Cherry";
break;
case 5: itemType = "Owl";
break;
case 6: itemType = "Bird";
break;
}
}
}
然后这是当我创建该类的实例时:
itemList.add(new ItemSpawn(randomX,randomY,0, strokeThickness,itemLoader.itemCollection));
这是我遍历数组以对图像进行动画处理的地方:
class ItemSpawn
{
PImage[] animationFrames;
PImage lastFrame;
float frameCounter;
int x_loc;
int y_loc;
float ImageScale = 0;
int ImageRotation = 0;
ItemSpawn(int x_loc_par, int y_loc_par, int _itemType, float _strokeSize, ArrayList _tempArray)
{
animationFrames = (PImage[]) _tempArray.get(_itemType);
x_loc = x_loc_par;
y_loc = y_loc_par;
ApplyScaleRotation();
}
void ApplyScaleRotation()
{
ImageScale = 0.5;
ImageRotation = int(random(0,360));
for(int i = 0; i < animationFrames.length; i++)
{
animationFrames[i].resize(int(animationFrames[i].width * ImageScale), int(animationFrames[i].height * ImageScale));
}
}
void LoopAnimation()
{
int convertCount = int(round(frameCounter));
if(convertCount < animationFrames.length - 1)
{
image(animationFrames[convertCount],x_loc - (animationFrames[convertCount].width/2) ,y_loc - (animationFrames[convertCount].height/2));
frameCounter += 0.4;
}else
{
image(animationFrames[animationFrames.length - 1],x_loc - (animationFrames[convertCount].width/2),y_loc - (animationFrames[convertCount].height/2));
}
}
}
在您的代码中,ItemSpawn类的每个对象都使用相同的 PImage[]
数组。当您在一个ItemSpawn
实例中更改图像时-所有其他实例均会看到此更改。
每个ItemSpawn
对象都应具有图像的专用副本-然后它可以安全地修改图像。
考虑这样的简化类:
class X {
int[] array;
X(int[] array) {
this.array = array;
}
void modify() {
array[0] = 99;
}
}
我们可以使用相同的数组创建2个此类的不同对象(x1和x2):
int[] values = {1,2,3,4};
X x1 = new X(values);
X x2 = new X(values);
接下来x1
修改数组:
x1.modify();
println(x1.array[0]); // output: 99
println(x2.array[0]); // output: 99
解决方案是为类的PImage[]
每个对象创建数组的深层副本ItemSpawn
:
ItemSpawn(int x_loc_par, int y_loc_par, int _itemType, float _strokeSize, ArrayList _tempArray)
{
// old:
// animationFrames = (PImage[]) _tempArray.get(_itemType);
// new:
PImage[] images = _tempArray.get(_itemType);
animationFrames = new PImage[images.length]; // create new array with the same length
// copy all images
for (int i = 0; i < images.length; i++) {
PImage img = images[i];
animationFrames[i] = img.get(); // returns copy of this image
}
...
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句