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

2017/5/10

How to subtract units attack from enemies health

InfuriatedHog InfuriatedHog

2017/5/10

#
So I am having a problem. My program is a very Fire Emblem-esque type game, similar to chess with stats. The Enemies and Allies have to Intersect on the board for one of them to be able to attack. I have 2 subclasses, Enemies and Allies, each with many subclasses of their own. Each subclass of those two, or units, have two unique stats, Health and Attack. I have gotten it to the point where it will return the unit(enemy) an ally is intersecting, but I cant figure out how to subtract the two stats. The
  if( "yes".equals(WillAttack))
                {
                    getIntersectingEnemies().Health - MasterSwordsman.Attack;
                    
                }
is what I am having trouble with specifically. (all the rest of the code)
public class MasterSwordsman extends Allies
{
    
   public void act() 
    {
       if(Greenfoot.isKeyDown("0"))
       {
        askaction();
       }
    }
    public int Health = 17;
    public int Attack = 19;
    String Action;
    Actor Enemy;
    
    public void askaction()
    {
    Action = Greenfoot.ask("What would you like the swordsman to do?");
        if ("move".equals(Action) &! "attack".equals(Action))
    {
        String MovementSpace = Greenfoot.ask("how many spaces would you like to move the swordsman?");
        int moveAmount = Integer.parseInt(MovementSpace);
        String MoveDirection = Greenfoot.ask("What direction? (up, left, right, or down)");
        if( "up".equals(MoveDirection))
        {
            setLocation(getX(), getY() - moveAmount);
        }
         if( "left".equals(MoveDirection))
        {
            move(-moveAmount);
        }
        if( "down".equals(MoveDirection))
        {
            setLocation(getX(), getY() + moveAmount);
        }
         if( "right".equals(MoveDirection))
        {
            move(moveAmount);
        }
        
        if("move".equals(Action) && "attack".equals(Action))
        {
            if (canAttack() != false)
            {
                String WillAttack = Greenfoot.ask("Would you like to attack the unit?");
                if( "yes".equals(WillAttack))
                {
                    getIntersectingEnemies().Health - MasterSwordsman.Attack;
                    
                }
            }
        }
        Greenfoot.stop();
    }
    
    }  
    
    public Actor getIntersectingEnemy() 
    {
        Actor Enemy = getOneIntersectingObject(Enemies.class);
        return(Enemies)getOneIntersectingObject(Enemies.class);
    }
    
    public boolean canAttack()
    {
       boolean unit = isTouching(Enemies.class);
       if(unit != false)
       {
           return true;
       }
            else
            {
                return false;
            }
    }
    
Thanks so much in advance!!!!
InfuriatedHog InfuriatedHog

2017/5/10

#
Heres the code I have for the enemy I am using to test it out,
public class EnemyCavalier extends Enemies
{
   
    public void act() 
    {
        // Add your action code here.
    }    
    public int Health = 25;
    public int Attack = 10;
}
(I'm focusing on the allies first if you're curious as to why there isn't much code for the enemy)
danpost danpost

2017/5/10

#
Remove the Health and Attack fields from all subclasses of Allies (lines 11 and 12 in the MasterSwordsman class) and replace them all with something like the following (example for the MasterSwordsman class):
public MasterSwordsman()
{
    Health = 17;
    Attack = 19;
}
Put the fields in the Allies class: If you have another class above Allies and Enemies that is not the Actor class, you could consider placing the fields in it, instead. By the way, you can move ALL codes that are the same in all subclasses to the superclass, removing them from the subclasses (that would include, at minimum, lines 58 through 72). The 'askAction' method can probably be moved also, since you will be able to access the field values from the superclass. You can do the same for the Enemies subclasses and the Enemies class. And any codes that end up the same in both the Enemies and Allies classes can be moved to their superclass, if shared and that class is not the Actor class. The 'if' condition on line 41 will never be true. A string cannot have two different character sequences at the same time.
InfuriatedHog InfuriatedHog

2017/5/18

#
Thank you so much!
You need to login to post a reply.