2

我已经在这个网站和谷歌上搜索了好几天,并且想知道为什么这不起作用。我正在制作像 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();

关于为什么我可以通过一个类而不是另一个类加载图像的任何帮助将不胜感激。

4

2 回答 2

0

好像您的 ImageIcon (tempii) 为空。您是否尝试过调试代码以检查对象的状态?

于 2013-04-05T15:53:48.600 回答
0

更换

public void AlphaAvatar(){

    img = new Image[NUMOFAVATARS];

}

经过

public AlphaAvatar(){

    img = new Image[NUMOFAVATARS];

}

public void AlphaAvatar()不是构造函数,它是一个AlphaAvatar不返回任何内容的调用方法......它解释了为什么 img 没有在其中初始化。

于 2013-04-05T15:54:51.997 回答