Java 基于摆动 2D 像素的精灵 class 无法正常工作

Java swing 2D pixel based sprite class not working correctly

我为 java 和 swing 中的一个简单游戏制作了一个基于像素的精灵 class,我不想让一些精灵穿过其他精灵。所以我写了两个循环,它们应该将每个其他精灵的像素添加到关卡的 "material" 数组中。 Material应该过不了。随着水平它确实起作用。精灵无法通过其 material。但对于其他精灵则不然。它可以通过它们。这就是我真正想要修复的错误。精灵的像素阵列 似乎没有 附加。

非常感谢任何帮助!

代码:

int applied_pixels=lvl.material.length;
Sprite[] others=new Sprite[] {other sprites};
/*EDIT : others[i].frameborders[others[i].frame].all is the point array of the sprites' pixels
others[i].frame is the frame of the sprite object, because they contain an array of BufferedImages. Frame is the one that should be taken*/
Level lvl=the level; //Containing a couple of point arrays of pixels of some types, for example containing the material array of pixels
int apply_pixels=0; //How many pixels are needed ?
for (int i=0; i < others.length; i++) {
     if (others[i] != null) { //Isn't the sprite null
         apply_pixels=apply_pixels+others[i].frameborders[others[i].frame].all.length; //How many points does it has to add ?
     }
}
level=lvl.clone(); //Copy level to be able to later append points to the material array
level.material=new Point[apply_pixels];
System.arraycopy(lvl.material,0,level.material,0,lvl.material.length); //Copy old material array points
int appending_position=0;
appending_position=lvl.material.length; //Which destination position to append the points at ?
for (int i=0; i < others.length; i++) {
     if (others[i] != null) { //Isn't the sprite null
         System.arraycopy(others[i].frameborders[others[i].frame].all,0,level.material,appending_position,others[i].frameborders[others[i].frame].all.length); //Copy the points from the sprite to the material array
         appending_position=appending_position+others[i].frameborders[others[i].frame].all.length; //Position to append at is now plus the length of appended points
     }
}

我发现您的代码有两个可能的问题 posted。

首先是level.material=new Point[apply_pixels];只为新像素分配元素。它可能应该读作 level.material=new Point[lvl.material.length + apply_pixels];。或者,您可以将 apply_pixels 初始化为 int apply_pixel = lvl.material.length 而不是零。

第二个问题是你从来没有向我们展示lvl如何替换原来的level。据推测,您 posted 的代码是某处方法的一部分,而 level 是传入的输入,但程序的其他部分通过字段访问。除非修改后的 lvl 被正确返回并替换原来的,否则这里的代码将没有任何效果。然而,这只是猜测,因为 OP 拒绝 post 相关代码。