我一直在搜索这个网站和谷歌几天,并撕扯我的头发想知道为什么这不起作用。我正在制作像FFII这样的2D RPG风格游戏,并且能够根据方向使用不同的图像使角色在屏幕上走动,并且代码工作正常,直到我将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();
关于为什么我可以通过一个类而不是另一个类加载图像的任何帮助都将非常感激。
答案 0 :(得分:0)
好像你的ImageIcon(tempii)是null。您是否尝试通过调试来逐步检查代码的状态?
答案 1 :(得分:0)
替换
public void AlphaAvatar(){
img = new Image[NUMOFAVATARS];
}
通过
public AlphaAvatar(){
img = new Image[NUMOFAVATARS];
}
public void AlphaAvatar()
不是构造函数,它是一个名为AlphaAvatar
的方法,它不返回任何内容......它解释了为什么img未在其中初始化。