2

I'm trying to write a small game to help with my Java skills. I have a class called "Zombie" and "Player" and I created instances of these classes as so:

Zombie zombie = new Zombie(50, "Infected Zombie", "Slash");
Player defaultPlayer = new Player(100, "Default Player");

Next I requested user input for the attack style of the player:

System.out.println("Which attack style would you like to use?");
        defaultPlayer.printAttackStyles();
        int option = scanner.nextInt();
        scanner.nextLine();
        switch(option) {
            case 0:
                System.out.println("You backed out of the fight.");
                break;
            case 1:
                System.out.println("Punching...");
                defaultPlayer.attack(1);
                break;
            case 2:
                System.out.println("Kicking...");
                defaultPlayer.attack(2);
                break;
            case 3:
                System.out.println("Headbutting...");
                defaultPlayer.attack(3);
                break;
            case 4:
                System.out.println("Tackling...");
                defaultPlayer.attack(4);
                break;
            default:
                System.out.println("Not a valid attack style");
        }

In my "Player" class I have a method called attack which inflicts a certain amount of damage based on attack style:

public int attack(int attackStyle) {
        int damage = 0;

        switch(attackStyle) {
            case 0:
                damage = 0;
                break;
            case 1:
                damage = random.nextInt(20) + 1;
                zombie.removeHealth(damage);
                break;
            case 2:
                damage = random.nextInt(25) + 1;
                zombie.removeHealth(damage);
                break;
            case 3:
                damage = random.nextInt(30) + 1;
                zombie.removeHealth(damage);
                this.health -= random.nextInt(5) + 1;
                break;
            case 4:
                damage = random.nextInt(45) + 1;
                zombie.removeHealth(damage);
                this.health -= random.nextInt(10) + 1;
                break;
        }

        return damage;
    }

In each case of the attack method, I have a line of code that says

zombie.removeHealth(damage);

Since the instance is only declared in the Main class how can I access that instance in order to access the method removeHealth() in the zombie class? Sorry if this question is simple but I can't figure this out.

2
  • pass the reference of Zombie in attack method? like this: defaultPlayer.attack(3, zombie ); Now inside attack method, you can use the reference of zombie and invoke methods on it. Commented Jan 16, 2017 at 3:53
  • Oh that makes sense, thank you! Commented Jan 16, 2017 at 4:00

2 Answers 2

3

In your method attack you need to add 1 more argument which can be used to pass your instance of Zombie class to attack method. Make your method signature as public int attack( int attackStyle, Zombie zombie )

Now in switch block when you call defaultPlayer.attack pass the int value as you was passing earlier and instance of your Zombie class like this

defaultPlayer.attack ( your int value, zombie)

This will pass the zombie instance to attack method then you can the same zombie instance there to call your removeHealth(damage) method.

Hope this solve your query.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! Clear answer as well.
2

pass the reference of Zombie in attack method? like this: defaultPlayer.attack(3, zombie ); Now inside attack method, you can use the reference of zombie and invoke methods on it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.