我有一种创造"精神的方法"我游戏中的物体。我创建了一个精神类的实例,然后为我想要的精神调用一个方法。例如:
public void risp(){
species = "Risp";
eletype = "plant";
baseattack=4; basespeed=4; baseevasion=5; basearmor=2; basemagres=3; basehealth = 13+2*strength; basemana = 13+intelligence;
attack = (int) (baseattack+.2*4*strength+strength); speed = (int) (basespeed+4*.2*dexterity); health = 13+2*strength; mana = 13+intelligence;
armor = (int) (basearmor+2*.2*strength); armit = 0; magres = (int) (basemagres+.2*3*intelligence); mrmit = 0; evasion = (int) (dexterity+.25*5*baseevasion); cooperation = 12; accuracy = 10+dexterity;
fire = 5; water = 35; air = 35; ice = 25; mud = 40; rock = 20; toxic = 25; energy = 10; dark = 15; death = 15; light = 20; plant = 0; psychic = 5;
ability1 = "prick"; ability2 = "photosynethesize"; ability3 = "regrowth"; ability4 = "None Equipped";
spiritimage = new Image(risptexture);
spiritimage2 = new Image(risptexture);
spiritxp = 0; spiritlvl = 1;
}
问题是,在我的高通中,我为某些值设置了公式,期望每当使用对象时变量都会更新。这似乎并非如此,考虑到这一点,它只是使用初始化的值。
所以,当我升级了我的" risp"并使它的基础攻击更高,比如10,它并不关心。 Risp仍然有它的初始攻击值,当我想要它应该是12时,它出现在6。
我创建的代表risp的对象存储在一个arraylist中,该arraylist包含我的"精神字符"。
我不知道如何更新基于攻击,速度,健康等公式的变量。
我知道我可以编写代码
if(spirit.species = "Risp") {
attack = (int) (baseattack+.2*4*strength+strength);
以及其他变量,然后在我改变risps的基本值时调用它。但那将是......打字太多了。我有很多精神。我想尽量避免这种情况。有更简单的方法吗?
答案 0 :(得分:0)
如果你正在使用Java,你总是可以让不同的角色成为不同的类,并让getter执行当前统计数据的计算。
例如:
public class Risp {
private int baseattack, basespeed; // ... and so on
// also have a constructor that sets all base stats
public void setStrength(int strength) {
this.strength = strength;
}
public String getSpecies() {
return "Risp";
}
public int getAttack() {
return (int) (baseattack+.2*4*strength+strength);
}
// remaining setters & getters omitted
}
然后,只要您需要使用这些值:
Risp risp = new Risp();
risp.getAttack(); // Returns attack with base stats
risp.setStrength(120);
risp.getAttack(); // Returns attack with base stats, but w/ strength 120