游戏引擎从另一个类或文件中设置java数组的元素
我现在正在制作一个游戏,我正在尝试设置一个CreatePlayer方法
在main类中,我将Player类作为对象来获取它的变量、方法等
package com.deud07.main;
import com.deud07.player.Player;
public class Main {
public static Player player = new Player("Bob", 86, null);
public static void main(String[] args) {
System.out.println(player.Position);
}
}
玩家的第三个参数是位置,它是一个数组。 我遇到的问题是,我不知道如何在不写入以下内容的情况下设置数组的每个元素:
position[0] = 1f;
position[1] = -6f;
position[2] = 0f;
玩家代码:
package com.deud07.player;
public class Player {
public static String Name;
public static int ID;
public static float x;
public static float y;
public static float z;
public static float[] Position = {x, y, z};
public Player(String name, int id, float[] pos) {
Player.Name = name;
Player.ID = id;
Player.Position = pos;
}
public void createPlayer(String name, int id, float[] pos) {
Player player = new Player(name, id, pos);
player.Name = name;
player.ID = id;
player.Position = pos;
}
}
有什么解决办法吗?当你这么做的时候,我能做些什么来修正我的代码
# 1 楼答案
我相信你要求的是一种速记方法。你也可能会发现它们是“一句台词”。在没有变量的情况下,在一行中启动数组的方法如下:
至于“修复代码”,它超出了你发布的实际问题。我能说的两件事是
Player
类的属性应该是name
、id
和position
李>createPlayer()
与构造函数的作用完全相同,因此它不是必需的。要创建一个新的Player
,只需使用。。。嗯new Player()
李>而且,
x
、y
和z
都是无用的。例如,如果需要x,只需使用position[0]