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

2015/3/21

Not updating position?

ProfessionalNoob ProfessionalNoob

2015/3/21

#
I have a code that basically deploys a card once you click and drag it. However, it seems that my cards overlap. Here is the code in the card act method.
if(!Dep && Deployment)
    {
        Position();
        position++; 
        Dep = true; 
    }
Here is the Position() method:
public void Position()
{  
        setLocation(43 + (50*(position)^2), 300);
        CardSounds[number].play();
}
Basically, it's supposed to increase the value of position by one every time a card is deployed. I also made sure that the event only happens *once* by putting it inside an if statement. Yet, the cards always land on the same position. The value of position never seems to increase. Any help would be greatly appreciated since this minor issue seems to be the death of me.
Super_Hippo Super_Hippo

2015/3/21

#
I don't know if you know what 'position^2' does, but for example position^2 is not equal to position*position. If you want to do that, you have to use 'Math.pow(position,2)'. Although I am pretty sure this is not what you want. Maybe you should remove the '^2' there?
ProfessionalNoob ProfessionalNoob

2015/3/21

#
Thanks, but removing the ^ changes the original location a bit.
danpost danpost

2015/3/21

#
The problem is that each card created starts with a position field value of one. That is, each card uses its own position field when deployed (they do not share the same field among them). So, of course it does not appear to change as each card is deployed.
ProfessionalNoob ProfessionalNoob

2015/3/21

#
danpost wrote...
The problem is that each card created starts with a position field value of one. That is, each card uses its own position field when deployed (they do not share the same field among them). So, of course it does not appear to change as each card is deployed.
If that is the case, should I put the variable somewhere else? Where?
danpost danpost

2015/3/21

#
Placing it in the Player class may work if both players place their cards in separate fields (general locations in the world). If they are combined within the same field, then the only place left to put it is in the World subclass.
ProfessionalNoob ProfessionalNoob

2015/3/21

#
Thanks for your response, but I don't know how to call a variable from another class. Should I put that variable inside a method and call that method?
danpost danpost

2015/3/21

#
ProfessionalNoob wrote...
Should I put that variable inside a method and call that method?
No. It will not be retained if put in a method. It must be declared outside any method.
I don't know how to call a variable from another class.
Why would you need to call it from another class? Where are you placing the field and where do you think you need to call it from?
ProfessionalNoob ProfessionalNoob

2015/3/21

#
The field is just a getY() value that I use. I think I might need to call it from another class so that it updates the position variable for every card object that's made. Either way, I need to update it every time a card object is made so that the position is different, which is why I assumed that it needed to be done in another actor.
danpost danpost

2015/3/21

#
I am so confused. I thought that a drag action was to be used to deploy each card being positioned. That would mean the cards are already created. But now, you are saying that you are updating their position every time a card object is made. This does not make any sense at all. What stage of the game are you working on -- the set-up or in-game play? explain exactly what you are trying to accomplish (like you would find as instructions for game set-up or game-play). First give a general over-view, then what part you are working on.
ProfessionalNoob ProfessionalNoob

2015/3/21

#
danpost wrote...
I am so confused. I thought that a drag action was to be used to deploy each card being positioned. That would mean the cards are already created. But now, you are saying that you are updating their position every time a card object is made. This does not make any sense at all. What stage of the game are you working on -- the set-up or in-game play? explain exactly what you are trying to accomplish (like you would find as instructions for game set-up or game-play). First give a general over-view, then what part you are working on.
Apologies for the confusion. Basically, what's happening is that the position variable is updated once the card is "deployed". Deployment is a boolean, and it only sets to true once the card has been clicked and dragged into the board.
ProfessionalNoob ProfessionalNoob

2015/3/21

#
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
{
 
    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 val;
   
    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()
{
        setLocation(43 + (50*(position+1)^2), 300);
        CardSounds[number].play();
}
}
What I'm trying to do is make it so that the cards do not land on one position. Rather, if a card is Deployed, I want it to be to the right of whatever previous card that was there.
danpost danpost

2015/3/21

#
How about positioning the cards with something like this:
public void Position()
{
    setLocation(93, 300);
    while (isTouching(Cards.class)) move(50);
    CadSounds[number].play();
}
You should make sure that you will never have enough cards on the board to reach to right edge of the world or this will cause an infinite loop (the card being placed would not be able to more far enough right to NOT be still touching another; and therefore, the condition of the 'while' loop will never be false to exit the loop).
You need to login to post a reply.