所以问题是,当我说ovrxp = ovrxp + xp
时,它永远不会堆叠,只会重置每次杀戮。解决这个问题并解释为什么这不起作用将非常感激。
@EventHandler
public void onDeath(EntityDeathEvent e) {
Player player = (Player) e.getEntity().getKiller();
Skeleton s = (Skeleton) e.getEntity();
int ovrlvl = 1;
int ovrxp = 0;
Random random = new Random();
int xp = random.nextInt(30) + 21;
if (ovrlvl == 1 && ovrxp >= 200) {
player.sendMessage(ChatColor.GREEN + "You are now level two!");
player.playSound(player.getLocation(), Sound.LEVEL_UP, 1.0F, 0.0F);
ovrlvl = 2;
}
if (ovrlvl == 2 && ovrxp >= 400) {
player.sendMessage(ChatColor.GREEN + "You are now level three!");
player.playSound(player.getLocation(), Sound.LEVEL_UP, 1.0F, 0.0F);
ovrlvl = 3;
}
ovrxp = ovrxp + xp;
if (s.getCustomName() == "Undead Recruit") {
if (ovrlvl == 1) {
player.sendMessage(ChatColor.GREEN + "" + ovrxp + "/200");
}
if (ovrlvl == 2) {
player.sendMessage(ChatColor.GREEN + "" + ovrxp + "/400");
}
}
}
}
答案 0 :(得分:1)
您已将ovrxp
声明为本地变量 - 每次调用onDeath
时都会对其进行初始化。
如果希望值在方法的多次调用之间保持不变,则需要将变量设置为字段(对象本身的一部分)。假设该方法总是在同一个对象上调用,并且在同一个线程上,只需将其设为实例字段即可。