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

2015/4/5

Wrong values for health being displayed?

ProfessionalNoob ProfessionalNoob

2015/4/5

#
I am trying to get the correct values for the card health, but it seems to be displaying the wrong value. For example, the "ogre" card has a cost of 6, an attack of 6 and a health of 7. However, the program is showing that it has only 5 health.
import greenfoot.*;

/**
 * Write a description of class Ogre here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Cards extends Actor
{
    
    private static Cards attacker;
    SomeDice dice = new SomeDice();
    int health;
    int cost;
    int attack;
    int number;
    boolean click;
    boolean Selected;
    public GreenfootSound[] CardSounds = { new GreenfootSound("KittyPlaySound.mp3"),
                                                         new GreenfootSound("HoundPlaySound.mp3"),
                                                         new GreenfootSound("OgrePlaySound.mp3"),
                                                         new GreenfootSound("RaptorPlaySound.mp3"),
                                                         new GreenfootSound("MurlocPlaySound.mp3"),
                                                         new GreenfootSound("GrommashPlaySound.mp3")};
    public static final GreenfootImage[] CardImages = { new GreenfootImage("Kitty.png"),
                                                         new GreenfootImage("Hound.png"),
                                                         new GreenfootImage("Ogre.png"),
                                                         new GreenfootImage("Raptor.png"),
                                                         new GreenfootImage("Murloc.png"),
                                                         new GreenfootImage("Grommash.png")};
                                                         
    private boolean visible;
    public GreenfootImage BackCard = new GreenfootImage("CardBack.png");
    boolean Grab = false;
    boolean MouseHover = false;
    boolean Deployment = false; 
    boolean OnBoard = false;
    boolean Dep = false;
    boolean val = false;
    boolean battleposition;
    boolean initialized = false;
    boolean attacked;
    boolean Grabbed;
    int NormalHeight = CardImages[number].getHeight();
    int NormalWidth = CardImages[number].getWidth(); 
    int position;
    int limit;
    int Cardvalue;
 
    
    public void act() 
    {
   int x = getX();
   int y = getY();
   String HP = Integer.toString(health);
    if (click == false)
    { 
        Generation(); 
        click = true;
    }
        mouseDragging();  
        Deployment();
    if(!Dep && Deployment)
    {
        Position();
        position++; 
        Dep = true; 
    }
 
    
    
    if(Deployment && Greenfoot.mouseClicked(this))
    {
        getWorld().showText(HP, x, y);
        if (attacker == null) attacker = this;
        else if(attacker != this)
        {
         loseHealth(attacker.attack);
         if (health <= 0)
         {
             getWorld().removeObject(this); 
         }
            attacker = null;
        }
        else attacker = null;
    }
  
}
    public void CardImage()
    {
        if(getY() > 400)
        {
            for (Object PlayerCards: getObjectsInRange(100, Cards.class))
            {
                Cards a = (Cards) PlayerCards;
                a.setImage(BackCard);
            }
        }
        
        if(getY() < 400)
        {
            for (Object PlayerCards: getObjectsInRange(100, Cards.class))
           {
               Cards a = (Cards) PlayerCards;
               a.setImage(BackCard);
           }
           
        }
    }
   public int Generation()
   {

    number = dice.Roll(number);
    CardImages[number].scale(NormalWidth / 2, NormalHeight / 2); 
    setImage(CardImages[number]);

    switch(number)
    {
        case 0:
            cost = 0;
            attack = 1;
            health = 1;
            return cost;
        case 1:
            cost = 7;
            attack = 9;
            health = 5;
            return cost;
        case 2:
            cost = 7;
            attack = 9;
            health = 5;
            return cost;
        case 3:
            cost = 6;
            attack = 6;
            health = 7;
            return cost;
        case 4:
            cost = 2;
            attack = 3;
            health = 2;
            return cost;
        case 5:
            cost = 1;
            attack = 2;
            health = 1;
            return cost;
    }
    return 10;
}
public void setVisible(boolean state)
{
    visible = state; 
    if(!Deployment)
    {
    if(visible == true)
    {
        setImage(CardImages[number]);
    }
    else if (visible == false)
    {
        setImage(BackCard);
    }
}
}
public void show()
{
    setVisible(true);
}
public void hide()
{
    setVisible(false);
}
public boolean isVisible()
{
    return visible;
}
public void mouseDragging()
{
    if (Greenfoot.mousePressed(this) && !Grab)
    {
        Grab = true;
        return;
    }
    if ((Greenfoot.mouseDragged(this)) && Grab)
    {
        MouseInfo Mouse = Greenfoot.getMouseInfo();
        setLocation(Mouse.getX(), Mouse.getY());
        return;
    }
    if (Greenfoot.mouseDragEnded(this) && Grab)
    {
        Grab = false;
        Grabbed = true; 
        return;
    }
}
public void Deployment()
{
    if(Deployment) return;
    if (!MouseHover && Greenfoot.mouseMoved(this))
        {
            CardImages[number].scale(NormalWidth, NormalHeight); 
            MouseHover = true;
        }
    if (MouseHover && Greenfoot.mouseMoved(null) && ! Greenfoot.mouseMoved(this))
        {
            CardImages[number].scale(NormalWidth / 2, NormalHeight / 2); 
            MouseHover = false;
        }
    if(MouseHover && Greenfoot.mouseMoved(null) && Greenfoot.mouseMoved(this) && getY() < 400 && Grabbed)
    {
        MouseHover = false;
        CardImages[number].scale(NormalWidth / 2, NormalHeight / 2); 
        Deployment = true; 
    }

}
public boolean DeploymentBoolean()
{
    return Deployment;
}
public void Position()
{

     if(limit < 8)
    {
       setLocation(130, 300);
       while (isTouching(Cards.class)) move(50);
       CardSounds[number].play();
       limit++;
    }
    
}
public void loseHealth(int damage)
{
    health -= damage;
}
}
Even if theoretically the card is attacking itself (the losehealth method), it should only have one HP.
Super_Hippo Super_Hippo

2015/4/5

#
In line 75, you display the HP, you change it in line 79. I think you should move line 75 to somewhere after 79, for example after 86.
ProfessionalNoob ProfessionalNoob

2015/4/6

#
Thank you, but it seems that it does not work. What happens is that when I click on a card, it shows it's health. So as soon as I click a card WITHOUT it being attacked, it shows the wrong HP value. The ogre always shows a 5 when it should be a 7.
danpost danpost

2015/4/6

#
What is the code for the 'Roll' method of the SomeDice class?
Super_Hippo Super_Hippo

2015/4/6

#
What if you use the following at the same position I suggested last post?
getWorld().showText(""+health, x, y);
ProfessionalNoob ProfessionalNoob

2015/4/7

#
Super_Hippo wrote...
What if you use the following at the same position I suggested last post?
getWorld().showText(""+health, x, y);
I tried that, but it still doesn't work.
danpost wrote...
What is the code for the 'Roll' method of the SomeDice class?
It's pretty basic.
 public static int Roll(int value)
    {
        value = Greenfoot.getRandomNumber(6); 
        return value;
    }
danpost danpost

2015/4/7

#
ProfessionalNoob wrote...
danpost wrote...
What is the code for the 'Roll' method of the SomeDice class?
It's pretty basic.
 public static int Roll(int value)
{
    value = Greenfoot.getRandomNumber(6); 
    return value;
}
Okay. There seems to be no purpose in have an 'int value' parameter for the method since you are setting its value inside the method and not using the value that it would initially contain. Therefore, this is all you need for the method:
public static int roll()
{
    return Greenfoot.getRandomNumber(6);
}
I changed the first letter of 'Roll' to lowercase to follow convention (changed it to 'roll'). Change line 114 in the Cards class code above to this:
number = dice.roll();
I was hoping to find something amiss in that method (as far as the value returned; but it is not here. After further investigation, I found that there is something amiss with respect to the Cards class and what you stated was the problem. You say the Ogre should have a health of 7, which is given to the Card with a number of 3. However the image for number 3 is not the Ogre image, it is the Raptor image. Make sure that the values given for each number value are for what image is assigned that number index in the image array. The first image in the array has a number index of zero (which is the lowest number that can be returned by a dice roll). Looking at it the other way around. The Ogre image, which has a number index of 2 cooresponds to the health value of 5, given to the Cards object with a number value of 2. Since 5 is what you were getting, the formatting seems to be alright; but the pairing is off between the values and the images.
ProfessionalNoob ProfessionalNoob

2015/4/7

#
It seems I have the same problem if I do this:
 public int Generation()
   {

    number = dice.Roll();
    CardImages[number].scale(NormalWidth / 2, NormalHeight / 2); 
    setImage(CardImages[number]);

    switch(number)
    {
        case 0:
            cost = 0;
            attack = 1;
            health = 1;
            return cost;
        case 1:
            cost = 7;
            attack = 9;
            health = 5;
            return cost;
        case 2:
            cost = 7;
            attack = 9;
            health = 5;
            return cost;
        case 3:
            cost = 6;
            attack = 6;
            health = 7;
            return cost;
        case 4:
            cost = 1;
            attack = 2;
            health = 1;
            return cost;
        case 5:
            cost = 8;
            attack = 4;
            health = 9;
            return cost;
    }
    return 10;
}
danpost danpost

2015/4/7

#
Your Ogre image is at index number 2 in the array. The values given to that image are at 'case 2' in the code given. It will get a health value of 5, as is. Either rearrange the images in the array to coorespond to the appropriate case numbers, or change the case numbers to coorespond with the order of the images in the array.
ProfessionalNoob ProfessionalNoob

2015/4/7

#
Thanks, I changed the values and that worked.
You need to login to post a reply.