可变独立性宠物小精灵代码

时间:2018-08-10 04:23:58

标签: java class object static instances

我目前正在研究Java的Pokemon战斗系统的娱乐性,目的只是为了娱乐和学习。到目前为止,我有一个Pokemon班,Move班,Trainer班等,这些战斗可以正常进行。在《口袋妖怪》中,移动具有称为“能量点”的值,该值确定在战斗中可以使用移动的次数。出于某种原因,当将一个Move对象的相同实例(例如Hydro Pump)分配给两个不同的Pokemon时,如果一个Pokemon使用该移动,则另一个Pokemon的该移动实例的PP值也会降低一个。他们应该彼此独立。我已经做了很多自我教学,到目前为止,我想在要实现类似目标时使用了静态变量,但是我在这里没有使用任何静态变量,所以我不确定为什么会这样。我是否需要为同一移动创建不同的实例来避免这种情况,或者当移动变量属于两个单独的“口袋妖怪”对象时,是否有更简单的方法使其独立?我在下面包含了我的Move类的一些代码。

public class Move
{
    private final String name;
    private final String type;
    private int damage;
    private double accuracy;
    private int powerPoints;
    private int maxPowerPoints;
    private boolean priority; // label for speed priority
    private boolean physical; // physical label
    private boolean special; // special label

    public Move(String n, String a, int d, double h, int pp, boolean p, boolean ph, boolean sp){
        name = n;
        type = a;
        damage = d;
        accuracy = h;
        powerPoints = pp;
        maxPowerPoints = pp;
        priority = p;
        physical = ph;
        special = sp;
    }

    public String getType(){
        return type;
    }

    public String getName(){
        return name;
    }

    public int getDamage(){
        return damage;
    }

    public double getAccuracy() {
        return accuracy;
    }
    public void setAccuracy(int a) {
        accuracy = a;
    }

    public int getPP() {
        return powerPoints;
    }
    public int getMaxPP() {
        return maxPowerPoints;
    }
    public void setPP(int pp) {
        powerPoints = pp;
    }

    /**
     * Checks if a move is a physical move.
     * @return true if the move is a physical move.
     */
    public boolean isPhysical() {
        return physical;
    }

    /**
     * Checks if a move is a special move.
     * @return true if the move is a special move.
     */
    public boolean isSpecial() {
        return special;
    }

    /**
     * Checks if a move can be used (if it has any power points left)
     * @return true if a move can be used.
     */
    public boolean hasPP() {
        if (powerPoints >= 1) {
            return true;
        }
        else return false;
    }
    /**
     * Determines whether a move has priority over speed attributes
     * @return true if a move has priority
     */
    public boolean hasPriority(){
        return priority;
    }

    /**
     * Determines if a move hits in its attack turn by
     * generating a random number and checking the accurary value
     * @return whether the attack landed or not.
     */
    public boolean hit() 
    {
        double randy = Math.random();
        if (randy <= accuracy/100){
           return true;
        } 
        else return false;
    }

    /**
     * Calculates if a move is a critical hit or not. 
     * @return true if critical.
     */
    public boolean isCritical() {

        double randy = Math.random();
        if(randy <= .0625) {
            return true;
        }
        else return false;
}

1 个答案:

答案 0 :(得分:1)

  

我是否需要创建同一举动的不同实例来避免这种情况   还是有更简单的方法让Move的变量独立   当它们属于两个单独的“口袋妖怪”对象时?

是的,您应该创建一个新实例。 在http://www.informit.com/articles/article.aspx?p=174371&seqNum=4

上了解有关Java引用的更多信息。