我已经在这个网站和Google上搜索了好几天了,并且想知道为什么这不起作用,所以把我的头发拔掉了。我正在制作2D rpg风格的游戏,例如FFII,并且能够根据方向使用不同的图像使角色在屏幕上走动,并且代码运行良好,直到将Image[]
放到新的类中以使其更适用于以后编码。这是完美运行的代码部分:
package alpharpg;
import java.awt.Image;
import javax.swing.ImageIcon;
public class AlphaCharacterSheet extends AlphaConstants{
Image[] avatar;
public AlphaCharacterSheet() {
avatar = new Image[NUMOFAVATARS];
}
public void GetCharacter (int c){
switch (c){
case BARD:
ImageIcon tempii;
tempii = new ImageIcon(this.getClass().getResource(FRANTIKMENU));
avatar[MENUAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKBATTLE));
avatar[BATTLEAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKFRONT));
avatar[FRONTAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKBACK));
avatar[BACKAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKLEFT));
avatar[LEFTAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKWALKFRONT));
avatar[FRONTWALKAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKWALKBACK));
avatar[BACKWALKAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKWALKLEFT));
avatar[LEFTWALKAVATAR] = tempii.getImage();
default:
break;
}
}
对
GetCharacter(BARD)
的调用有效,并且在paint()
中用drawImage(party.players[inputcounter].avatar[currentzone.avatarfacing], currentzone.avatarx, currentzone.avatary, null);
但是,即使当我将其放入时,它也能完美地工作
public Image[] img;
public void AlphaAvatar(){
img = new Image[NUMOFAVATARS];
}
public void SetAvatar (int avatarid){
switch (avatarid){
case BARD:
ImageIcon tempii;
tempii = new ImageIcon(this.getClass().getResource(FRANTIKBATTLE));
img[BATTLEAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKFRONT));
img[FRONTAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKBACK));
img[BACKAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKLEFT));
img[LEFTAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKWALKFRONT));
img[FRONTWALKAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKWALKBACK));
img[BACKWALKAVATAR] = tempii.getImage();
tempii = new ImageIcon(this.getClass().getResource(FRANTIKWALKLEFT));
img[LEFTWALKAVATAR] = tempii.getImage();
现在在CharacterSheet中,我有AlphaAvatar头像;
在
CharacterSheet()
中,我有avatar = new AlphaAvatar();
然后,当调用
GetCharacter(BARD)
时,我对avatar.SetAvatar(BARD)
进行了调用,它在第一个img[] = tempii.getImage();
处给出了错误对于为什么我可以通过一个类而不是另一个类加载图像的任何帮助,将不胜感激。
最佳答案
更换
public void AlphaAvatar(){
img = new Image[NUMOFAVATARS];
}
通过
public AlphaAvatar(){
img = new Image[NUMOFAVATARS];
}
public void AlphaAvatar()
不是构造函数,它是一个名为AlphaAvatar
的方法,该方法不返回任何内容...它解释了为什么未在其中初始化img。