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:
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".
But it appears that my code does not seem to work properly.
It does not deal any damage to any minions.
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);
}
}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;
}
}
