我是Java的初学者,目前正在尝试制作基于文本的基本游戏。使用4个类:.6
,Main
,Character
和World
,我建立了一个地图系统,其中有5个Constructor
定义为:>
ArrayList<Point>
并且public class Constructor {
//Make Worlds
World desktopSize = new World(15, 15);
World laptopSize = new World(10,10);
World routerSize = new World(10, 5);
World dlTravelSpace = new World(3,1);
World dlrTravelSpace = new World(1, 3);
}
的创建方式为:
World
在public class World {
private int x, y;
private String direction;
private int index;
ArrayList<Point> mapSize = new ArrayList();
Scanner scan = new Scanner(System.in);
public World(int x, int y){
this.x = x;
this.y = y;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
mapSize.add(new Point(i + 1, j + 1));
}
}
}
}
类中,我使用以下方法设置所有属性:
Character
当我调用 public void setCharacter(String charClass, Point currentPoint) {
if (charClass.equals("warrior")) {
setName("Sparky");
setLevel(1);
setHp(100);
setAttack(50);
setDefense(20);
setSpeed(10);
//setSkills();
setBackPackSpace(15);
//setInventory();
setCredits(1000);
setCurrentPoint(10, 8);
} else if (charClass.equals("ranged")) {
setName("Sparky");
setLevel(1);
setHp(90);
setAttack(60);
setDefense(10);
setSpeed(12);
//setSkills();
setBackPackSpace(14);
//setInventory();
setCredits(950);
setCurrentPoint(10, 8);
} else if (charClass.equals("tank")) {
setName("Sparky");
setLevel(1);
setHp(110);
setAttack(40);
setDefense(30);
setSpeed(9);
//setSkills();
setBackPackSpace(16);
//setInventory();
setCredits(1050);
setCurrentPoint(10, 8);
}
}
方法时,我使用:
setCharacter
但是,当我运行代码时,它将返回以下错误:
if (player.getCharClass().equals("warrior") ||
player.getCharClass().equals("ranged") ||
player.getCharClass().equals("tank")) {
System.out.println("Class " + player.getCharClass() + " selected!");
System.out.println("");
player.setCharacter(player.getCharClass(),
player.getCurrentPoint());
} else {
System.out.println("Invalid class selected. Please restart and try
again.");
System.exit(0);
}
错误似乎发生在
Error:(34, 13) java: method setCurrentPoint in class Character cannot be
applied to given types;
required: java.awt.Point
found: int,int
reason: actual and formal argument lists differ in length
在
public void setCharacter(String charClass, Point currentPoint) {
if (charClass.equals("warrior")) {
setName("Sparky");
setLevel(1);
setHp(100);
setAttack(50);
setDefense(20);
setSpeed(10);
//setSkills();
setBackPackSpace(15);
//setInventory();
setCredits(1000);
setCurrentPoint(10, 8);
setCurrentPoint方法是
setCurrentPoint(10, 8);
我的问题是:如何设置角色的point属性,使其在世界desktopSize中处于(10,8)。创建时
答案 0 :(得分:0)
您的setCurrentPoint
方法未在此处显示,但基于错误,我认为它是类似的
public void setCurrentPoint(Point somePoint) {...}
您正在尝试使用两个整数(10和8)调用该方法,从而引发错误。您正在使用Point
的{{1}}方法中传递一个setCharacter
对象。您要设置为当前点的currentPoint
对象吗?如果是这样,您应该按以下方式致电Point
:
setCurrentPoint
如果要将10和8硬编码为坐标,则可以使用这些值创建一个新的setCurrentPoint(currentPoint);
对象,并将其发送到Point
方法:
setCurrentPoint
希望这会有所帮助。