Hello,
I'm currently making a game and there are subclasses of enemies that have their own unique sets of characteristics, such as HP. I've made it so that whenever the player comes in contact with an enemy, it should send them to the fight screen world, which takes the enemy actor as a parameter, it then spawns in the enemy actor. However, when I created this class, I used minion1 as a placeholder, and I cannot make it work with any enemy that the player touches. I made a method in the player class that detects what enemy has been touched, and I use that method in the attack class in order to spawn said enemy, but since they are all a subclass of Enemies, I cannot seem to make it work. Any help would be appreciated.
// Player class public void act() { movement(); fightTrigger(); levelUp(); borderDetection(); playMusic(); if (fighting == false) { theme.stop(); } } public Actor getEnemyTrigger() { fighting = true; Minion one = new Minion(); Minion2 two = new Minion2(); Minion3 three = new Minion3(); Boss finalBoss = new Boss(); Actor minion1Trigger = getOneIntersectingObject(Minion.class); if (minion1Trigger != null) { return one; } Actor minion2Trigger = getOneIntersectingObject(Minion2.class); if (minion2Trigger != null) { return two; } Actor minion3Trigger = getOneIntersectingObject(Minion3.class); if (minion3Trigger != null) { return three; } Actor BossTrigger = getOneIntersectingObject(Boss.class); if (BossTrigger != null) { return finalBoss; } return null; } //Alternate Attempt public void fightTrigger() { fighting = true; Minion one = new Minion(); Minion2 two = new Minion2(); Minion3 three = new Minion3(); Boss finalBoss = new Boss(); Actor minion1Trigger = getOneIntersectingObject(Minion.class); if (minion1Trigger != null) { Greenfoot.setWorld(new FightScreen(one)); } Actor minion2Trigger = getOneIntersectingObject(Minion2.class); if (minion2Trigger != null) { Greenfoot.setWorld(new FightScreen(two)); } Actor minion3Trigger = getOneIntersectingObject(Minion3.class); if (minion3Trigger != null) { Greenfoot.setWorld(new FightScreen(three)); } Actor BossTrigger = getOneIntersectingObject(Boss.class); if (BossTrigger != null) { Greenfoot.setWorld(new FightScreen(finalBoss)); } } public int getHP() { return health; } public void setHP(int newHP) { health = newHP; } public int getAttack() { return attack; } public void setAttack(int newAttack) { attack = newAttack; } public int getXP() { return XP; } public void addXP(int newXP) { XP = XP + newXP; } public void levelUp() { if (XP > 8) { health += 2; attack += 2; XP = 0; } //FightMenu, subclass of Player public class FightMenu extends Player { public GreenfootSound fightMusic = new GreenfootSound("fightsong.mp3"); public GreenfootSound bonk = new GreenfootSound("le bonk.mp3"); Player dog = new Player(); Minion enemy = new Minion(); //should be replaced with whatever actor the player touches int playerHP = dog.getHP(); int minionHP = enemy.getEnemyHP(); int playerAttack = dog.getAttack(); int enemyAttack = enemy.getEnemyAttack(); public void act() { dog.stopMusic(); createEnemy(dog.getEnemyTrigger()); } public void createEnemy(Actor enemy) { getWorld().addObject(enemy, 320,400); } //Attack class, subclass of FightMenu public class Attack extends FightMenu { public void act() { dog.stopMusic(); checkVictory(); checkLose(); clicked(); playMusic(); if(minionHP<= 0 || playerHP <= 0) { stopMusic(); getWorld().removeObject(enemy); } } public void checkVictory() { if (minionHP <= 0) { dog.addXP(dog.getXP() + enemy.getEnemyXP()); getWorld().removeObject(enemy); getWorld().showText("You Won!", 320, 365); getWorld().showText("", 530,400); Greenfoot.delay(120); getWorld().showText("You gained " + enemy.getEnemyXP() + " experience!", 320, 365); Greenfoot.delay(120); stopMusic(); levelUp(); Greenfoot.setWorld(new BasementAfterFight()); dog.setFightStatus(false); } } public void clicked() { getWorld().showText("What will you do?", 320,365); getWorld().showText("Enemy HP: " + minionHP, 530,380); getWorld().showText("Your HP: " + playerHP, 530, 420); if (Greenfoot.mouseClicked(this)) { playBonk(); int amountAttacked = (int)(Math.random()*playerAttack) + 2; minionHP = minionHP - amountAttacked; playBonk(); getWorld().showText("You attacked for " + amountAttacked +"!", 320,365); if(minionHP <= 0) { minionHP = 0; } getWorld().showText("Enemy HP: " + minionHP, 530,380); Greenfoot.delay(120); getWorld().showText("", 320,355); enemyAttack(); } } public void enemyAttack() { if(minionHP != 0) { playBonk(); int amountEnemyAttacked = (int)(Math.random()*enemyAttack) + 2; playerHP = playerHP - amountEnemyAttacked; getWorld().showText("You're attacked for " + amountEnemyAttacked +"!", 320,365); if(playerHP <= 0) { playerHP = 0; } getWorld().showText("Enemy HP: " + minionHP, 530,380); Greenfoot.delay(120); getWorld().showText("", 320,355); getWorld().showText("Your HP: " + playerHP, 530, 420); } } public void checkLose() { if (playerHP <= 0) { getWorld().showText("You Lost.", 320, 365); Greenfoot.delay(120); stopMusic(); Greenfoot.setWorld(new StartMenu()); } } public void levelUp() { if (dog.getXP() > 8) { dog.setHP(dog.getHP() + 2); dog.setAttack(dog.getAttack() + 2); getWorld().showText("You Leveled Up!", 320, 365); Greenfoot.delay(120); getWorld().showText("All Stats Increased By 3.", 320, 365); Greenfoot.delay(120); } } } // Enemies class public class Enemies extends Actor { private int speed = 2; private int hp; private int attack; private int enemyXP; /** * Act - do whatever the Enemies wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { movement(); checkVictory(); } public void movement() { if(this.getWorld().getClass() == Basement.class ||this.getWorld().getClass() == EnemyRoom.class ) { if((this.isTouching(Wall2.class))) { speed = -speed; move(speed); } move(speed); } } public void checkVictory() { Player dog = new Player(); Minion enemy = new Minion(); int playerHP = dog.getHP(); int minionHP = enemy.getEnemyHP(); if (minionHP <= 0) { dog.addXP(dog.getXP() + enemy.getEnemyXP()); getWorld().removeObject(enemy); getWorld().showText("You Won!", 320, 380); Greenfoot.delay(120); getWorld().showText("You gained " + enemy.getEnemyXP() + " experience!", 320, 380); Greenfoot.setWorld(new Basement()); } } public int getEnemyHP() { return hp; } public void setMinionHP(int newHP) { hp = newHP; } public int getEnemyAttack() { return attack; } public int getEnemyXP() { return enemyXP; } } // Example of enemy, subclass of enemies public class Minion extends Enemies { public int hp = 10; private int attack = 3; private int enemyXP = 4; /** * Act - do whatever the Minion wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { movement(); } public int getEnemyHP() { return hp; } public void setMinionHP(int newHP) { hp = newHP; } public int getEnemyAttack() { return attack; } public int getEnemyXP() { return enemyXP; } } // Fight screen public FightScreen(Actor enemy) { super(640, 480, 1); addObject(enemy, 320, 175); addObject(new BoxTest(),320, 400); addObject(new IconTest(),90,395); addObject(new Attack(), 320, 410); }