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

2015/4/12

Question about Inherited classes

ProfessionalNoob ProfessionalNoob

2015/4/12

#
I was wondering how to make a certain object use inherited classes properly. I have a spells class in my game that does one damage to a minion at the cost of one player and gives two health to the other. In my cards class, I have an attack method that does this:
public void Attack()
{
    if (attacker == null) attacker = this;          //set attacker
    else if(attacker != this)        //if there already is an attacker, attack this with the attacker
    {
         loseHealth(attacker.attack);
         attacker.loseHealth(attack);               //If you are attacking someone, have both loose health.
         attacker = null;
         array.add(this);
         array.add(attacker);
     }
}
I want to use this method on my spells class so that the fireball can do one damage to a minion at the cost of two mana. I also set it to have infinite health so that it doesn't "die".
import greenfoot.*;


/**
 * Write a description of class Spells here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Spells extends Cards
{
    /**
     * Act - do whatever the Spells wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    GreenfootImage FireBlast = new GreenfootImage("Spell.png");             //Hero power images
    GreenfootImage Armor = new GreenfootImage("Armor.png");
    GifImage gifImage = new GifImage("GoodEnoughISuppose.gif");         //Animate the fireball gif
    boolean fire;                                       //boolean for the fireball
    double inf = Double.POSITIVE_INFINITY;
    int health = (int)inf; 
    int attack = 1;
    boolean armor;
    public void act() 
    {
    if(getY() > 400)        //check if warrior or mage.
    {
        setImage(FireBlast);                //Set the image of the fireblast.
        if(Greenfoot.mousePressed(this)) 
        {
            fire = true;                //If you have clicked the object, this variable is true.
        }
        if(fire)
        {
            setImage(gifImage.getCurrentImage());   //Once the variable is true, animate the image.
        }
        
    if(fire)
    {
        Attack(); 
    }
  
    }    
    if(getY() < 400)            //If warrior, set the armor image.
    {
        setImage(Armor);
        
    }

}


public int Cost()
{
    if(Greenfoot.mouseClicked(this))
    {
        attack = 2;
        return 2;
    }
    return 0;
}

}
But it appears that my code does not seem to work properly. It does not deal any damage to any minions.
Super_Hippo Super_Hippo

2015/4/12

#
I think the problem is, that it is usually like this: - click on the attacking card - click on the card which should be attacked Now you have something in the act method of the spell class which does something itself. If it should be a normal class which other "stats" (for example 1 attack), maybe the add method could be the same.
ProfessionalNoob ProfessionalNoob

2015/4/12

#
So how can I make it simply attack a card without doing damage to itself? Using
if(Greenfoot.mouseClicked(Cards.class))
{
     Cards.loseHealth(1); 
}
does not seem to work because I can't reference the method to all cards classes, only specific ones.
Super_Hippo Super_Hippo

2015/4/12

#
Well... you could do what you tried like this:
for (Object obj : getWorld().getObjects(Cards.class))
{
    Cards c = (Cards) obj;
    if (Greenfoot.mouseClicked(c)) c.loseHealth(1);
}
ProfessionalNoob ProfessionalNoob

2015/4/12

#
Thanks, but do you know how to reset that? For example, once I've played it, I want to reset it so that you can't use it until the end of your turn.
ProfessionalNoob ProfessionalNoob

2015/4/13

#
Thanks for your help. Using an arraylist is one way to do it
You need to login to post a reply.