1

我正在用java组合一个简单的RPG游戏引擎。此时一切正常,而我的所有课程都在一个目录中。基本上,我知道我最终会得到一堆文件,并希望将它们组织成一个包结构。我按照http://www.jarticles.com/package/package_eng.html上的说明进行操作,但似乎无法让魔法发生。发布的两个类是最不依赖的,我想如果我能让这些工作,那么剩下的就不应该是戏剧了。作为记录,我在 Leeenux 中使用 openJDK(Ubuntu netbook Remix 的混音)

头等舱

package adventure.engine;

import java.util.*;

public class Inventory
{
ArrayList itemList = new ArrayList();

public Inventory()
{

}

public void addItem()
{

}

public void removeItem()
{

}
}

第二个:

package adventure.engine;



import adventure.engine.*;



public class PlayerCharacter

{

private String name = "Player";

private String race;

private String plrClass;

private int level;

private int xp;

private int levelXp;

private Inventory inventory = new Inventory();



//---------

//Abilities

//---------



private static final String[] abilitiesList = {"Strength",

                    "Dexterity",

                    "Constitution",

                    "Intelligence",

                    "Wisdom",

                    "Charisma"};



private int[] abilitiesValues = new int[abilitiesList.length];



//------

//Skills

//------

private static final String[] skillsList    = {"Acrobatics"     , "Insight",

                    "Arcana"            , "Intimidate",

                    "Athletics"         , "Nature",

                    "Bluff"         , "Perception",

                    "Diplomacy"     , "Religion",

                    "Dungeoneering"     , "Stealth",

                    "Endurance"     , "Streetwise",

                    "Heal"          , "Thievery",

                    "History"};



private int[] skillsValues = new int[skillsList.length];



//***********

//Constructor

//***********

public PlayerCharacter()

{

    level = 1;

    xp = 0;

    levelXp = 1000;



    setAbility("Strength", 8);

    setAbility("Dexterity", 10);

    setAbility("Constitution", 10);

    setAbility("Intelligence", 10);

    setAbility("Wisdom", 10);

    setAbility("Charisma", 10);

}       //public PlayerSheet()





//*************

//Class Methods

//*************

public void addXp(int val)

{

    xp += val;



    if (xp >= levelXp)

    {

        level++;

        xp -= levelXp;

        //levelXp += ;

    }

}       //public void addXp(int val)





public void updateSkills()

{



}





//Mutators

public void setName(String n)

{

    name = n;

}





public void setLevel(int l)

{

    level = l;

}



public void setRace(String r)

{

    race = r;

}



public void setXP(int x)

{

    xp = x;

}





public void setClass(String c)

{

    plrClass = c;

}



//set ability value by name

public void setAbility(String a, int val)

{

    for(int i = 0; i < abilitiesList.length; i++)

    {

        if(abilitiesList[i].compareTo(a) == 0)

        {

            abilitiesValues[i] = val;

        }

    }

}



//set ability by index

public void setAbility(int index, int val)

{

    abilitiesValues[index] = val;

}



//set skill by name

public void setSkill(String name, int val)

{

    for(int i = 0; i < skillsList.length; i++)

    {

        if(skillsList[i].compareTo(name) == 0)

        {

            skillsValues[i] = val;

        }

    }

}



//set skill by index

public void setSkill(int index, int val)

{

    skillsValues[index] = val;

}



//Accessors

public static String[] getAbilityList()

{

    return abilitiesList;

}



public static String[] getSkillsList()

{

    return skillsList;

}



//retrieve an ability value by name

public int getAbility(String a)

{

    int val = 0;



    for(int i = 0; i < abilitiesList.length; i++)

    {

        if(abilitiesList[i].compareTo(a) == 0)

        {

            val = abilitiesValues[i];

            break;

        }

    }



    return val;

}



//retrieve an ability value by index number

public int getAbility(int i)

{

    return abilitiesValues[i];

}



public int getSkill(String s)

{

    int val = 0;



    for(int i = 0; i < skillsList.length; i++)

    {

        if(skillsList[i].compareTo(s) == 0)

        {

            val = skillsValues[i];

            break;

        }

    }



    return val;

}



public int getSkill(int i)

{

    return skillsValues[i];

}



public String getName()

{

    return name;

}



public String getRace()

{

    return race;

}



public String getPlrClass()

{

    return plrClass;

}



public int getLevel()

{

    return level;

}



public int getXP()

{

    return xp;

}



public int getLevelXP()

{

    return levelXp;

}



}       //public class PlayerCharacter

类驻留在 /home/user/Java/adventure/engine

echo $classpath 的输出是 /home/james/Java:/.:

当我尝试编译第二个类时,出现以下错误:

PlayerCharacter.java:18: cannot find symbol
symbol  : class Inventory
location: class adventure.engine.PlayerCharacter
    private Inventory inventory = new Inventory();
            ^
PlayerCharacter.java:18: cannot find symbol
symbol  : class Inventory
location: class adventure.engine.PlayerCharacter
private Inventory inventory = new Inventory();

对此的任何反馈将不胜感激。如何解决这个问题?

4

1 回答 1

3

两件事情。

1) 你可能没有编译 Inventory
2) PlayerCharacter 和 Inventory 在同一个包中。所以不需要导入。

您应该将它们编译为

javac冒险/引擎/Inventory.java

javac冒险/引擎/PlayerCharacter.java

于 2012-10-06T08:49:23.853 回答