This site requires JavaScript, please enable it in your browser!
Greenfoot back
nat12
nat12 wrote ...

2017/5/25

Possible to make changeable method and change it accordingly.

nat12 nat12

2017/5/25

#
So I have two classes that each should use the "same" method from their superclass, however there is one variable that should changed depending on the class calling it.
            public void checkForBullet()
    {
        
        
        if (getOneIntersectingObject(PlayerBullet.class) != null)
        {
            if(VARIABLE > 0)
            {
                getWorld().removeObject(getOneIntersectingObject(PlayerBullet.class));
                VARIABLE --;
            }
            else
            {
                getWorld().removeObject(getOneIntersectingObject(PlayerBullet.class));
                GameWorld gameworld = (GameWorld)getWorld();
                Counter counter = gameworld.getCounter();
                counter.addScore();
                gameworld.killCount();
                getWorld().removeObject(this);
            }
        }
    }
The variable that should change depending on the class calling the method is "VARIABLE ". If it is the enemyShip calling this method and it is indeed touching the PlayerBullet.class it should change the "VARIABLE" to "enemyHP". If it is the bossShip calling this method and is touching the bullet it should change the variable to "bossHP". I'm not sure if this is possible, but I would appreciate it if someone gave it a go. As you can see, I only need to change one variable in the method for it to be useful for two classes, so I really don't want to copy and paste the code into each of the classes, or make two very similar methods.
danpost danpost

2017/5/25

#
Remove the line declaring bossHP from the boss class and the line declaring enemyHP from the enemy class. Then, wherever either is used in the two classes, change it (either 'bossHP' or 'enemyHP') to just 'hp'. Finally, declare 'hp' in the superclass and change 'VARIABLE' to 'hp' in that class as well. In other words, you want to declare 'hp' for all instances of the superclass (which include instances of its subclasses) in the superclass. Any methods you might have to adjust the value of the health in the subclasses can also be move into the superclass. Each subclass can set its own starting value for objects of the class individually in its constructor. Briefly, you could have these three classes (a class with two subclasses to that class):
public class Enemy extends greenfoot.Actor
{
    int hp;

    public void adjustHP(int amount)
    {
        hp += amount;
        if (hp <= 0) getWorld().removeObject(this);
    }
}

/* ******************************************************** */

public class Boss1 extends Enemy
{
    public Boss1()
    {
        hp = 50;
    }
}

/* ******************************************************** */

public class Boss2 extends Enemy
{
    public Boss2()
    {
        hp = 100;
    }
}
A Boss1 object will start with 50 hp and a Boss2 object will start with 100. The 'adjustHP' method can be called on either type of object.
You need to login to post a reply.