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

2015/3/23

How to make objects attack one other?

ProfessionalNoob ProfessionalNoob

2015/3/23

#
I was wondering how a player makes an object attack one another. Let's say that a card has a certain attack value. I want it so that once the cards are "deployed", you can attack another card by clicking both of them. However, I can't find a way to make it attack because I don't know how to reduce their health by the attack variable.
import greenfoot.*;

/**
 * Write a description of class Ogre here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Cards extends Actor
{
 
    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;

    
    int NormalHeight = CardImages[number].getHeight();
    int NormalWidth = CardImages[number].getWidth(); 
    int position;
    int limit;
   
    public void act() 
    {
        //CardImage();
        if (click == false)
        { 
            Generation(); 
            click = true;
        }
       if(visible == true && getY() > 285)
        {
        mouseDragging();  
        Deployment();
    }
    if(visible == true && getY() < 285)
    {
        mouseDragging();  
        Deployment();
    }
    
    if(!Dep && Deployment)
    {
        Position();
        position++; 
        Dep = true; 
    }

}
    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 = 8;
            attack = 4;
            health = 9;
            return 8;
        case 1:
            cost = 0;
            attack = 1;
            health = 1;
            return 0;
        case 2:
            cost = 7;
            attack = 9;
            health = 5;
            return 7;
        case 3:
            cost = 6;
            attack = 6;
            health = 7;
            return 6;
        case 4:
            cost = 2;
            attack = 3;
            health = 2;
            return 2;
        case 5:
            cost = 1;
            attack = 2;
            health = 1;
            return 1;
    }
    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 Mousey = Greenfoot.getMouseInfo();
        setLocation(Mousey.getX(), Mousey.getY());
        return;
    }
    if (Greenfoot.mouseDragEnded(this) && Grab)
    {
        Grab = false;
        return;
    }
}

public void Deployment()
{
    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)
    {
        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++;
    }
}
}
This is my cards class. Each player will have different sets of cards. Friendly fire is okay, so I'm wondering how to make them attack. How can I make it so that they are able to attack one another once "deployed"? Any help would be greatly appreciated.
Super_Hippo Super_Hippo

2015/3/23

#
You could do it like this. Of course you have to check that the clicked card is a correct one (the owner of the card is currently acting). But maybe you get the way how to do it.
//in the Cards class
public void act()
{
    if (Greenfoot.mousePressed(this)) ((WorldName)getWorld()).clickedCard(this);
    //rest of act method
}

public void loseHealth(final int dmg)
{
    health -= dmg;
}
//in the world class
private Cards card1 = null;

public void clickedCard(Cards c)
{
    if (card1==null) card1=c;
    else if (c!=card1) //not attacking the same card
    {
        c.loseHealth( card1.attack );
        card1 = null;
    }
}
ProfessionalNoob ProfessionalNoob

2015/3/23

#
Super_Hippo wrote...
You could do it like this. Of course you have to check that the clicked card is a correct one (the owner of the card is currently acting). But maybe you get the way how to do it.
//in the Cards class
public void act()
{
    if (Greenfoot.mousePressed(this)) ((WorldName)getWorld()).clickedCard(this);
    //rest of act method
}

public void loseHealth(final int dmg)
{
    health -= dmg;
}
//in the world class
private Cards card1 = null;

public void clickedCard(Cards c)
{
    if (card1==null) card1=c;
    else if (c!=card1) //not attacking the same card
    {
        c.loseHealth( card1.attack );
        card1 = null;
    }
}
Thanks for the response, but I'm wondering how to incorporate my attack values to the health.
Super_Hippo Super_Hippo

2015/3/23

#
My idea was to save the first clicked card. If a second one is clicked, this one loses as much damage as the first card has attack.
ProfessionalNoob ProfessionalNoob

2015/3/23

#
One question: My card class is an array. How do I incorporate the array into the clickedCard method?
ProfessionalNoob ProfessionalNoob

2015/3/23

#
This is what I have so far: Apparently, it's telling me that "void cannot be converted into boolean" This is my card class.
import greenfoot.*;

/**
 * Write a description of class Ogre here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Cards extends Actor
{
    Player Player = new Player();
    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;

    
    int NormalHeight = CardImages[number].getHeight();
    int NormalWidth = CardImages[number].getWidth(); 
    int position;
    int limit;
   
    public void act() 
    {
        if(Greenfoot.mouseClicked(this))
        {
        if (Player.clickedCards(this))
        {
        }
    }

        if (click == false)
        { 
            Generation(); 
            click = true;
        }
       if(visible == true && getY() > 285)
        {
        mouseDragging();  
        Deployment();
    }
    if(visible == true && getY() < 285)
    {
        mouseDragging();  
        Deployment();
    }
    
    if(!Dep && Deployment)
    {
        Position();
        position++; 
        Dep = true; 
    }

    

}
    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 = 8;
            attack = 4;
            health = 9;
            return 8;
        case 1:
            cost = 0;
            attack = 1;
            health = 1;
            return 0;
        case 2:
            cost = 7;
            attack = 9;
            health = 5;
            return 7;
        case 3:
            cost = 6;
            attack = 6;
            health = 7;
            return 6;
        case 4:
            cost = 2;
            attack = 3;
            health = 2;
            return 2;
        case 5:
            cost = 1;
            attack = 2;
            health = 1;
            return 1;
    }
    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 Mousey = Greenfoot.getMouseInfo();
        setLocation(Mousey.getX(), Mousey.getY());
        return;
    }
    if (Greenfoot.mouseDragEnded(this) && Grab)
    {
        Grab = false;
        return;
    }
}

public void Deployment()
{
    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)
    {
        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(final int damage)
{
    health -= damage;
}

}
Since the cards are being generated in the player class, I put the stuff that's supposed to go in world there.
import greenfoot.*;
import javax.swing.JOptionPane;
import java.util.ArrayList;
import java.util.List;
 
/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    boolean cardPossessed[] = new boolean[2];
    //int[] Cards = new int[2];
    private boolean click = false;
    int health = 30;
    SomeDice playerdice = new SomeDice();
    int count = 0;
    int number;
    boolean button;
    int position;
    int CurrentMana;
    int MaxMana;
    GreenfootImage[] playerImages ;
    Cards[] playerCards = {new Cards(),new Cards(),new Cards(),new Cards(),new Cards(),new Cards()};
    int value = Greenfoot.getRandomNumber(6-position);
    private Cards NullCard = null;

    public Player()
    {
         int CurrentMana = 0; 
         int MaxMana = 0;
    }
    
     
   public void CardSelect()
    {
            
               CurrentMana ++;
               MaxMana++; 
               int number = 0;

               if (position == 7) return; 


               Cards card = playerCards[value];
               if(getY() > 400)
               {
               getWorld().addObject(card, 40 + (50*(position+1)^2), getY()+60);
            }
            if(getY() < 400)
            {
                getWorld().addObject(card, 40 + (50*(position+1)^2), getY() -40);
            }
               position++;
               playerCards[value] = playerCards[6-position];
               playerCards[6-position] = card;

              
              }
              
    
  
    public void act() 
    {  
            String manaDisplay = Integer.toString(CurrentMana);
            String mana = Integer.toString(MaxMana); 
            getWorld().showText(manaDisplay, 600, 550);
            getWorld().showText("/" + mana, 620, 550);
            
             
               
    }
    
    public void hideCards()
    {
        for (Cards Card : playerCards) Card.hide(); 
    }
 
    public void showCards()
    {
        for (Cards Card : playerCards) Card.show();
    }
    
    public void clickedCards(Cards c)
    {
        if(playerCards[value]==null)
        {
            playerCards[value] = NullCard;
        }
        else if(NullCard!= playerCards[value])
        {
            c.loseHealth(NullCard.attack);
            NullCard = null;
        }
    }
  
}
 
        
What's the source of the error?
ProfessionalNoob ProfessionalNoob

2015/3/23

#
Actually: this is what I have so far. Sorry about that.
 public void clickedCards(Cards c)
    {
        if(playerCards[value]==null)
        {
            playerCards[value] = NullCard;
        }
        else if(NullCard!= playerCards[value])
        {
            c.loseHealth(NullCard.attack);
            NullCard = null;
        }
    }
 public void act() 
    {
        if(Greenfoot.mouseClicked(this))
        {
            Player.clickedCards(this);
        }

        if (click == false)
        { 
            Generation(); 
            click = true;
        }
       if(visible == true && getY() > 285)
        {
        mouseDragging();  
        Deployment();
    }
    if(visible == true && getY() < 285)
    {
        mouseDragging();  
        Deployment();
    }
    
    if(!Dep && Deployment)
    {
        Position();
        position++; 
        Dep = true; 
    }

}
ProfessionalNoob ProfessionalNoob

2015/3/23

#
Sorry, but I still don't understand how to do this
Super_Hippo Super_Hippo

2015/3/23

#
ProfessionalNoob wrote...
    public void clickedCards(Cards c)
   {
       if(playerCards[value]==null)
       {
           playerCards[value] = NullCard;
       }
       else if(NullCard!= playerCards[value])
       {
           c.loseHealth(NullCard.attack);
           NullCard = null;
       }
   }
This is what you have. This can't work, because 'playerCards' won't be 'null'. Every field of the array holds a Cards object. To be honest, I don't know why 'value' is a random number. What are you doing with 'NullCard'? What this method is doing now, is the following: Check if the field with index 'value' of the array playerCards is null. If it is null, set this field to 'NullCard' which is also 'null'. (So nothing changes.) But as I said, it will never be null, so the 'else' is executed. 'NullCard!= playerCards' will always return 'true' because 'NullCard' is 'null' and 'playerCards' will never be 'null'. Then you attack the clicked card with the attack of the 'NullCard'. This can't work, because 'NullCard' isn't really a Card, it's 'null' and you can't get the attack of 'null'. Then you set 'NullCard' to 'null' again. Compare your code with the code I gave you and try to understand the differences. Another thing that won't work like this is:
Player.clickedCards(this);
The 'Player' field (which shouldn't have the exactly same name as the class → player) doesn't hold the same Player object as the one in the world. Instead, you create a new one. If I understand you correctly, you add a Player object to the world. This one creates the cards of the player and hold these Cards in an array. Then it should work like this:
//in the Player class
private Cards card1 = null;
//put 'clickedCards();' into the act method
public void clickedCards()
{
    for (int c=0; c<playerCards.length; c++)
    {
        if (Greenfoot.mousePressed(playerCards[c]))
        {
            if (card1==null) card1 = playerCards[c];
            else if (card1 != playerCards[c])
            {
                playerCards[c].loseHealth(card1.attack);
                card1 = null;
                //next player's turn
            }
            break;
        }
    }
}
Some other things: if you have an error, in you case 'void cannot be converted into boolean', you should mention where exactly the error occurs. Please use the usual java convention, for example all method and field names start with a small letter. It makes it easier to follow.
You need to login to post a reply.